标签:
hessian较早版本通过 byte[] 进行文件传输;4.0之后支持 InputStream 作为参数或返回值进行传输。
注意:hessian会读取整个文件,如果文件过大,会导致JVM内存溢出。可以通过控制上传文件的大小,设置合理的JVM参数,以及采用随机读取方式来解决。
创建一个FileUploader Web项目作为文件上传的服务端,如下图所示:
FileUploadServiceI接口代码如下:
1 package file.upload.service; 2 3 import java.io.InputStream; 4 5 /** 6 * <p>ClassName: FileUploadServiceI<p> 7 * <p>Description: 文件上传服务接口<p> 8 * @author xudp 9 * @version 1.0 V 10 * @createTime 2014-8-8 上午10:41:27 11 */ 12 public interface FileUploadServiceI { 13 14 public void upload(String filename, InputStream data); 15 }
FileUploadServiceImpl类的代码如下:
1 package file.upload.service.impl; 2 3 import java.io.BufferedInputStream; 4 import java.io.BufferedOutputStream; 5 import java.io.FileOutputStream; 6 import java.io.IOException; 7 import java.io.InputStream; 8 9 import file.upload.service.FileUploadServiceI; 10 11 /** 12 * <p>ClassName: FileUploadServiceImpl<p> 13 * <p>Description: FileUploadServiceI文件上传接口的具体实现类<p> 14 * @author xudp 15 * @version 1.0 V 16 * @createTime 2014-8-8 上午10:42:43 17 */ 18 public class FileUploadServiceImpl implements FileUploadServiceI { 19 20 /* (non-Javadoc) 21 * @MethodName upload 22 * @Description 上传文件 23 * @author xudp 24 * @param filename 上传的文件名 25 * @param data 上传的文件的输入流 26 * @see file.upload.service.FileUploadServiceI#upload(java.lang.String, java.io.InputStream) 27 */ 28 public void upload(String filename, InputStream data) { 29 BufferedInputStream bis = null; 30 BufferedOutputStream bos = null; 31 try { 32 //获取客户端传递的InputStream 33 bis = new BufferedInputStream(data); 34 //创建文件输出流 35 bos = new BufferedOutputStream(new FileOutputStream( 36 "E:/fileUpload/" + filename)); 37 byte[] buffer = new byte[8192]; 38 int r = bis.read(buffer, 0, buffer.length); 39 while (r > 0) { 40 bos.write(buffer, 0, r); 41 r = bis.read(buffer, 0, buffer.length); 42 } 43 System.out.println("-------文件上传成功!-------------"); 44 } catch (IOException e) { 45 throw new RuntimeException(e); 46 } finally { 47 if (bos != null) { 48 try { 49 bos.close(); 50 } catch (IOException e) { 51 throw new RuntimeException(e); 52 } 53 } 54 if (bis != null) { 55 try { 56 bis.close(); 57 } catch (IOException e) { 58 throw new RuntimeException(e); 59 } 60 } 61 } 62 } 63 64 }
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app version="2.5" 3 xmlns="http://java.sun.com/xml/ns/javaee" 4 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 5 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 6 http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 7 <welcome-file-list> 8 <welcome-file>index.jsp</welcome-file> 9 </welcome-file-list> 10 11 <servlet> 12 <servlet-name>FileUploadService</servlet-name> 13 <servlet-class>com.caucho.hessian.server.HessianServlet</servlet-class> 14 <init-param> 15 <!-- home-class也可以写为service-class --> 16 <param-name>home-class</param-name> 17 <!-- 服务器对接口的现实,供客户端调用 --> 18 <param-value>file.upload.service.impl.FileUploadServiceImpl</param-value> 19 </init-param> 20 <init-param> 21 22 <!-- 23 提供给客户端调用的接口,这里的home-api配置不是必须的,可以不配置 24 --> 25 <!--home-api也可以写为api-class --> 26 <param-name>home-api</param-name> 27 <!-- 客户端接口 --> 28 <param-value>file.upload.service.FileUploadServiceI</param-value> 29 </init-param> 30 </servlet> 31 <servlet-mapping> 32 <servlet-name>FileUploadService</servlet-name> 33 <url-pattern>/FileUploadService</url-pattern> 34 </servlet-mapping> 35 36 </web-app>
到此,服务端的编写就完成了,部署FileUploader到Tomcat服务器,启动Tomcat服务器,在浏览器地址栏输入:http://localhost:8080/FileUploader/FileUploadService,查看是否可以调用。
看到这样的运行结果就说明Hessian服务器的测试通过。
创建一个普通的java项目作为文件上传客户端,添加对hessian-4.0.37.jar的引用,同时将Hessian服务端的FileUploadServiceI接口打包成jar包提供给文件上传客户端调用,如下图所示:
FileUploaderClient类的代码如下:
1 package file.uplaod.client; 2 3 import java.io.BufferedInputStream; 4 import java.io.FileInputStream; 5 import java.io.InputStream; 6 import com.caucho.hessian.client.HessianProxyFactory; 7 import file.upload.service.FileUploadServiceI; 8 9 /** 10 * <p>ClassName: FileUploaderClient<p> 11 * <p>Description: 文件上传客户端<p> 12 * @author xudp 13 * @version 1.0 V 14 * @createTime 2014-8-8 上午10:00:13 15 */ 16 public class FileUploaderClient { 17 //Hessian服务的url 18 private static final String url = "http://gacl:8080/FileUploader/FileUploadService"; 19 20 public static void main(String[] args) throws Exception { 21 //创建HessianProxyFactory实例 22 HessianProxyFactory factory = new HessianProxyFactory(); 23 //获得Hessian服务的远程引用 24 FileUploadServiceI uploader = (FileUploadServiceI) factory.create(FileUploadServiceI.class, url); 25 //读取需要上传的文件 26 InputStream data = new BufferedInputStream(new FileInputStream("E:/开发资料/电子书/Shiro教程.pdf")); 27 //调用远程服务上传文件。 28 uploader.upload("Shiro教程.pdf", data); 29 } 30 }
运行FileUploaderClient客户端,就可以将文件"Shiro教程.pdf"上传到服务器上的指定存储目录了,如下图所示:
标签:
原文地址:http://www.cnblogs.com/xdp-gacl/p/3898100.html