(九)⼆进制⽂件在webservice中的处理(以DataHandler⽅
式)
⼀、需求
1. 客户端从服务端下载附件
2. 客户端上传附件到服务端
⼆、案例
本章通过DataHander的⽅式来进⾏传递。
注意:  1:接⼝中要定义@MTOM
2:⽅法中要使⽤@XmlMimeType(value = "application/octet-stream")
服务端
  2.1  编写服务接⼝
package com.webservice;
import javax.activation.DataHandler;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
l.bind.annotation.XmlMimeType;
webservice实现l.ws.soap.MTOM;
/**
* 设置AttachementPart中MIME的功能
*
*
* 1:在接⼝中开启@MTOM注解。
*
* 2:在⽅法中加上@XmlMimeType(value = "application/octet-stream")
*
*/
@WebService
@MTOM
public interface IFileDataHandler {
@WebResult(name = "sendServerImageResult")
@XmlMimeType(value = "application/octet-stream")
public DataHandler sendServerImage();
public void receiveClientImage(@WebParam(name = "dataHandler")
@XmlMimeType(value = "application/octet-stream")
DataHandler dataHandler, @WebParam(name = "filename")
String filename);
}
  2.2  实现服务接⼝
package com.webservice;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.jws.WebService;
import org.apachemons.io.IOUtils;
@WebService(endpointInterface = "com.webservice.IFileDataHandler")
public class SendFileDataHandler implements IFileDataHandler {
public DataHandler sendServerImage() {
File file = new File("E:\\server\\jQuery1.4 API-20100204.chm");
DataSource dataSource = new FileDataSource(file);
DataHandler dataHandler = new DataHandler(dataSource);
return dataHandler;
}
public void receiveClientImage(DataHandler dataHandler, String filename) {        OutputStream outputStream = null;
try {
outputStream = new FileOutputStream("E:\\server\\" + filename + "");            dataHandler.writeTo(outputStream);
} catch (IOException e) {
e.printStackTrace();
} finally {
IOUtils.closeQuietly(outputStream);
}
}
}
  2.3  发布服务
package com.webservice;
l.ws.Endpoint;
public class TestPublish {
public static void main(String[] args) {
Endpoint.publish("localhost:5050/sendDataHandler",
new SendFileDataHandler());
System.out.println("服务发布成功...");
}
}
  客户端
  2.4  测试
package com.file_handler;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
public class TestMain {
public static void main(String[] args) {
//ServerFile();
TestMain.sendClientFile();
}
/**
* 接收服务端发送的⽂件
*/
private static void getServerFile() {
IFileDataHandler dataService = new SendFileDataHandlerService()
.getSendFileDataHandlerPort();
DataHandler dataHandler = dataService.sendServerImage();
try {
dataHandler.writeTo(new FileOutputStream("E:\\client\\test.chm"));        } catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
private static void sendClientFile() {
IFileDataHandler dataService = new SendFileDataHandlerService()
.getSendFileDataHandlerPort();
String filename = "Storm.zip";
DataSource dataSource = new FileDataSource(new File("E:\\client\\"                + filename + ""));
DataHandler dataHandler = new DataHandler(dataSource);
}
}