WebService体系之——CXF+SPRING文件上传
摘要:此篇笔记实现一个web项目中不可避免的功能——文件上传。主要是FileEntity这个file的封装javaBean的构建。测试时使用两种方法、一种是原始的获取webservice接口掉结果、另一种是使用spring实现上传。
一:简介
在前面搭建的spring+webservice项目的基础上实现文件上传。
1、在服务器端添加一个表示file信息的JavaBean:FileEntity。
5、新建webservice客户端项目(可直接使用前面笔记中创建的客户端项目)。
6、在客户端创建file实体类:FileEntity(属性名要完全相同、简单点就是直接拷贝、包名也要一样)。
7、创建与服务端功能完全相同的上传文件接口(直接拷贝。注意包名也要一样)。
8、使用spring配置文件获取服务器端发布的上传文件的webservice。
9、测试:
a) 使用原始的获取方式测试。
b) 使用spring注册的webservice测试。
二:具体实现步骤
1、在服务器端添加一个表示file信息的JavaBean:FileEntity代码:
package com.chy.ws.entity; import javax.activation.DataHandler; public class FileEntity { private String fileName; private String fileType; private DataHandler file; public String getFileName() { return fileName; } public void setFileName(String fileName) { this.fileName = fileName; } public String getFileType() { return fileType; } public void setFileType(String fileType) { this.fileType = fileType; } public DataHandler getFile() { return file; } public void setFile(DataHandler file) { this.file = file; } }
2、创建上传文件的服务接口——UploadFileService代码:
package com.chy.ws.service; import javax.jws.WebMethod; import javax.jws.WebParam; import javax.jws.WebService; import com.chy.ws.entity.FileEntity; @WebService public interface UploadFileService { @WebMethod public void uploadFile(@WebParam(name="fileEntity") FileEntity fileEntity); }
3、实现上传文件的服务接口——UploadFileServiceImpl代码:
package com.chy.ws.service; 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; public class UploadFileServerImpl implements UploadFileService { @Override public 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,n); } os.flush(); } catch (IOException e) { e.printstacktrace(); } finally { try { if (is != null) { is.close(); } if (os != null) { os.close(); } } catch (IOException e) { e.printstacktrace(); } } } }
4、将上传文件的服务接口通过spring注册发布——spring配置文件applicationContext-server.xml代码:
<?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 http://cxf.apache.org/jaxws http://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" /> <!-- services接口配置 --> <bean id="helloServicesBean" class="com.chy.ws.service.HelloServiceImpl" /> <bean id="uploadFileServiceBean" class="com.chy.ws.service.UploadFileServerImpl" /> <!-- CXF 配置WebServices的服务名及访问地址 --> <jaxws:server id="helloService" address="/HelloService" serviceClass="com.chy.ws.service.HelloService"> <jaxws:serviceBean> <ref bean="helloServicesBean" /> </jaxws:serviceBean> </jaxws:server> <jaxws:server id="uploadFileService" address="/UploadFileService" serviceClass="com.chy.ws.service.UploadFileService"> <jaxws:serviceBean> <ref bean="uploadFileServiceBean" /> </jaxws:serviceBean> </jaxws:server> </beans>
5、客户端——FileEntity代码:
package com.chy.ws.entity; import javax.activation.DataHandler; public class FileEntity { private String fileName; private String fileType; private DataHandler file; public String getFileName() { return fileName; } public void setFileName(String fileName) { this.fileName = fileName; } public String getFileType() { return fileType; } public void setFileType(String fileType) { this.fileType = fileType; } public DataHandler getFile() { return file; } public void setFile(DataHandler file) { this.file = file; } }
6、客户端——UploadFileService代码:
package com.chy.ws.service; import javax.jws.WebMethod; import javax.jws.WebParam; import javax.jws.WebService; import com.chy.ws.entity.FileEntity; @WebService public interface UploadFileService { @WebMethod public void uploadFile(@WebParam(name="fileEntity") FileEntity fileEntity); }
7、客户端——applicationContext-client.xml代码:
<?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 http://cxf.apache.org/jaxws http://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代码:
package com.chy.ws.test; import java.io.File; import javax.activation.DataHandler; 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.entity.FileEntity; import com.chy.ws.service.UploadFileService; @SuppressWarnings("unused") public 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. */ private static 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.class); try { uploadFileService.uploadFile(fileEntity); } catch (Exception e) { e.printstacktrace(); return; } System.out.println("Upload file succeed!"); } /** * Construct FileEntity. * @param the real file path. * @return FileEntity. */ private static FileEntity constructFileEntity(String filePath) { // construct FileEntity FileEntity fileEntity = new FileEntity(); File file = new File(filePath); fileEntity.setFileName(file.getName().substring(0,(file.getName().lastIndexOf(".")))); fileEntity.setFileType(filePath.substring(filePath.lastIndexOf(".")+1)); DataSource source = new FileDataSource(file); DataHandler handler = new DataHandler(source); fileEntity.setFile(handler); return fileEntity; } public static void main(String[] args) { String filePath = "D:\\text.txt"; //invokingUploadFile(filePath); invokingUploadFileBySpring(filePath); } }
补充:
1、注意:服务端和客户端的JavaBean的名称属性要一模一样、最好是连包一起拷贝。
2、完整项目图:
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。