理解和灵活应用 Struts2 的文件下载功能

文件下载给我们最直接的概念就是,给个文件链接点击就下载。似乎太简单,然而 Struts2 却把它作为一个独立的学问来对待,理由有四: 1. 文件名为中文时,直接点击下载,链接可能会走样(某些浏览器,URL 编码的问题),致使无法下载。 2. 不总是从下载实际的文件,文件内容有时候是动态生成的,如数据库中的内容。 3. 对于知名的文件类型不让浏览器直接打开,而是出现下载对话框保存文件。例如,要下载的文件是 .txt 的,可能直接就在浏览器中显示其内容。 4. 需要授权才能下载文件时 当然对于以上若干问题,Servlet/JSP 都能通过正确的 URL 编码,响应头设置、权限代码控制解决,只是 Struts2 让我们处理起来更方便了,内部原理自然是一样的。 先来看下 Servlet 如何实现文件下载的,直接见代码: view sourceprint?01.PrintWriter out = response.getWriter(); 02. 03.//不管实际类型,待下载文件 ContentType 统一指定为 application/octet-stream 04.response.setContentType("application/octet-stream"); 05. 06.//中文文件名必须转码为 ISO8859-1,否则为乱码 07.String fileName = new String("文本文件.txt".getBytes(), "ISO8859-1"); 08. 09.//作为附件下载,相应的 "inline;filename = "+fileName 是在线(浏览器中显示内容)打开 10.response.setHeader("Content-Disposition", "attachment;filename=" + fileName); 11. 12.//因为文件编码也为 ISO8859-1,所以内容须转码成 ISO8859-1,尚不知如何控制下载文本文件的编码 13.//或有谁知道的,还请告诉我一下。 文件内容可以从物理文件中来,或者数据库中读取填入等等 14.out.write(new String("Servlet 文件下载测试".getBytes(), "ISO8859-1")); 15. 16.out.close(); 知道了上面各行的含义,再来看下 Struts2 的解决方式,其实不过是把某些代码的功能移入到了配置文件而已。在李刚所著的《Struts 2 权威指南》中说 Struts 实现文件下载是由一个 download 拦截器。其实不然,只是一个 StreamResult(org.apache.struts2.dispatcher.StreamResult) 而已,也不像实现文件上传那样要额外的 JAR 包。在 StreamResult 中有以下几个默认属性要留意一下: public static final String DEFAULT_PARAM = "inputName"; protected String contentType = "text/plain"; protected String contentDisposition = "inline"; protected String inputName = "inputStream"; protected InputStream inputStream; protected int bufferSize = 1024; StreamResult 的实现细节敬请阅读它的源代码,实现过程一言以蔽之就是:从 inputStream 获取内容,以相应的 contentType、contentDisposition 和 bufferSize 输出给浏览器,对 contentType 和 contentDisposition 的相应设置就能实现文件下载,可对照前面 Servlet 的实现。看个实际的例子吧。 struts.xml 中 Action 的配置,假定 Action 类为 com.unmi.DownLoadAction view sourceprint?01. 02. 03. application/octet-stream 04. 05. 07. inputStream 08. 09. 12. attachment;filename="${fileName}" 13. 4096 14. 说明:对于上面的配置其他参数可以用默认值,关键就是 contentDisposition 要设置为 attachment 才能提示下载,同时用 filename 指定文件名,若直接指定非动态的文件名。 DownloadAction 代码,需要实现 getInputStream() 返回输入流;因前面用的动态文件名,所以须加上 getFileName() 返回文件名,若非动态文件名,则该方法可省去。 view sourceprint?01.package com.unmi.action; 02. 03.import java.io.*; 04.import java.text.*; 05.import java.util.Date; 06. 07./** 08. * 文件下载的 Action 09. * @author Unmi 10. */ 11.public class NetbookSerialAction { 12. 13. public String execute() throws Exception { 14. //这里可加入权限控制 15. return "success"; 16. } 17. 18. //获得下载文件的内容,可以直接读入一个物理文件或从数据库中获取内容 19. public InputStream getInputStream() throws Exception { 20. //return new FileInputStream("somefile.rar"); 直接下载 somefile.rar 21. 22. //和 Servlet 中不一样,这里我们不需对输出的中文转码为 ISO8859-1 23. return new ByteArrayInputStream("Struts2 文件下载测试".getBytes()); 24. } 25. 26. //对于配置中的 ${fileName}, 获得下载保存时的文件名 27. public String getFileName() { 28. DateFormat df = new SimpleDateFormat("yyyy-MM-dd"); 29. String fileName = "序列号(" + df.format(new Date()) + ").txt"; 30. try { 31. //中文文件名也是需要转码为 ISO8859-1,否则乱码 32. return new String(fileName.getBytes(), "ISO8859-1"); 33. } catch (UnsupportedEncodingException e) { 34. return "impossible.txt"; 35. } 36. } 37.} 谨记一个就是,要想下载的文件名不乱码就要以 ISO8859-1 字符集进行转码,内容会否乱码可在调试中解决。 好啦,启动服务,访问 http://localhost:8080/teststruts2/download.action,浏览器便会提示下载 序列号(2009-06-17).txt,内容为:“Struts2 文件下载测试”。 参考:1. 浅谈Struts2下载文件的方法实现 2. struts2多文件动态下载及中文解决方案
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
Hibernate和Struts2可以结合使用来实现登录功能。 首先,在Hibernate中,需要创建一个User实体类,该类包含用户名和密码等属性。然后,通过Hibernate提供的API,将用户输入的用户名和密码与数据库中的信息进行比对,以判断用户输入的是否正确。 在Struts2中,可以使用Action来处理用户提交的登录请求。在Action中,需要编写验证逻辑,通过调用Hibernate API来验证用户输入的用户名和密码是否正确。如果验证成功,则将用户信息存储在Session中,以便后续的操作使用。 以下是示例代码: User类: ``` public class User { private int id; private String username; private String password; //getter和setter } ``` Hibernate验证用户方法: ``` public boolean checkUser(String username, String password) { Session session = HibernateUtil.getSessionFactory().getCurrentSession(); session.beginTransaction(); Query query = session.createQuery("from User where username = :username and password = :password"); query.setString("username", username); query.setString("password", password); List<User> userList = query.list(); session.getTransaction().commit(); if (userList.size() > 0) { return true; } else { return false; } } ``` Struts2 Action类: ``` public class LoginAction extends ActionSupport { private String username; private String password; private UserService userService; public String execute() { boolean isLoginSuccess = userService.checkUser(username, password); if (isLoginSuccess) { HttpSession session = ServletActionContext.getRequest().getSession(); session.setAttribute("username", username); return SUCCESS; } else { return ERROR; } } //getter和setter } ``` 以上是简单的示例代码,具体的实现需要根据实际情况进行调整。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值