1

I have a problem with the use of DataHandler with jax-ws. When the client invoke getName() method of the DataHandler, returns an empty string. On the server side, it is valued correctly. Here I report the portion of the code on the server side and the client side:

server-side

public @XmlMimeType("application/octet-stream") DataHandler fileDownload(String name) {
   try {
         File file = new File("/tmpwk/share/SharedRepository/apps-data/jaxws-large_upload/" + name);
         FileDataSource fds = new FileDataSource(file);
     DataHandler dh = new DataHandler(fds);
         // Note: getName(), return a String with file name.
     System.out.println("File Name = " + dh.getName() );
     System.out.println("Content Type  " + dh.getContentType());

     System.out.println("quiting from downloadFile...");    
     return dh;
    } catch(Exception e) {
        throw new WebServiceException(e);
    }

}

client side

public void download() throws IOException {
    System.out.println(">>>>> download <<<<<<");
    MTOMFeature feature = new MTOMFeature();
    UploadImplService service = new UploadImplService();
    UploadImpl proxy = service.getUploadImplPort(feature);        
    Map<String, Object> ctxt = ((BindingProvider)proxy).getRequestContext();
    ctxt.put(JAXWSProperties.HTTP_CLIENT_STREAMING_CHUNK_SIZE, 8192); 


    DataHandler dh = proxy.fileDownload("file.bin");
    InputStream in = dh.getInputStream();

    //Note:  getName() return a empty string, why ????
            System.out.println("File Name = " + dh.getName());
    System.out.println("Content Type = " + dh.getContentType());


    File file = new File("/tmp/dfile.bin");
    OutputStream out = new FileOutputStream(file);

    // Transfer bytes from in to out
    byte[] buf = new byte[8192];
    int len;
    while ((len = in.read(buf)) > 0) {
        out.write(buf, 0, len);
    }
    in.close();
    out.close();
}

Why does this happen?

Jav_Rock
  • 22,059
  • 20
  • 123
  • 164
user1640828
  • 11
  • 1
  • 3

3 Answers3

1

today I experienced the same circumstance. Fortunately, I found why it is.

If you debug deeper into the source code, you will find among JAX-WS, the DataHandler typed parameter or result will be wrapped into a MIMEPartStreamingDataHandler.

The dataSource of MIMEPartStreamingDataHandler is an inner class called StreamingDataSource. Defined by DataHandler, it's getName method should be delegated by it's dataSource. However, the getName method of StreamingDataSource, just return an empty String "". Just like:

public String getName() {
    return "";
}

You could have an overview of the source code at here.

One possible solution to this problem is that:

  1. Give your upload service method another parameter to specify it's name.
  2. Wrap your result with a String property.
FurtherLee
  • 51
  • 6
0

I too encountered the same problem, but some how i used StreamingDataHandler, and my code works.

dh=proxy.get("file.bin");   //WS CALL
if( dh!=null){          
    StreamingDataHandler sdh = (StreamingDataHandler)dh;            
    File file = new File(filePath+ File.separator +fileName);
    sdh.moveTo(file);
 }
sailor
  • 753
  • 1
  • 6
  • 17
0

I had the same issue and it kept me busy for a while. As strange as it might seem, I finally discovered that the name of the file is provided in the contentType property of DataHandler object!! Here is how to derive the file name:

String contentType = dataHandler.getContentType();
//if you print out contentType, it is something like this:  "application/pdf; name=9ad22859-cdc9-4de5-b374-2c36acc5c253.pdf"
String fileName = (contentType.substring(contentType.indexOf("name=") + 5)).trim();

Cheers!

Javad
  • 55
  • 5