Chinaunix首页 | 论坛 | 博客
  • 博客访问: 847681
  • 博文数量: 366
  • 博客积分: 10267
  • 博客等级: 上将
  • 技术积分: 4290
  • 用 户 组: 普通用户
  • 注册时间: 2012-02-24 14:04
文章分类

全部博文(366)

文章存档

2012年(366)

分类: 系统运维

2012-03-16 17:38:31

测试环境:axis2-1.6.1、6.0.20、jdk1.5

说明:本方式仅适用于文件小于10M的场景(否则会出现内存溢出),大文件的上传下载应另选其他方式。

1、创建要发布成webservice的java类。

Java代码 复制代码 收藏代码
  1. import java.io.FileOutputStream;
  2. import java.io.IOException;
  3. import java.io.OutputStream;
  4. import javax.activation.DataHandler;
  5. import javax.activation.FileDataSource;
  6. /*
  7. * DataHandler处理方式
  8. */
  9. public class BlobService2 {
  10. /*
  11. * 文件上传服务
  12. */
  13. public boolean uploadFile(String fileName,DataHandler dataHandler)
  14. {
  15. OutputStream os = null;
  16. try{
  17. os = new FileOutputStream("F:\\"+fileName);
  18. dataHandler.writeTo(os);//大附件也会出现内存溢出
  19. os.flush();
  20. }catch (Exception e){
  21. e.printStackTrace();
  22. return false;
  23. }finally{
  24. try {
  25. os.close();
  26. } catch (IOException e) {
  27. e.printStackTrace();
  28. }
  29. }
  30. return true;
  31. }
  32. /*
  33. * 文件下载服务
  34. */
  35. public DataHandler downloadFile()
  36. {
  37. String filepath = "F:\\head.jpg";
  38. DataHandler dataHandler = new DataHandler(new FileDataSource(filepath));
  39. return dataHandler;
  40. }
  41. }
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、编写客户端程序。

Java代码 复制代码 收藏代码
  1. package client;
  2. import java.io.FileOutputStream;
  3. import java.util.Date;
  4. import javax.activation.DataHandler;
  5. import javax.activation.FileDataSource;
  6. import javax.xml.namespace.QName;
  7. import org.apache.axis2.addressing.EndpointReference;
  8. import org.apache.axis2.client.Options;
  9. import org.apache.axis2.rpc.client.RPCServiceClient;
  10. /*
  11. * 仅适用于小附件上传、下载,10M以下。
  12. */
  13. public class BlobRPCClient2
  14. {
  15. public static void main(String[] args) throws Exception
  16. {
  17. RPCServiceClient serviceClient = new RPCServiceClient();
  18. Options options = serviceClient.getOptions();
  19. EndpointReference targetEPR = new EndpointReference("");
  20. options.setTo(targetEPR);
  21. //=================测试文件上传==================================
  22. String filePath = "f:\\head.jpg";
  23. DataHandler dataHandler = new DataHandler(new FileDataSource(filePath));
  24. //设置入参(1、文件名,2、DataHandler)
  25. Object[] opAddEntryArgs = new Object[]{"我是上传的文件.jpg", dataHandler};
  26. //返回值类型
  27. Class[] classes = new Class[]{ Boolean.class };
  28. //指定要调用的方法名及WSDL文件的命名空间
  29. QName opAddEntry = new QName("","uploadFile");
  30. //执行文件上传
  31. System.out.println(new Date()+" 文件上传开始");
  32. Object returnValue = serviceClient.invokeBlocking(opAddEntry,opAddEntryArgs, classes)[0];
  33. System.out.println(new Date()+" 文件上传结束,返回值="+returnValue);
  34. //=================测试文件下载==================================
  35. opAddEntry = new QName("", "downloadFile");
  36. opAddEntryArgs = new Object[]{};
  37. classes = new Class[]{ DataHandler.class };
  38. System.out.println(new Date()+" 文件下载开始");
  39. DataHandler returnHandler = (DataHandler) serviceClient.invokeBlocking(opAddEntry, opAddEntryArgs, classes)[0];
  40. FileOutputStream fileOutPutStream = new FileOutputStream("F:\\我是下载的文件.jpg");
  41. returnHandler.writeTo(fileOutPutStream);
  42. fileOutPutStream.flush();
  43. fileOutPutStream.close();
  44. System.out.println(new Date()+" 文件下载完成");
  45. }
  46. }
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(""); 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("","uploadFile"); //执行文件上传 System.out.println(new Date()+" 文件上传开始"); Object returnValue = serviceClient.invokeBlocking(opAddEntry,opAddEntryArgs, classes)[0]; System.out.println(new Date()+" 文件上传结束,返回值="+returnValue); //=================测试文件下载================================== opAddEntry = new QName("", "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、运行客户端程序,输出结果如下:

Java代码 复制代码 收藏代码
  1. Fri Mar 16 11:48:11 CST 2012 文件上传开始
  2. Fri Mar 16 11:48:11 CST 2012 文件上传结束,返回值=true
  3. Fri Mar 16 11:48:11 CST 2012 文件下载开始
  4. Fri Mar 16 11:48:12 CST 2012 文件下载完成
Fri Mar 16 11:48:11 CST 2012 文件上传开始 Fri Mar 16 11:48:11 CST 2012 文件上传结束,返回值=true Fri Mar 16 11:48:11 CST 2012 文件下载开始 Fri Mar 16 11:48:12 CST 2012 文件下载完成

http://huangqiqing123.iteye.com/blog/1455169

阅读(2422) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~