Struts實現文件下載中文亂碼解決方案

   頁面一開始進去action,action負責把file文件夾下的所有文件讀入一個ArrayList中
Action代碼如下:
ArrayList list = new ArrayList();
     String path=request.getRealPath("/")+"file";
     String FullPath;
       //System.out.println(path);
     myDir=new File(path);
     list.clear();
     contents=myDir.listFiles();
     for(int i=0;i<contents.length;i++){
      FullPath=contents[i].getName();
      list.add(FullPath);
      //System.out.println(FullPath);
     }
 request.setAttribute("list",list);
        ActionForward forward=new ActionForward("/download.jsp"); 
        return forward;
然後進入download.jsp中,這個頁面主要負責把所有文件顯示,並提供下載連接,代碼如下:
<%@ page language="java" contentType="text/html;charset=GBK" import="java.util.ArrayList"%>

<head>
<style>
</style>
</head>
<body>
<%ArrayList list=(ArrayList)request.getAttribute("list");
  for(int i=0;i<list.size();i++)
  {
  String a=java.net.URLEncoder.encode((String)list.get(i));

   out.print("<a href=./loaded.do?name="+a+">"+list.get(i)+"</a><br>");
  }
  %>
</body>
</html>
注意,下劃線畫中的代碼的作用,就是解決問題的所在。
接下來可以直接傳入到loadedaction中,也可以通過一個form,我演示的是通過一個form
Form代碼如下
package org.aeolus.struts.form;

import javax.servlet.http.HttpServletRequest;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;

public class LoadForm extends ActionForm {
    /*
     * Generated Methods
     */
    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}
接下來就是action的代碼
  LoadForm doc=(LoadForm)form;
String docName = new String(doc.getName().getBytes("8859_1"));
          File f;
          if(docName!=""){
          String docFullPath=request.getRealPath("/"); 
              f = new File(docFullPath+"file"+docName);
             response.reset();        
             response.setContentType("application/x-msdownload;charset=GBK"); 
             System.out.print(response.getContentType());
             response.setCharacterEncoding("UTF-8");
            docName=java.net.URLEncoder.encode(docName,"UTF-8");
             response.setHeader("Content-Disposition", "p_w_upload; filename=" +new String(docName.getBytes("UTF-8"),"GBK")); 
             BufferedInputStream br = new BufferedInputStream(new FileInputStream(f));
             byte[] buf = new byte[1024];
             int len = 0;
              OutputStream out = response.getOutputStream();
              while((len = br.read(buf)) >0)
              out.write(buf,0,len);
              out.close();
              response.wait();
              ActionForward forward=new ActionForward("/download.jsp"); 
              
              return forward;
          }
          return null;  
注意,下劃線畫中的代碼的作用,就是解決問題的所在。
說明一下
response.setCharacterEncoding("UTF-8");
            docName=java.net.URLEncoder.encode(docName,"UTF-8");
             response.setHeader("Content-Disposition", "p_w_upload; filename=" +new String(docName.getBytes("UTF-8"),"GBK")); 
如果不這樣做你將要下載的文件名是亂碼。
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章