我是Java Web服务的新手,所以我可能做错了事情。
我正在尝试使用DataHandler传输文件-这是我得到的:
Web服务:
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;
}
}客户端:
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
}
}
}看起来好像一切都在正确完成,但创建的文件却是空的--我做错了什么?
=================编辑==================
getfile()返回的DataHandler对象为空-从not服务返回此对象是不可能的吗?
发布于 2009-11-06 17:51:41
如果返回的DataHandler为null,我猜可能是该方法出了问题(例如,您正在捕获的MalformedURLException )。如果没有,您可以尝试以不同的方式创建DataHandler,例如使用FileDataSource或ByteArrayDataSource。
https://stackoverflow.com/questions/1686450
复制相似问题