webservice文件上传下载(DataHandler 实现方式)
测试环境:axis2-1.6.1、6.0.20、jdk1.5
说明:本方式仅适用于文件小于10M的场景(否则会出现内存溢出),大文件的上传下载应另选其他方式。
?
1、创建要发布成webservice的java类。
import java.io.FileOutputStream;import java.io.IOException;import java.io.OutputStream;import javax.activation.DataHandler;import javax.activation.FileDataSource;/* * DataHandler处理方式 */public class BlobService2 {/* * 文件上传服务 */ public boolean uploadFile(String fileName,DataHandler dataHandler) { OutputStream os = null; try{ os = new FileOutputStream("F:\"+fileName); dataHandler.writeTo(os);//大附件也会出现内存溢出 os.flush(); }catch (Exception e){ e.printStackTrace(); return false; }finally{ try {os.close();} catch (IOException e) {e.printStackTrace();} } return true; } /* * 文件下载服务 */ public DataHandler downloadFile() { String filepath = "F:\\head.jpg"; DataHandler dataHandler = new DataHandler(new FileDataSource(filepath)); return dataHandler; }}?
2、将上面的java类编译后的class文件放到axis2\WEB-INF\pojo目录下。
?
3、编写客户端程序。
package client;import java.io.FileOutputStream;import java.util.Date;import javax.activation.DataHandler;import javax.activation.FileDataSource;import javax.xml.namespace.QName;import org.apache.axis2.addressing.EndpointReference;import org.apache.axis2.client.Options;import org.apache.axis2.rpc.client.RPCServiceClient;/* * 仅适用于小附件上传、下载,10M以下。 */public class BlobRPCClient2{ public static void main(String[] args) throws Exception { RPCServiceClient serviceClient = new RPCServiceClient(); Options options = serviceClient.getOptions(); EndpointReference targetEPR = new EndpointReference("http://localhost:8080/axis2/services/BlobService"); options.setTo(targetEPR); //=================测试文件上传================================== String filePath = "f:\\head.jpg"; DataHandler dataHandler = new DataHandler(new FileDataSource(filePath)); //设置入参(1、文件名,2、DataHandler) Object[] opAddEntryArgs = new Object[]{"我是上传的文件.jpg", dataHandler}; //返回值类型 Class<?>[] classes = new Class<?>[]{ Boolean.class }; //指定要调用的方法名及WSDL文件的命名空间 QName opAddEntry = new QName("http://ws.apache.org/axis2","uploadFile"); //执行文件上传 System.out.println(new Date()+" 文件上传开始"); Object returnValue = serviceClient.invokeBlocking(opAddEntry,opAddEntryArgs, classes)[0]; System.out.println(new Date()+" 文件上传结束,返回值="+returnValue); //=================测试文件下载================================== opAddEntry = new QName("http://ws.apache.org/axis2", "downloadFile"); opAddEntryArgs = new Object[]{}; classes = new Class<?>[]{ DataHandler.class }; System.out.println(new Date()+" 文件下载开始"); DataHandler returnHandler = (DataHandler) serviceClient.invokeBlocking(opAddEntry, opAddEntryArgs, classes)[0]; FileOutputStream fileOutPutStream = new FileOutputStream("F:\\我是下载的文件.jpg"); returnHandler.writeTo(fileOutPutStream); fileOutPutStream.flush(); fileOutPutStream.close(); System.out.println(new Date()+" 文件下载完成"); }}?
4、运行客户端程序,输出结果如下:
Fri Mar 16 11:48:11 CST 2012 文件上传开始Fri Mar 16 11:48:11 CST 2012 文件上传结束,返回值=trueFri Mar 16 11:48:11 CST 2012 文件下载开始Fri Mar 16 11:48:12 CST 2012 文件下载完成
?
http://huangqiqing123.iteye.com/blog/1455169
?