在Hessian学习笔记之Hessian实例这一篇中简单介绍了Hessian的一个入门实例,本篇主要是深入学习Hessian,即利用Hessian实现文件上传。
1、服务端接口
import java.io.InputStream; /** * 文件上传服务接口 * */ public interface FileUploadService { public void uploadFile(String filename,InputStream data); }
2、服务端接口具体实现
import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; /** * 文件上传服务端具体实现 * */ public class FileUploadServiceImpl implements FileUploadService { @Override public void uploadFile(String filename, InputStream data) { String path = "D:/tools/LearningByMyself/hessian/" ; BufferedInputStream in = null ; BufferedOutputStream out = null ; try{ //接收客户端上送的数据 in = new BufferedInputStream(data); //创建文件输出流 out = new BufferedOutputStream(new FileOutputStream(path + filename)); byte[] buffer = new byte[10240]; int read = in.read(buffer, 0, buffer.length); while(read > 0){ out.write(buffer, 0, read); read = in.read(buffer, 0, buffer.length); } System.out.println("file upload successfully"); }catch(IOException e){ throw new RuntimeException(e); }finally{ if(out != null){ try{ out.close(); }catch(IOException e){ throw new RuntimeException(e); } } if(in != null){ try{ in.close(); }catch(IOException e){ throw new RuntimeException(e); } } } } }
3、web.xml配置文件
<!-- 文件上传Begin--> <servlet> <servlet-name>fileUpload</servlet-name> <servlet-class>com.caucho.hessian.server.HessianServlet</servlet-class> <init-param> <param-name>service-class</param-name> <!-- service-class也可以写成home-class --> <param-value>com.zh.hessian.FileUploadServiceImpl</param-value> <!-- 服务器对应接口的实现 --> </init-param> <!-- 客户端的配置不是必须的,也可以不配置 --> <init-param> <param-name>api-class</param-name> <!-- 也可以写成home-api --> <param-value>com.zh.hessian.FileUploadService</param-value> <!-- 提供给客户端的接口 --> </init-param> </servlet> <servlet-mapping> <servlet-name>fileUpload</servlet-name> <url-pattern>/fileUpload</url-pattern> </servlet-mapping> <!-- 文件上传End -->
4、验证服务端是否可用
启动Tomcat后,在浏览器中输入地址:http://localhost:8080/上下文名称/fileUpload。由于个人定义的上下文不一致,所以此处可根据自己的上下文定义具体修改。例如:本人这里定义的上下文是JavaDemo,所以在浏览器上输入:http://localhost:8080/JavaDemo/fileUpload,回车后,会出现如下结果:
Hessian Requires POST
5、将服务端的接口文件打成jar包导入的客户端工程中
6、客户端
import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.InputStream; import com.caucho.hessian.client.HessianProxyFactory; public class FileUploadClient { //需要上传的文件 private static final String path = "D:/papers/work_opt.txt" ; //Hessian服务的url private static final String url = "http://127.0.0.1:8080/JavaDemo/fileUpload" ; public static void main(String[] args) throws Exception{ //创建HessianProxyFactory实例 HessianProxyFactory factory = new HessianProxyFactory(); //获得Hessian服务的远程引用 FileUploadService upload = (FileUploadService) factory.create(FileUploadService.class, url); //读取需要上传的文件 InputStream data = new BufferedInputStream(new FileInputStream(path)); //调用文件上传服务 upload.uploadFile("work_opt.txt", data); } }
7、测试
启动服务后,运行客户端程序。本人最后的测试结果如下: file upload successfully。再到对应的本地目录中取查看,是否有对应的上传文件,此处就不再显示结果,因为每个人所定义的文件目录不一样。