站内搜索: 请输入搜索关键词

当前页面: 开发资料首页Struts 专题Struts实现文件下载中文乱码解决方案

Struts实现文件下载中文乱码解决方案

摘要: Struts实现文件下载中文乱码解决方案 页面一开始进去action,action负责把file文件夹下的所有文件读入一个ArrayList中 Action代码如下: ArrayList list =...
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", "attachment; 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", "attachment; filename=" +new String(docName.getBytes("UTF-8"),"GBK")); 
如果不这样做你将要下载的文件名是乱码。
说明就到这里,如果有问题或要eclipse下的工程代码可以留下你的地址,我会尽可能的给你发邮件。
我的邮件aeoluswqq@163.com


↑返回目录
前一篇: struts开发实践—分页的实现
后一篇: struts国际化轻松尝试