title: Java Web Services: sending files using DataHandler class
body: <p>I'm new to Java Web Services, so I might be doing things wrong.</p>
  
  <p>I'm trying to transfer a file using the DataHandler - this is what I've got:</p>
  
  <p>Web Service:</p>
  
  <pre><code>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;
      }
  
  }
  </code></pre>
  
  <p>Client:</p>
  
  <pre><code>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
      }
  
      }
  
  }
  </code></pre>
  
  <p>It seems, as if everything is being done correctly, but the file created is empty - what am I doing wrong?</p>
  
  <p>================= EDIT ==================</p>
  
  <p>The DataHandler object returned by getfile() is null - is it not possible to return this object from a webservice?</p>