Sturts2文件下载—中文乱码处理

//后台处理类

package com.action.action;

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

import com.opensymphony.xwork2.ActionSupport;
 
public class DownLoadAction extends ActionSupport {  
 
 private static final long serialVersionUID = -2570279127996753260L;

 private String inputPath;  
 
    private String filename;  
 
    public String getInputPath() {  
        return inputPath;  
    }  
 
    public void setInputPath(String inputPath) {  
        this.inputPath = inputPath;  
    }  
 
    public String getFilename() {  
     try {
            return new String(filename.getBytes(), "ISO-8859-1");
     } catch (Exception e) {
           e.printStackTrace();
           return filename;
    }

    }  
 
    public void setFilename(String filename) {  
        this.filename =filename;

    }

    
    public InputStream getInputStream() throws Exception {
      inputPath=URLDecoder.decode(inputPath,"UTF-8");    
   InputStream input=ServletActionContext.getServletContext().getResourceAsStream(inputPath);   //解析服务器资源
     return input;   
    }  
 
    public String download() throws Exception {  
     System.err.println("错误");
     try {
   
  } catch (Exception e) {
   e.printStackTrace();
  }
        return SUCCESS;  
    }  

配置文件:

  <action name="download" class="com.action.action.DownLoadAction"   method="download">  
            <result name="success" type="stream" >  
                <param name="contentType">application/octet-stream;charset=ISO8859-1 </param>  
                <param name="inputName">inputStream</param>
                <param name="bufferSize">4096</param>  
                <param name="contentDisposition">attachment;filename="${filename}"  </param>
            </result>  
        </action> 

前台页面:

<s:url id="url" action='download' namespace='/struts2' includeParams="none">
      <s:param name="inputPath">/file/utils.rar</s:param> <s:param name="filename">utils.rar
         </s:param>  
 </s:url>
  <s:a href="%{url}">下载utils.rar</s:a>   
 //其中">/file/utils.rar  file为服务器的文件夹

详细参见:http://www.blogjava.net/xcp/archive/2009/10/30/downloadList.html

附件为上传的demo。

 

你可能感兴趣的:(java,html,.net)