一、需求
- 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;
import javax.xml.bind.annotation.XmlMimeType;
import javax.xml.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.apache.commons.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;
import javax.xml.ws.Endpoint;
public class TestPublish {
public static void main(String[] args) {
Endpoint.publish("http://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) {
//TestMain.getServerFile();
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);
dataService.receiveClientImage(dataHandler, filename);
}
}