加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

webservice文件上传下载(byte[] 实现方式)

发布时间:2020-12-17 00:20:04 所属栏目:安全 来源:网络整理
导读:转载自:http://huangqiqing123.iteye.com/blog/1454819 测试环境:axis2-1.6.1、6.0.20、jdk1.5 说明:本方式仅适用于文件小于10M的场景(否则会出现内存溢出),大文件的上传下载应另选其他方式。 ? 1、创建要发布成webservice的java类。 Java代码 ? impor

转载自:http://huangqiqing123.iteye.com/blog/1454819


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

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

?

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

Java代码 ?

收藏代码

  1. import?java.io.FileInputStream;??
  2. import?java.io.FileOutputStream;??
  3. import?java.io.IOException;??
  4. ??
  5. public?class?BlobService?{??
  6. ??
  7. ????/*?
  8. ?????*?文件上传服务?
  9. ?????*/??
  10. ????public?boolean?uploadFile(String?fileName,byte[]?bytes)??
  11. ????{??
  12. ????????FileOutputStream?fos?=?null;??
  13. ????????try{??
  14. ????????????fos?=?new?FileOutputStream("F:"+fileName);??
  15. ??????????????
  16. ????????????//将字节数组bytes中的数据,写入文件输出流fos中??
  17. ????????????fos.write(bytes);??
  18. ????????????fos.flush();??
  19. ????????}catch?(Exception?e){??
  20. ????????????e.printStackTrace();??
  21. ????????????return?false;??
  22. ????????}finally{??
  23. ????????????try?{??
  24. ????????????????fos.close();??
  25. ????????????}?catch?(IOException?e)?{??
  26. ????????????????e.printStackTrace();??
  27. ????????????}?????
  28. ????????}??
  29. ????????return?true;??
  30. ????}??
  31. ????/*?
  32. ?????*?文件下载服务?
  33. ?????*/??
  34. ????public?byte[]?downloadFile()??
  35. ????{??
  36. ????????String?filepath?=?"F:head.jpg";??
  37. ????????FileInputStream?in?=?null;??
  38. ????????byte?bytes[]?=?null;??
  39. ????????try?{??
  40. ????????????in?=?new?FileInputStream(filepath);??
  41. ????????????bytes?=?new?byte[in.available()];??
  42. ??????????????
  43. ????????????//从输入流in中,将?bytes.length?个字节的数据读入字节数组bytes中??
  44. ????????????in.read(bytes);??
  45. ????????}?catch?(Exception?e)?{??
  46. ????????????e.printStackTrace();??
  47. ????????}finally{?????????
  48. ????????????try?{??
  49. ????????????????in.close();??
  50. ????????????}?catch?(IOException?e)?{??
  51. ????????????????e.printStackTrace();??
  52. ????????????}??
  53. ????????}??
  54. ????????return?bytes;??
  55. ????}??
  56. }??

?

?

2、将上面的java类编译后的class文件放到axis2WEB-INFpojo目录下。

?

3、编写客户端程序。

Java代码 ?

收藏代码

  1. package?client;??
  2. ??
  3. import?java.io.FileInputStream;??
  4. import?java.io.FileOutputStream;??
  5. import?java.util.Date;??
  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. public?class?BlobRPCClient??
  12. {??
  13. ????public?static?void?main(String[]?args)?throws?Exception??
  14. ????{??
  15. ????????RPCServiceClient?serviceClient?=?new?RPCServiceClient();??
  16. ????????Options?options?=?serviceClient.getOptions();??
  17. ????????EndpointReference?targetEPR?=?new?EndpointReference("http://localhost:8080/axis2/services/BlobService");??
  18. ????????options.setTo(targetEPR);??
  19. ?????????
  20. ????????//=================测试文件上传==================================??
  21. ??????????
  22. ????????String?filePath?=?"f:head.jpg";??
  23. ????????FileInputStream?fis?=?new?FileInputStream(filePath);??
  24. ?????????
  25. ????????//?创建保存要上传的图像文件内容的字节数组??
  26. ????????byte[]?buffer?=?new?byte[fis.available()];??
  27. ??????????
  28. ????????//将输入流fis中的数据读入字节数组buffer中??
  29. ????????fis.read(buffer);????
  30. ????????
  31. ????????//设置入参(1、文件名,2、文件字节流数组)??
  32. ????????Object[]?opAddEntryArgs?=?new?Object[]{"我是上传的文件.jpg",?buffer};??
  33. ??????????
  34. ????????//返回值类型??
  35. ????????Class<?>[]?classes?=?new?Class<?>[]{?Boolean.class?};??
  36. ??????????
  37. ????????//指定要调用的方法名及WSDL文件的命名空间??
  38. ????????QName?opAddEntry?=?new?QName("http://ws.apache.org/axis2","uploadFile");??
  39. ??????????
  40. ????????//关闭流??
  41. ????????fis.close();??
  42. ???????
  43. ????????//执行文件上传??
  44. ????????System.out.println(new?Date()+"?文件上传开始");??
  45. ????????Object?returnValue?=?serviceClient.invokeBlocking(opAddEntry,opAddEntryArgs,?classes)[0];??
  46. ????????System.out.println(new?Date()+"?文件上传结束,返回值="+returnValue);??
  47. ????????
  48. ????????//=================测试文件下载==================================??
  49. ??
  50. ????????opAddEntry?=?new?QName("http://ws.apache.org/axis2",?"downloadFile");??
  51. ??????????
  52. ????????System.out.println(new?Date()+"?文件下载开始");??
  53. ????????byte?bytes[]?=?(byte[])?serviceClient.invokeBlocking(opAddEntry,?new?Object[]{},?new?Class[]{byte[].class})[0];??
  54. ????????FileOutputStream?fileOutPutStream?=?new?FileOutputStream("F:我是下载的文件.jpg");??
  55. ?????????
  56. ????????//将字节数组bytes中的数据,全部写入输出流fileOutPutStream中??
  57. ????????fileOutPutStream.write(bytes);??
  58. ????????fileOutPutStream.flush();??
  59. ????????fileOutPutStream.close();??
  60. ????????System.out.println(new?Date()+"?文件下载完成");??
  61. ????}??
  62. }??

?

?

4、运行客户端程序,输出结果如下:

Java代码 ?

收藏代码

  1. Thu?Mar?15?20:42:55?CST?2012?文件上传开始??
  2. Thu?Mar?15?20:42:56?CST?2012?文件上传结束,返回值=true??
  3. Thu?Mar?15?20:42:56?CST?2012?文件下载开始??
  4. Thu?Mar?15?20:42:56?CST?2012?文件下载完成??

?

?

5、打开目录 F:,会看到:


?

http://huangqiqing123.iteye.com/admin/blogs/1454819

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读