码迷,mamicode.com
首页 > 其他好文 > 详细

下载的时候如果文件名是中文就变成zip.zip

时间:2014-06-12 10:26:19      阅读:223      评论:0      收藏:0      [点我收藏+]

标签:style   class   blog   code   java   http   

struts2

 

/WEB-INF/web.xml

bubuko.com,布布扣
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
      <display-name>blk</display-name>

    <filter>
        <filter-name>struts2</filter-name>
        <filter-class>org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter</filter-class>
    </filter>

    <filter-mapping>
        <filter-name>struts2</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
    </welcome-file-list>
    
</web-app>
bubuko.com,布布扣

 

/WEB-INF/classes/struts.xml

bubuko.com,布布扣
<default-action-ref name="download"/>
        <global-results>
            <result name="sql">/exceptionHandle/exception.jsp</result>
            <result name="root">/exceptionHandle/exception.jsp</result>
        </global-results>

        <global-exception-mappings>
            <exception-mapping exception="java.sql.SQLException" result="sql"/>
            <exception-mapping exception="java.lang.Exception" result="root"/>
        </global-exception-mappings>

<action name="download" class="down.FileDownloadAction">
            <param name="inputPath">\down\images\文件.zip</param>
            <result name="success" type="stream">
                <param name="contentType">application/zip</param>
                <param name="inputName">targetFile</param>
                <param name="contentDisposition">filename="文件.zip"</param>
                <param name="bufferSize">4096</param>
            </result>
        </action>
bubuko.com,布布扣

 

/down/strutsDown.html

bubuko.com,布布扣
<html>
<head>
    <title>Struts2的文件下载</title>
</head>

<body>
    <h1>Struts2的文件下载</h1>
        下载Struts2的Logo:<a href="download.action">下载文件</a> 

</body>
</html>
bubuko.com,布布扣

 

down.FileDownloadAction

 

bubuko.com,布布扣
package down;

import java.io.InputStream;
import org.apache.struts2.ServletActionContext;
import com.opensymphony.xwork2.Action;


public class FileDownloadAction implements Action 
{

    private String inputPath;
    public void setInputPath(String value)
    {
        inputPath = value;
    }

    /*
     下载用的Action应该返回一个InputStream实例,
     该方法对应在result里的inputName属性值为targetFile
    */
    public InputStream getTargetFile() throws Exception 
    {
        return ServletActionContext.getServletContext().getResourceAsStream(inputPath);
    }

    public String execute() throws Exception
    {
        return SUCCESS;
    }

}
bubuko.com,布布扣

 

 

 

下载的时候如果文件名是中文就变成zip.zip,布布扣,bubuko.com

下载的时候如果文件名是中文就变成zip.zip

标签:style   class   blog   code   java   http   

原文地址:http://www.cnblogs.com/qrlozte/p/3782378.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!