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

WebService体系之——CXF+SPRING文件上传

发布时间:2020-12-16 21:49:06 所属栏目:安全 来源:网络整理
导读:WebService体系之——CXF+SPRING文件上传 ? ? ? ? 摘要:此篇笔记实现一个web项目中不可避免的功能——文件上传。主要是FileEntity这个file的封装javaBean的构建。测试时使用两种方法、一种是原始的获取webservice接口掉结果、另一种是使用spring实现上传。

WebService体系之——CXF+SPRING文件上传


? ? ? ? 摘要:此篇笔记实现一个web项目中不可避免的功能——文件上传。主要是FileEntity这个file的封装javaBean的构建。测试时使用两种方法、一种是原始的获取webservice接口掉结果、另一种是使用spring实现上传。

?

一:简介

?

? ? ? ??在前面搭建的spring+webservice项目的基础上实现文件上传。

? ? ? ??1、在服务器端添加一个表示file信息的JavaBean:FileEntity。

? ? ? ??2、创建上传文件的服务接口。

? ? ? ??3、实现上传文件的服务接口。

? ? ? ??4、将上传文件的服务接口通过spring注册发布。

? ? ? ??5、新建webservice客户端项目(可直接使用前面笔记中创建的客户端项目)。

? ? ? ??6、在客户端创建file实体类:FileEntity(属性名要完全相同、简单点就是直接拷贝、包名也要一样)。

? ? ? ??7、创建与服务端功能完全相同的上传文件接口(直接拷贝。注意包名也要一样)。

? ? ? ??8、使用spring配置文件获取服务器端发布的上传文件的webservice。

? ? ? ??9、测试:

? ? ? ??? ? ? ??a)???????使用原始的获取方式测试。

? ? ? ??? ? ? ??b)???????使用spring注册的webservice测试。

二:具体实现步骤

? ? ? ??1、在服务器端添加一个表示file信息的JavaBean:FileEntity代码:


[java]? view plain ?copy
?

在CODE上查看代码片

