在struts1中做了个文件下载的功能,结果中文英文下载下来后都是乱码,求各位大虾给诊断诊断public ActionForward downLoad(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
DocumentForm documentForm = (DocumentForm) form;
String docid = request.getParameter("docid");
// if the document's id is null , return failure
if (docid == null || "".equals(docid))
return mapping.findForward("failure"); SecurityContext securityContext = SecurityContextHolder.getContext();
DocumentManager mgr = (DocumentManager) getBean("documentManager");
Document document = mgr.getDocument(docid);
String remotePath = document.getDocLocation();
String fileName = remotePath.substring(remotePath.lastIndexOf("/") + 1);
String downloadDir = servlet.getServletContext().getRealPath("/resources") + "/" + request.getRemoteUser() + "/download/"; File dirPath = new File(downloadDir); if (!dirPath.exists()) {
dirPath.mkdirs();
}
try {
URL url = new URL(remotePath);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.connect();
DataInputStream in = new DataInputStream(connection.getInputStream());
DataOutputStream outa = new DataOutputStream(new FileOutputStream(downloadDir + fileName));
byte[] buffer = new byte[4096];
int count = 0;
while ((count = in.read(buffer)) > 0) {
outa.write(buffer, 0, count);
}
outa.flush();
outa.close();
in.close();
} catch (Exception e) { } File file = new File(downloadDir + fileName); if (file.exists()) {
try { BufferedInputStream bis = null;
BufferedOutputStream bos = null;
OutputStream fos = null;
InputStream fis = null; String filepath = file.getPath();
fis = new FileInputStream(file);
bis = new BufferedInputStream(fis);
fos = response.getOutputStream();
bos = new BufferedOutputStream(fos); response.setHeader("Content-disposition",
"attachment;filename=" +fileName);
response.setCharacterEncoding("UTF-8");
response.setContentType("UTF-8");
response.setContentType("Application/x-download;charset=UTF-8"); int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = bis.read(buffer, 0, 8192)) != -1) {
bos.write(buffer, 0, bytesRead);
}
bos.flush();
fis.close();
bis.close();
fos.close();
bos.close();
} catch (Exception e) {
System.out.println("when download file,there is an error!");
return mapping.findForward("failure");
}
} else {
}
return null;
}
HttpServletRequest request, HttpServletResponse response)
throws Exception {
DocumentForm documentForm = (DocumentForm) form;
String docid = request.getParameter("docid");
// if the document's id is null , return failure
if (docid == null || "".equals(docid))
return mapping.findForward("failure"); SecurityContext securityContext = SecurityContextHolder.getContext();
DocumentManager mgr = (DocumentManager) getBean("documentManager");
Document document = mgr.getDocument(docid);
String remotePath = document.getDocLocation();
String fileName = remotePath.substring(remotePath.lastIndexOf("/") + 1);
String downloadDir = servlet.getServletContext().getRealPath("/resources") + "/" + request.getRemoteUser() + "/download/"; File dirPath = new File(downloadDir); if (!dirPath.exists()) {
dirPath.mkdirs();
}
try {
URL url = new URL(remotePath);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.connect();
DataInputStream in = new DataInputStream(connection.getInputStream());
DataOutputStream outa = new DataOutputStream(new FileOutputStream(downloadDir + fileName));
byte[] buffer = new byte[4096];
int count = 0;
while ((count = in.read(buffer)) > 0) {
outa.write(buffer, 0, count);
}
outa.flush();
outa.close();
in.close();
} catch (Exception e) { } File file = new File(downloadDir + fileName); if (file.exists()) {
try { BufferedInputStream bis = null;
BufferedOutputStream bos = null;
OutputStream fos = null;
InputStream fis = null; String filepath = file.getPath();
fis = new FileInputStream(file);
bis = new BufferedInputStream(fis);
fos = response.getOutputStream();
bos = new BufferedOutputStream(fos); response.setHeader("Content-disposition",
"attachment;filename=" +fileName);
response.setCharacterEncoding("UTF-8");
response.setContentType("UTF-8");
response.setContentType("Application/x-download;charset=UTF-8"); int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = bis.read(buffer, 0, 8192)) != -1) {
bos.write(buffer, 0, bytesRead);
}
bos.flush();
fis.close();
bis.close();
fos.close();
bos.close();
} catch (Exception e) {
System.out.println("when download file,there is an error!");
return mapping.findForward("failure");
}
} else {
}
return null;
}
或则在你写的response的地方加上这句也行,可以试试.