struts2文件下载

下面是jsp文件的代码:

  1. <%@pagelanguage="java"import="java.io.*"pageEncoding="GBK"%>
  2. <!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN">
  3. <html>
  4. <head>
  5. <title>MyJSP'down.jsp'startingpage</title>
  6. <metahttp-equiv="pragma"content="no-cache">
  7. <metahttp-equiv="cache-control"content="no-cache">
  8. <metahttp-equiv="expires"content="0">
  9. <metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">
  10. <metahttp-equiv="description"content="Thisismypage">
  11. <!--
  12. <linkrel="stylesheet"type="text/css"href="styles.css">
  13. -->
  14. </head>
  15. <body>
  16. <%
  17. //取得服务器"/download"目录的物理路径
  18. Stringpath=request.getRealPath("/download");
  19. //取得"/download/file"目录的file对象
  20. Filefile=newFile(path);
  21. //取得file目录下所有文件
  22. File[]files=file.listFiles();
  23. for(inti=0;i<files.length;i++){
  24. Stringfname=files[i].getName();
  25. //对文件名进行url编码(UTF-8指明fname原来的编码,UTF-8一般由本地编码GBK代替)
  26. fname=java.net.URLEncoder.encode(fname,"UTF-8");
  27. out.println("<ahref=download.action?name="+fname+">"
  28. +files[i].getName()+"</a><br>");
  29. }
  30. %>
  31. </body>
  32. </html>

struts.xml相应的Action配置:

  1. <actionname="download"class="com.hxz.action.DownloadAction">
  2. <resultname="success"type="stream">
  3. <paramname="contentType">application/rar</param>
  4. <paramname="inputName">targetFile</param>
  5. <paramname="contentDisposition">attachment;filename="${fileName}"</param>
  6. <paramname="bufferSize">4096</param>
  7. </result>
  8. </action>

DownloadAction

  1. packagecom.hxz.action;
  2. importjava.io.InputStream;
  3. importjava.io.UnsupportedEncodingException;
  4. importorg.apache.struts2.ServletActionContext;
  5. importcom.opensymphony.xwork2.ActionSupport;
  6. @SuppressWarnings("serial")
  7. publicclassDownloadActionextendsActionSupport{
  8. privateStringfileName;
  9. publicvoidsetFileName(){
  10. //得到请求下载的文件名
  11. Stringfname=ServletActionContext.getRequest().getParameter("name");
  12. try{
  13. /*
  14. *对fname参数进行UTF-8解码,注意:实际进行UTF-8解码时会使用本地编码,本机为GBK。
  15. *这里使用request.setCharacterEncoding解码无效.
  16. *只有解码了getTargetFile()方法才能在下载目录下正确找到请求的文件
  17. **/
  18. fname=newString(fname.getBytes("ISO-8859-1"),"UTF-8");
  19. }catch(Exceptione){
  20. e.printStackTrace();
  21. }
  22. this.fileName=fname;
  23. System.out.println(fileName);
  24. }
  25. /*
  26. *@getFileName
  27. *此方法对应的是struts.xml文件中的:
  28. *<paramname="contentDisposition">attachment;filename="${fileName}"</param>
  29. *这个属性设置的是下载工具下载文件时显示的文件名,
  30. *要想正确的显示中文文件名,我们需要对fileName再次编码
  31. *否则中文名文件将出现乱码,或无法下载的情况
  32. **/
  33. publicStringgetFileName()throwsUnsupportedEncodingException{
  34. fileName=newString(fileName.getBytes(),"ISO-8859-1");
  35. returnfileName;
  36. }
  37. publicInputStreamgetTargetFile()throwsException{
  38. this.setFileName();
  39. returnServletActionContext.getServletContext().getResourceAsStream(("/download/"+fileName));
  40. }
  41. publicStringexecute()throwsException{
  42. returnSUCCESS;
  43. }
  44. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值