派生到我的代码片

  1. package?com.chy.ws.entity;??
  2. ??
  3. import?javax.activation.DataHandler;??
  4. public?class?FileEntity?{??
  5. ????private?String?fileName;??
  6. ????private?String?fileType;??
  7. private?DataHandler?file;??
  8. public?String?getFileName()?{??
  9. ????????return?fileName;??
  10. ????}??
  11. void?setFileName(String?fileName)?{??
  12. ????????this.fileName?=?fileName;??
  13. ????}??
  14. public?String?getFileType()?{??
  15. return?fileType;??
  16. void?setFileType(String?fileType)?{??
  17. this.fileType?=?fileType;??
  18. public?DataHandler?getFile()?{??
  19. return?file;??
  20. void?setFile(DataHandler?file)?{??
  21. this.file?=?file;??
  22. }??

? ? ? ??2、创建上传文件的服务接口——UploadFileService代码:

copy

? package?com.chy.ws.service;??
  • import?javax.jws.WebMethod;??
  • import?javax.jws.WebParam;??
  • import?javax.jws.WebService;??
  • import?com.chy.ws.entity.FileEntity;??
  • @WebService??
  • interface?UploadFileService?{??
  • ??????
  • ????@WebMethod??
  • void?uploadFile(@WebParam(name="fileEntity")?FileEntity?fileEntity);??
  • }??

  • ? ? ? ??3、实现上传文件的服务接口——UploadFileServiceImpl代码:

    copy

    ? import?java.io.FileOutputStream;??
  • import?java.io.IOException;??
  • import?java.io.InputStream;??
  • import?java.io.OutputStream;??
  • ??
  • import?javax.activation.DataHandler;??
  • import?com.chy.ws.entity.FileEntity;??
  • class?UploadFileServerImpl?implements?UploadFileService?{??
  • @Override??
  • void?uploadFile(FileEntity?fileEntity)?{??
  • ????????DataHandler?handler?=?fileEntity.getFile();??
  • ????????InputStream?is?=?null;??
  • ????????OutputStream?os?=?null;??
  • try?{??
  • ????????????is?=?handler.getInputStream();??
  • ????????????os?=?new?FileOutputStream("F:/"?+?fileEntity.getFileName()?+?"."??
  • ????????????????????+?fileEntity.getFileType());??
  • ????????????int?n?=?0;??
  • ????????????byte[]?b?=?new?byte[1024];??
  • while?((n?=?is.read(b))?!=?-1)?{??
  • ????????????????os.write(b,?0,?n);??
  • ????????????}??
  • ????????????os.flush();??
  • ????????}?catch?(IOException?e)?{??
  • ????????????e.printStackTrace();??
  • finally?{??
  • try?{??
  • ????????????????if?(is?!=?null)?{??
  • ????????????????????is.close();??
  • ????????????????}??
  • ????????????????if?(os?!=?null)?{??
  • ????????????????????os.close();??
  • ????????????????}??
  • ????????????}? ????????????????e.printStackTrace();??
  • ????????}??
  • ? ? ? ??4、将上传文件的服务接口通过spring注册发布——spring配置文件applicationContext-server.xml代码:

    [html]? copy
    ?

    派生到我的代码片

    1. <?xml?version="1.0"?encoding="UTF-8"?>??
    2. <beans?xmlns="http://www.springframework.org/schema/beans"??
    3. ????xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"?xmlns:jaxws="http://cxf.apache.org/jaxws"??
    4. ????xsi:schemaLocation="??????
    5. ????????????http://www.springframework.org/schema/beans???????
    6. ????????????http://www.springframework.org/schema/beans/spring-beans-3.0.xsd??????
    7. ????????????http://cxf.apache.org/jaxws??????
    8. ????????????http://cxf.apache.org/schemas/jaxws.xsd">??
    9. ????<!--?Import?apache?CXF?bean?definition?固定-->??
    10. ????import?resource="classpath:META-INF/cxf/cxf.xml"?/>??
    11. ????import?resource="classpath:META-INF/cxf/cxf-extension-soap.xml"?/>??
    12. import?resource="classpath:META-INF/cxf/cxf-servlet.xml"? ????<!--?services接口配置?-->??
    13. bean?id="helloServicesBean"?class="com.chy.ws.service.HelloServiceImpl"?bean?id="uploadFileServiceBean"?class="com.chy.ws.service.UploadFileServerImpl"?<!--?CXF?配置WebServices的服务名及访问地址?-->??
    14. jaxws:server?id="helloService"?address="/HelloService"?serviceClass="com.chy.ws.service.HelloService" ????????jaxws:serviceBean>??
    15. ????????????ref?bean="helloServicesBean"?</jaxws:serverjaxws:server?id="uploadFileService"?address="/UploadFileService"?serviceClass="com.chy.ws.service.UploadFileService"ref?bean="uploadFileServiceBean"?beans>????

    ? ? ? ??5、客户端——FileEntity代码:

    copy

    ? ? ? ? ??6、客户端——UploadFileService代码:

    copy

    ? ? ? ? ??7、客户端——applicationContext-client.xml代码:

    copy

    ? <?xml?version="1.0"?encoding="UTF-8"?>??
  • <beans?xmlns="http://www.springframework.org/schema/beans"??
  • ????xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"?xmlns:jaxws="http://cxf.apache.org/jaxws"??
  • ????xsi:schemaLocation="??????
  • ????????????http://www.springframework.org/schema/beans???????
  • ????????????http://www.springframework.org/schema/beans/spring-beans-3.0.xsd??????
  • //cxf.apache.org/jaxws??????
  • //cxf.apache.org/schemas/jaxws.xsd">??
  • ????<!--?Import?apache?CXF?bean?definition?-->??
  • ????<import?resource="classpath:META-INF/cxf/cxf.xml"?/>??
  • ????<import?resource="classpath:META-INF/cxf/cxf-extension-soap.xml"?/>??
  • import?resource="classpath:META-INF/cxf/cxf-servlet.xml"?/>??
  • ????<!--?CXF?webservices?客户端配置?-->??
  • ????<jaxws:client?id="helloClient"?serviceClass="com.chy.ws.service.HelloService"??
  • ????????address="http://localhost:8080/webservice_spring_server/services/HelloService">??
  • ????</jaxws:client>??
  • ????<!--?上传文件?-->??
  • ????<jaxws:client?id="uploadFileService"?serviceClass="com.chy.ws.service.UploadFileService"??
  • ????????address="http://localhost:8080/webservice_spring_server/services/UploadFileService">??
  • ????</jaxws:client>??
  • </beans>????

  • ? ? ? ??8、测试——UploadClient代码:

    copy

    ? package?com.chy.ws.test;??
  • import?java.io.File;??
  • import?javax.activation.DataSource;??
  • import?javax.activation.FileDataSource;??
  • import?org.apache.cxf.jaxws.JaxWsProxyFactoryBean;??
  • import?org.springframework.context.ApplicationContext;??
  • import?org.springframework.context.support.ClassPathXmlApplicationContext;??
  • import?com.chy.ws.service.UploadFileService;??
  • @SuppressWarnings("unused")??
  • class?UploadFileClient?{??
  • ????/**?
  • ?????*?use?original?method?to?test?upload?file.?
  • ?????*?@param?the?real?file?path.?
  • ?????*/??
  • private?static?void?invokingUploadFile(String?filePath){??
  • ????????FileEntity?fileEntity?=?constructFileEntity(filePath);??
  • ??????????
  • ????????//obtain?web?service??
  • ????????JaxWsProxyFactoryBean?factory?=?new?JaxWsProxyFactoryBean();??
  • ????????factory.setAddress("http://localhost:8080/webservice_spring_server/services/UploadFileService");??
  • ????????factory.setServiceClass(UploadFileService.class);??
  • ??????????
  • ????????//upload?file??
  • ????????UploadFileService?uploadFileService?=?(UploadFileService)factory.create();??
  • ????????uploadFileService.uploadFile(fileEntity);??
  • ????/**?
  • ?????*?use?the?spring?application?context?to?test?upload?file.?
  • ?????*?@param?the?real?file?path.?
  • ?????*/??
  • void?invokingUploadFileBySpring(String?filePath){??
  • ????????FileEntity?fileEntity?=?constructFileEntity(filePath);??
  • //Obtain?the?spring?UploadFileService?through?the?spring?application?context!??
  • ????????ApplicationContext?applicationContext?=?new?ClassPathXmlApplicationContext("applicationContext-client.xml");??
  • ????????UploadFileService?uploadFileService?=?applicationContext.getBean("uploadFileService",?UploadFileService. ????????????uploadFileService.uploadFile(fileEntity);??
  • ????????}?catch?(Exception?e)?{??
  • ????????????e.printStackTrace();??
  • return;??
  • ????????}??
  • ????????System.out.println("Upload?file?succeed!");??
  • ??????
  • ?????*?Construct?FileEntity.?
  • ?????*?@return?FileEntity.?
  • static?FileEntity?constructFileEntity(String?filePath)?{??
  • //?construct?FileEntity??
  • ????????FileEntity?fileEntity?=?new?FileEntity();??
  • ????????File?file?=?new?File(filePath);??
  • ????????fileEntity.setFileName(file.getName().substring("."))));??
  • ????????fileEntity.setFileType(filePath.substring(filePath.lastIndexOf(".")+1));??
  • ????????DataSource?source?=?new?FileDataSource(file);??
  • ????????DataHandler?handler?=?new?DataHandler(source);??
  • ????????fileEntity.setFile(handler);??
  • return?fileEntity;??
  • void?main(String[]?args)?{??
  • ????????String?filePath?=?"D:text.txt";??
  • //invokingUploadFile(filePath);??
  • ????????invokingUploadFileBySpring(filePath);??
  • 补充:

    ?????? 1、注意:服务端和客户端的JavaBean的名称属性要一模一样、最好是连包一起拷贝。

    ?????? 2、完整项目图:

    ? ? ? ??? ? ? ??


    ?

    ?

    (编辑:李大同)

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

    • 推荐文章
        热点阅读