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

来源:互联网 发布:神魔布袋戏 知乎 编辑:程序博客网 时间:2024/06/07 17:42

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.   
  5. public class FileEntity {  
  6.     private String fileName;  
  7.     private String fileType;  
  8.     private DataHandler file;  
  9.     public String getFileName() {  
  10.         return fileName;  
  11.     }  
  12.     public void setFileName(String fileName) {  
  13.         this.fileName = fileName;  
  14.     }  
  15.     public String getFileType() {  
  16.         return fileType;  
  17.     }  
  18.     public void setFileType(String fileType) {  
  19.         this.fileType = fileType;  
  20.     }  
  21.     public DataHandler getFile() {  
  22.         return file;  
  23.     }  
  24.     public void setFile(DataHandler file) {  
  25.         this.file = file;  
  26.     }  
  27. }  

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


[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. package com.chy.ws.service;  
  2.   
  3. import javax.jws.WebMethod;  
  4. import javax.jws.WebParam;  
  5. import javax.jws.WebService;  
  6.   
  7. import com.chy.ws.entity.FileEntity;  
  8.   
  9. @WebService  
  10. public interface UploadFileService {  
  11.       
  12.     @WebMethod  
  13.     public void uploadFile(@WebParam(name="fileEntity") FileEntity fileEntity);  
  14. }  

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


[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. package com.chy.ws.service;  
  2.   
  3. import java.io.FileOutputStream;  
  4. import java.io.IOException;  
  5. import java.io.InputStream;  
  6. import java.io.OutputStream;  
  7.   
  8. import javax.activation.DataHandler;  
  9.   
  10. import com.chy.ws.entity.FileEntity;  
  11.   
  12. public class UploadFileServerImpl implements UploadFileService {  
  13.   
  14.     @Override  
  15.     public void uploadFile(FileEntity fileEntity) {  
  16.         DataHandler handler = fileEntity.getFile();  
  17.         InputStream is = null;  
  18.         OutputStream os = null;  
  19.         try {  
  20.             is = handler.getInputStream();  
  21.             os = new FileOutputStream("F:/" + fileEntity.getFileName() + "."  
  22.                     + fileEntity.getFileType());  
  23.             int n = 0;  
  24.             byte[] b = new byte[1024];  
  25.             while ((n = is.read(b)) != -1) {  
  26.                 os.write(b, 0, n);  
  27.             }  
  28.             os.flush();  
  29.         } catch (IOException e) {  
  30.             e.printStackTrace();  
  31.         } finally {  
  32.             try {  
  33.                 if (is != null) {  
  34.                     is.close();  
  35.                 }  
  36.                 if (os != null) {  
  37.                     os.close();  
  38.                 }  
  39.             } catch (IOException e) {  
  40.                 e.printStackTrace();  
  41.             }  
  42.         }  
  43.     }  
  44. }  

        4、将上传文件的服务接口通过spring注册发布——spring配置文件applicationContext-server.xml代码:


[html] view plain copy
 在CODE上查看代码片派生到我的代码片
  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.   
  10.     <!-- Import apache CXF bean definition 固定-->  
  11.     <import resource="classpath:META-INF/cxf/cxf.xml" />  
  12.     <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />  
  13.     <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />  
  14.   
  15.     <!-- services接口配置 -->  
  16.     <bean id="helloServicesBean" class="com.chy.ws.service.HelloServiceImpl" />  
  17.     <bean id="uploadFileServiceBean" class="com.chy.ws.service.UploadFileServerImpl" />  
  18.   
  19.     <!-- CXF 配置WebServices的服务名及访问地址 -->  
  20.     <jaxws:server id="helloService" address="/HelloService" serviceClass="com.chy.ws.service.HelloService">  
  21.         <jaxws:serviceBean>  
  22.             <ref bean="helloServicesBean" />  
  23.         </jaxws:serviceBean>  
  24.     </jaxws:server>  
  25.   
  26.     <jaxws:server id="uploadFileService" address="/UploadFileService" serviceClass="com.chy.ws.service.UploadFileService">  
  27.         <jaxws:serviceBean>  
  28.             <ref bean="uploadFileServiceBean" />  
  29.         </jaxws:serviceBean>  
  30.     </jaxws:server>  
  31. </beans>    

        5、客户端——FileEntity代码:


[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. package com.chy.ws.entity;  
  2.   
  3. import javax.activation.DataHandler;  
  4.   
  5. public class FileEntity {  
  6.     private String fileName;  
  7.     private String fileType;  
  8.     private DataHandler file;  
  9.     public String getFileName() {  
  10.         return fileName;  
  11.     }  
  12.     public void setFileName(String fileName) {  
  13.         this.fileName = fileName;  
  14.     }  
  15.     public String getFileType() {  
  16.         return fileType;  
  17.     }  
  18.     public void setFileType(String fileType) {  
  19.         this.fileType = fileType;  
  20.     }  
  21.     public DataHandler getFile() {  
  22.         return file;  
  23.     }  
  24.     public void setFile(DataHandler file) {  
  25.         this.file = file;  
  26.     }  
  27. }  

        6、客户端——UploadFileService代码:


[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. package com.chy.ws.service;  
  2.   
  3. import javax.jws.WebMethod;  
  4. import javax.jws.WebParam;  
  5. import javax.jws.WebService;  
  6.   
  7. import com.chy.ws.entity.FileEntity;  
  8.   
  9. @WebService  
  10. public interface UploadFileService {  
  11.       
  12.     @WebMethod  
  13.     public void uploadFile(@WebParam(name="fileEntity") FileEntity fileEntity);  
  14. }  

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


[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  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" />  
  13.   
  14.     <!-- CXF webservices 客户端配置 -->  
  15.     <jaxws:client id="helloClient" serviceClass="com.chy.ws.service.HelloService"  
  16.         address="http://localhost:8080/webservice_spring_server/services/HelloService">  
  17.     </jaxws:client>  
  18.   
  19.     <!-- 上传文件 -->  
  20.     <jaxws:client id="uploadFileService" serviceClass="com.chy.ws.service.UploadFileService"  
  21.         address="http://localhost:8080/webservice_spring_server/services/UploadFileService">  
  22.     </jaxws:client>  
  23. </beans>    

        8、测试——UploadClient代码:

 

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. package com.chy.ws.test;  
  2.   
  3. import java.io.File;  
  4.   
  5. import javax.activation.DataHandler;  
  6. import javax.activation.DataSource;  
  7. import javax.activation.FileDataSource;  
  8.   
  9. import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;  
  10. import org.springframework.context.ApplicationContext;  
  11. import org.springframework.context.support.ClassPathXmlApplicationContext;  
  12.   
  13. import com.chy.ws.entity.FileEntity;  
  14. import com.chy.ws.service.UploadFileService;  
  15.   
  16. @SuppressWarnings("unused")  
  17. public class UploadFileClient {  
  18.   
  19.     /** 
  20.      * use original method to test upload file. 
  21.      * @param the real file path. 
  22.      */  
  23.     private static void invokingUploadFile(String filePath){  
  24.         FileEntity fileEntity = constructFileEntity(filePath);  
  25.           
  26.         //obtain web service  
  27.         JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();  
  28.         factory.setAddress("http://localhost:8080/webservice_spring_server/services/UploadFileService");  
  29.         factory.setServiceClass(UploadFileService.class);  
  30.           
  31.         //upload file  
  32.         UploadFileService uploadFileService = (UploadFileService)factory.create();  
  33.         uploadFileService.uploadFile(fileEntity);  
  34.     }  
  35.       
  36.     /** 
  37.      * use the spring application context to test upload file. 
  38.      * @param the real file path. 
  39.      */  
  40.     private static void invokingUploadFileBySpring(String filePath){  
  41.         FileEntity fileEntity = constructFileEntity(filePath);  
  42.           
  43.         //Obtain the spring UploadFileService through the spring application context!  
  44.         ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext-client.xml");  
  45.         UploadFileService uploadFileService = applicationContext.getBean("uploadFileService", UploadFileService.class);  
  46.         try {  
  47.             uploadFileService.uploadFile(fileEntity);  
  48.         } catch (Exception e) {  
  49.             e.printStackTrace();  
  50.             return;  
  51.         }  
  52.         System.out.println("Upload file succeed!");  
  53.     }  
  54.       
  55.     /** 
  56.      * Construct FileEntity. 
  57.      * @param the real file path. 
  58.      * @return FileEntity. 
  59.      */  
  60.     private static FileEntity constructFileEntity(String filePath) {  
  61.         // construct FileEntity  
  62.         FileEntity fileEntity = new FileEntity();  
  63.         File file = new File(filePath);  
  64.         fileEntity.setFileName(file.getName().substring(0,(file.getName().lastIndexOf("."))));  
  65.         fileEntity.setFileType(filePath.substring(filePath.lastIndexOf(".")+1));  
  66.         DataSource source = new FileDataSource(file);  
  67.         DataHandler handler = new DataHandler(source);  
  68.         fileEntity.setFile(handler);  
  69.         return fileEntity;  
  70.     }  
  71.       
  72.     public static void main(String[] args) {  
  73.         String filePath = "D:\\text.txt";  
  74.         //invokingUploadFile(filePath);  
  75.         invokingUploadFileBySpring(filePath);  
  76.     }  
  77. }  

补充:

 

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

       2、完整项目图:


                

 

 

原创粉丝点击