I am new to Java web services, so I might be wrong.
I am trying to transfer a file using a DataHandler - this is what I have:
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; @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; } }
Customer:
package fileclient; import java.io.FileOutputStream; import java.io.OutputStream; import javax.activation.DataHandler; public class Main { 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) {
It seems like everything was done correctly, but the created file is empty - what am I doing wrong?
================== EDIT ===================
The DataHandler returned by getfile () is null - is it impossible to return this object from a web service?
source share