我在将 DataHandler 与 jax-ws 一起使用时遇到问题。当客户端调用 DataHandler 的 getName() 方法时,返回一个空字符串。在服务器端,它的价值是正确的。这里我报告一下服务器端和客户端的部分代码:
服务器端
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);
}
}
客户端
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();
}
为什么会这样?