见:http://www.cnblogs.com/hoojo/archive/2010/12/20/1911385.html
WebService处理传递普通的信息,还可以传输文件,下面介绍WebService是怎么完成文件传输的。
package com.hoo.service; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.InputStream; import javax.activation.DataHandler; /** * <b>function:</b>Axis WebService完成文件上传服务器端 * @author hoojo * @createDate Dec 18,2010 1:16:16 PM * @file UploadFileService.java * @package com.hoo.service * @project AxisWebService * @blog http://blog.csdn.net/IBM_hoojo * @email hoojo_@126.com * @version 1.0 */ public class UploadFileService { * <b>function:</b>传递文件 * @param handler DataHandler这个参数必须 * 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 " ; } } return " fileName is null " ; } } }
上传方法和我们以前在Web中上传唯一不同的就是参数一DataHandler,可以将这类看成文件传输器,他可以把文件序列化。然后通过DataHandler可以得到一个输入流InputStream,通过这个流可以读到文件的内容。其他的操作和普通上传类似。
上面才xml节点元素在前面都见过了,说明下operation中的参数,注意要指定参数类型,特别是DataHandler的类型,然后就是typeMapping的serializer、deserializer的序列化和反序列化工厂类的配置。
3、 用dos命令发布当前WebService
C:\SoftWare\tomcat-5.0.28\tomcat-5.0.28\webapps\AxisWebService\WEB-INF>java -Djava.ext.dirs=lib org.apache.axis.client.AdminClient -lhttp://localhost:8080/AxisWebService/services/AdminService deployUpload.wsdd
发布完成后,可以通过这个地址查看uploadFile这个service了
http://localhost:8080/AxisWebService/servlet/AxisServlet
4、 编写客户端代码
至此,文件传输就完成了。怎么样,还不错吧!
如果你用myEclipse进行开发的话,运行时可能会出现以下的错误:
Exception in thread "main" java.lang.NoClassDefFoundError: com/sun/mail/util/LineInputStream
原因是jar包版本不统一,解决方法如下:
删除Java EE 5 Libraries/javaee.jar/mail里的包有东西.
具体方法如下:
用rar打开X:/Program Files/MyEclipse 6.0/myeclipse/eclipse/plugins/com.genuitec.eclipse.j2eedt.core_6.0.1.zmyeclipse601200710/data/libraryset/EE_5/javaee.jar,然后删除mail,一切就ok了.