java 利用webservice传输文件
2016-06-05 08:46
639 查看
几天前,好不容易用java的ftp方式实现传输数据了,可结果客户觉得ftp传输数据不安全,而且嫌弃ftp还单独创建一个ftp服务器,觉得管理起来比较麻烦,所以人家希望用webservice来传输数据,虽然webservice用来传递数据量比较大的数据时,会不太稳定,但是客户喜欢那就照做啦。关于ftp数据上传我会单独写文章来说明。
好了下面就来开始整理webservice传输文件把。
既然用webservice,那么就需要发布服务了,关于发布服务的方式也有很多,这里我采用的是axis来发布,关于axis如何发布服务,可以查看我的这边博客:Axis2开发webservice案例详解
项目代码结果如下图:
1. 服务器端代码,UploadFileService.java
服务发布的效果是:
进入wsdl文件内容如下:
2. 客户端程序:UploadFileClient.java
执行程序后:效果如图:
项目代码可在这里下载:
http://download.csdn.net/detail/zp357252539/9541237
好了下面就来开始整理webservice传输文件把。
既然用webservice,那么就需要发布服务了,关于发布服务的方式也有很多,这里我采用的是axis来发布,关于axis如何发布服务,可以查看我的这边博客:Axis2开发webservice案例详解
项目代码结果如下图:
1. 服务器端代码,UploadFileService.java
package com.webserviceUploadFile; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.InputStream; import javax.activation.DataHandler; /** 1. <b>function:</b>Axis WebService完成文件上传服务器端 */ public class UploadFileService { /** * <b>function:</b>传递文件 * * @param handler * DataHandler这个参数必须 * @param fileName * 文件名称 * @return upload Info */ public String upload(DataHandler handler, String fileName) { if (fileName != null && !"".equals(fileName)) { File file = new File(fileName); if (handler != null) { InputStream is = null; FileOutputStream fos = null; try { is = handler.getInputStream(); fos = new FileOutputStream(file); byte[] buff = new byte[1024 * 8]; int len = 0; while ((len = is.read(buff)) > 0) { fos.write(buff, 0, len); } } catch (FileNotFoundException e) { return "fileNotFound"; } catch (Exception e) { return "upload File failure"; } finally { try { if (fos != null) { fos.flush(); fos.close(); } if (is != null) { is.close(); } } catch (Exception e) { e.printStackTrace(); } } return "file absolute path:" + file.getAbsolutePath(); } else { return "handler is null"; } } else { return "fileName is null"; } } }
服务发布的效果是:
进入wsdl文件内容如下:
2. 客户端程序:UploadFileClient.java
package com.webserviceUploadFile; import java.rmi.RemoteException; import javax.activation.DataHandler; import javax.activation.FileDataSource; import javax.xml.namespace.QName; import javax.xml.rpc.ParameterMode; import javax.xml.rpc.ServiceException; import org.apache.axis.client.Call; import org.apache.axis.client.Service; import org.apache.axis.encoding.XMLType; import org.apache.axis.encoding.ser.JAFDataHandlerDeserializerFactory; import org.apache.axis.encoding.ser.JAFDataHandlerSerializerFactory; /** * <b>function:</b>上传文件WebService客户端 */ public class UploadFileClient { public static void main(String[] args) throws ServiceException, RemoteException { String url = "http://localhost:8080/axis/services/UploadFile"; // String fileName = "readMe.txt"; String fileName = "算法导论.pdf"; String path = System.getProperty("user.dir") + "\\files\\" + fileName; System.out.println(path); //这样就相当于构造了一个带文件路径的File了 DataHandler handler = new DataHandler(new FileDataSource(path)); Service service = new Service(); Call call = (Call) service.createCall(); call.setTargetEndpointAddress(url); /** * 注册异常类信息和序列化类 ns:FileUploadHandler 和 wsdd 配置文件中的typeMapping中的xmlns:hns="ns:FileUploadHandler" 的对应 DataHandler * 和 wsdd 配置文件中的typeMapping中的qname="hns:DataHandler"的DataHandler对应 */ QName qn = new QName("ns:FileUploadHandler", "DataHandler"); call.registerTypeMapping(DataHandler.class, qn, JAFDataHandlerSerializerFactory.class, JAFDataHandlerDeserializerFactory.class); call.setOperationName(new QName(url, "upload")); //设置方法形参,注意的是参数1的type的DataHandler类型的,和上面的qn的类型是一样的 call.addParameter("handler", qn, ParameterMode.IN); call.addParameter("fileName", XMLType.XSD_STRING, ParameterMode.IN); //设置返回值类型,下面2种方法都可以 call.setReturnClass(String.class); //call.setReturnType(XMLType.XSD_STRING); String result = (String) call.invoke(new Object[] { handler, "D:/算法导论.pdf" }); System.out.println(result); } }
执行程序后:效果如图:
项目代码可在这里下载:
http://download.csdn.net/detail/zp357252539/9541237
相关文章推荐
- java对世界各个时区(TimeZone)的通用转换处理方法(转载)
- java-注解annotation
- java-模拟tomcat服务器
- java-用HttpURLConnection发送Http请求.
- java-WEB中的监听器Lisener
- Android IPC进程间通讯机制
- Android Native 绘图方法
- Android java 与 javascript互访(相互调用)的方法例子
- 介绍一款信息管理系统的开源框架---jeecg
- 聚类算法之kmeans算法java版本
- java实现 PageRank算法
- PropertyChangeListener简单理解
- c++11 + SDL2 + ffmpeg +OpenAL + java = Android播放器
- 插入排序
- 冒泡排序
- 堆排序
- 快速排序
- 二叉查找树