Struts实现文件下载中文乱码解决方案
2008-01-05 08:27:41 来源:WEB开发网核心提示: 页面一开始进去action,action负责把file文件夹下的所有文件读入一个ArrayList中Action代码如下:ArrayList list = new ArrayList(); String path=request.getRealPath("/")+"file"
页面一开始进去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进入讨论组讨论。
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进入讨论组讨论。
更多精彩
赞助商链接