在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;
}
解决方案 »
- 这个是什么编码???
- 为什么使用HibernateTemplate中的save方法没有输出SQL语句
- 用超链接去下载word文件出问题
- java打开word编辑器,有点复杂,谁能提供个思路?
- 用javascript在网页上显示时间的问题
- 有谁知道,为什么hibernate的sql语句,同一个操作在控制台会打印两遍。
- web Services 与JMS(消息服务)用作消息数据传输各有那些优点
- struts ActionForm设计的缺陷
- 如何解决EJB中主键的自增问题??在线等待..................
- 怎么样删除一个文件!????????
- hibernate many to many查询
- ssh整合问题!!!
或则在你写的response的地方加上这句也行,可以试试.