使用webservice上传文件

lilinhui

贡献于2013-06-28

字数:3294 关键词: WEB服务/RPC/SOA

WebService文件传输 一、服务器端: 新建工程,servicetest: 新建类文件,UploadFileService: 代码: package com.test; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.InputStream; import javax.activation.DataHandler; /** * function:Axis WebService完成文件上传服务器端 * */ public class UploadFileService { /** * function:传递文件 * * @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"; } } } 基于该实现类,创建 Web Service,如下图: 点击Next会出现,可以忽略这个警告。 会自动生成这些文件: 启动tomcat时,生成server-config.wsdd文件。 启动信息里面有:警告: Unable to find required classes (javax.activation.DataHandler and javax.mail.internet.MimeMultipart). Attachment support is disabled. 解决方法:将mail.jar和activation.jar放到lib目录下(我都是在http://www.jar114.com上找到的)。 二、客户端: 1.编写类UploadFileClient,代码: package com.test; import javax.activation.DataHandler; import javax.activation.FileDataSource; import javax.xml.namespace.QName; import javax.xml.rpc.Call; import javax.xml.rpc.ParameterMode; import javax.xml.rpc.Service; import javax.xml.rpc.ServiceFactory; import org.apache.axis.encoding.XMLType; public class UploadFileClient { /** * @param args */ public static void main(String[] args) { String address = "http://localhost:8080/servicetest/services/UploadFileService"; String namespaceURI = "http://hello.com"; String serviceName = "UploadFileServiceService"; String portName = "UploadFileService"; String operationName = "upload"; try { ServiceFactory factory = ServiceFactory.newInstance(); Service service = factory.createService(new QName(serviceName)); Call call = service.createCall(new QName(portName)); call.setTargetEndpointAddress(address); QName sqn = new QName("fileName", "String"); String filepath = "d://test.zip"; DataHandler handler = new DataHandler(new FileDataSource(filepath)); QName hqn = new QName("handler", "DataHandler"); call.setOperationName(new QName(namespaceURI, operationName)); call.addParameter("handler", hqn, ParameterMode.IN); call.addParameter("fileName", sqn, ParameterMode.IN); call.setReturnType(XMLType.XSD_STRING); Object[] inParams = new Object[2]; inParams[0] = handler; inParams[1] = "remote_test.zip"; String value = call.invoke(inParams).toString(); System.out .println("Dynamic Client Invocation:\n\tThe summation is: " + value); } catch (Exception e) { e.printStackTrace(); } } } 启动tomcat以后,运行UploadFileClient,文件传输就完成了 未解决的问题: Tomcat6和Tomcat7在eclipse中启动的时候报错: 2011-7-12 13:05:59 org.apache.catalina.startup.SetContextPropertiesRule begin 警告: [SetContextPropertiesRule]{Context} Setting property 'source' to 'org.eclipse.jst.j2ee.server:test1' did not find a matching property. 参考资料: 1.http://www.ibm.com/developerworks/cn/webservices/1106_webservicessecurity/?cmp=dwskl&cpb=dw&ct=dwcon&cr=cn_CCID&ccy=cn 2.http://www.cnblogs.com/hoojo/archive/2010/12/23/1911385.html 3.http://topic.csdn.net/t/20060702/12/4855371.html

下载文档,方便阅读与编辑

文档的实际排版效果,会与网站的显示效果略有不同!!

需要 5 金币 [ 分享文档获得金币 ]
0 人已下载

下载文档

相关文档