-
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代码:
2、创建上传文件的服务接口——UploadFileService代码:
copy
- 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.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代码:
- <?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:serverjaxws:server id="uploadFileService" address="/UploadFileService" serviceClass="com.chy.ws.service.UploadFileService"ref bean="uploadFileServiceBean" beans>
5、客户端——FileEntity代码:
copy
copy
copy
8、测试——UploadClient代码:
copy
补充:
1、注意:服务端和客户端的JavaBean的名称属性要一模一样、最好是连包一起拷贝。
2、完整项目图: