首页 > 技术文章 > java文件下载

hemingshui 2013-10-04 14:27 原文

Action的写法:

package cn.itcast.struts2.demo2;

import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import java.net.URLEncoder;

import org.apache.struts2.ServletActionContext;

import sun.misc.BASE64Encoder;

import com.opensymphony.xwork2.ActionSupport;

/**  * 文件下载  *  * @author seawind  *  */

public class DownloadAction extends ActionSupport {

        private String filename;

        public void setFilename(String filename) throws IOException {  

                // 文件名 get方式提交 乱码   

                this.filename = new String(filename.getBytes("ISO-8859-1"), "utf-8");  }

                public String execute() throws Exception {   

                System.out.println("下载:" + filename);  

                // 文件下载 结果集 是一个流  

                return SUCCESS;  

         }

      // 提供 下载文件 流  

       // 因为StreamResult中 protected String inputName = "inputStream";  

      public InputStream getInputStream() throws IOException {

                // 下载文件输入流   

               File file = new File(ServletActionContext.getServletContext()     .getRealPath("/download") + "/" + filename);  

              return new FileInputStream(file);

     }

        // 根据下载文件名动态获得 MIME文件类型  

       public String getContentType() {  

            // 读取tomcat/conf/web.xml  

           return ServletActionContext.getServletContext().getMimeType(filename);

           }

       // 下载附件名 ${filename}  

      public String getFilename() throws IOException {   

            // 附件名乱码 问题 (IE和其它浏览器 : URL编码 , 火狐: Base64编码)

            String agent = ServletActionContext.getRequest() .getHeader("user-agent");   

           return encodeDownloadFilename(filename, agent);  

      }

 

      public String encodeDownloadFilename(String filename, String agent)    throws IOException {

           if (agent.contains("Firefox")) {

           // 火狐浏览器    

               filename = "=?UTF-8?B?" + new BASE64Encoder().encode(filename.getBytes("utf-8")) + "?=";  

           }else {

              // IE及其他浏览器  

              filename = URLEncoder.encode(filename, "utf-8");  

          }  

         return filename;

       }

}

 

jsp的写法:

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<a href="${pageContext.request.contextPath }/download.action?filename=MIME协议简介.txt">MIME协议</a>
<a href="${pageContext.request.contextPath }/download.action?filename=Struts2上传下载.ppt">Struts2上传下载.ppt</a>
<a href="${pageContext.request.contextPath }/download.action?filename=老男孩.mp3">老男孩.mp3</a>
</body>
</html>

 

XML的写法:

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE struts SYSTEM "http://struts.apache.org/dtds/struts-2.0.dtd" PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"> -<struts>

<constant value="true" name="struts.devMode"/>

<!-- 文件上传总大小 -->

<constant value="20000000" name="struts.multipart.maxSize"/>

<!-- 开启ognl 静态方法调用 -->

<constant value="true" name="struts.ognl.allowStaticMethodAccess"/>

<!-- 修改默认主题样式 -->

<constant value="simple" name="struts.ui.theme"/>-

<package name="basicstruts2" extends="struts-default">-

<action name="index"><result>/index.jsp</result></action>

<!-- 文件下载 -->

-<action name="download" class="cn.itcast.struts2.demo2.DownloadAction">-

<result type="stream">

<!-- 使用默认流 名称 inputStream, 设置两个头 -->

<!-- 应该根据文件名 动态获得 MIME类型 -->

<!-- 在Action 提供 getContentType方法 -->

<param name="contentType">${contentType}</param>

<!-- 解析下载附件名 问题-->

<param name="contentDisposition">attachment;filename=${filename}</param>

</result>

</action>

</package>

</struts>

 

 

 

推荐阅读