Java Web 服务:使用 DataHandler 类发送文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1686450/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 17:35:58  来源:igfitidea点击:

Java Web Services: sending files using DataHandler class

javaweb-servicesfile

提问by zbigh

I'm new to Java Web Services, so I might be doing things wrong.

我是 Java Web 服务的新手,所以我可能做错了事。

I'm trying to transfer a file using the DataHandler - this is what I've got:

我正在尝试使用 DataHandler 传输文件 - 这就是我所拥有的:

Web Service:

网络服务:

import java.net.MalformedURLException;
import java.net.URL;
import javax.activation.DataHandler;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.xml.bind.annotation.XmlMimeType;

/**
 *
 * @author pc1
 */
@WebService()
public class WSFileSender {

    @WebMethod( operationName = "getfile" )
    public @XmlMimeType( "application/octet-stream" ) DataHandler getfile( @WebParam( name = "path" ) String path ) {

        DataHandler datahandler = null;

        try {
            datahandler = new DataHandler( new URL( path ) );
        }
        catch ( MalformedURLException e ) {
            System.out.println( "Bad" );
        }

        return datahandler;
    }

}

Client:

客户:

package fileclient;

import java.io.FileOutputStream;
import java.io.OutputStream;
import javax.activation.DataHandler;

/**
 *
 * @author pc1
 */
public class Main {

    /**
     * @param args the command line arguments
     */
    public static void main( String[] args ) {

        try {
            fspg.WSFileSenderService service = new fspg.WSFileSenderService();
            fspg.WSFileSender port = service.getWSFileSenderPort();

            DataHandler handler = port.getfile( "FileSender/file.jpg" );

            OutputStream out = new FileOutputStream( "dest.jpg" );

            handler.writeTo( out );

            out.close();

            System.out.println( "Done" );

        } catch (Exception ex) {
        // TODO handle custom exceptions here
    }

    }

}

It seems, as if everything is being done correctly, but the file created is empty - what am I doing wrong?

似乎一切都正确完成,但创建的文件是空的 - 我做错了什么?

================= EDIT ==================

================ 编辑 ==================

The DataHandler object returned by getfile() is null - is it not possible to return this object from a webservice?

getfile() 返回的 DataHandler 对象为 null - 是否无法从 Web 服务返回此对象?

采纳答案by Fabian Steeg

If the DataHandlerreturned is nullmy guess would be something goes wrong in that method (e.g. the MalformedURLExceptionyou are catching). If not, you could try to create the DataHandlerin a different way, e.g. with a FileDataSourceor a ByteArrayDataSource.

如果DataHandler返回的是null我的猜测,那么该方法会出现问题(例如,MalformedURLException您正在捕获)。如果没有,您可以尝试以DataHandler不同的方式创建,例如使用 aFileDataSource或 a ByteArrayDataSource