zoukankan      html  css  js  c++  java
  • 下载的时候如果文件名是中文就变成zip.zip

    struts2

    /WEB-INF/web.xml

    <?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>

    /WEB-INF/classes/struts.xml

    <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">downimages文件.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>

    /down/strutsDown.html

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

    down.FileDownloadAction

    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;
        }
    
    }
  • 相关阅读:
    【spring-boot】spring-boot 事物管理——之注解@Transactional
    【spring-boot】Spring-boot学习-helloWorld
    【Maven】failOnMissingWebXml
    【java基础领域】高内聚、低耦合理解
    Pascal's Triangle
    Remove Nth Node From End of List
    Valid Palindrome
    Longest Common Prefix
    Roman to Integer
    Palindrome Number
  • 原文地址:https://www.cnblogs.com/qrlozte/p/3782378.html
Copyright © 2011-2022 走看看