jsp实现文件下载与中文文件名乱码问题解决
来源:互联网 发布:白马啸西风知乎 编辑:程序博客网 时间:2024/05/01 03:12
问题如下:
之前,写过一个Download.jsp文件,可以解决下载文件乱码问题(诸如:DOC,XSL文件等等).
后来发现,遇到中文名的文件的时候,文件下载将会报错~~~~
今天,通过改写原Download.jsp文件已经彻底解决了这个问题~
现在,把一整套的文件上传下载的方法给贴出来~~~以便大家借鉴!~!~!~!~!
作者:古埃及法老
download.jsp文件
---------------------------------------------------------
java.io.BufferedInputStream bis=null;
java.io.BufferedOutputStream bos=null;
try{
String filename=request.getParameter("filename");
filename=new String(filename.getBytes("iso8859-1"),"gb2312");
response.setContentType("application/x-msdownload");
response.setHeader("Content-disposition","attachment; filename="+new String(filename.getBytes("gb2312"),"iso8859-1"));
bis =new java.io.BufferedInputStream(new java.io.FileInputStream(config.getServletContext().getRealPath("files/" + filename)));
bos=new java.io.BufferedOutputStream(response.getOutputStream());
byte[] buff = new byte[2048];
int bytesRead;
while(-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
bos.write(buff,0,bytesRead);
}
}
catch(Exception e){
e.printStackTrace();
}
finally {
if (bis != null)bis.close();
if (bos != null)bos.close();
}
%>
注意,关键就是setHeader里的filename需要重新编码,格式是ISO-8859-1就OK了
以下是我自己项目中用到的代码片断,供参考:
list.jsp: 显示附件名称的页面
<td height="25" class="tdcor">附 件 </td>
<td colspan="3" height=50>
<%
if (null != publish.getAttatchFilename() &&
publish.getAttatchFilename().length() > 0) {
%>
<a href="publish_do.jsp?method=download&fileName=
<%=URLEncoder.encode(publish.getAttatchFilename(),"GBK")%>">
<%=URLDecoder.decode(publish.getAttatchFilename(),"GBK")%></a>
<%
}
%>
</td>
</tr>
download.jsp:下载页面
String fileName = request.getParameter("fileName");
File file = new File(Constants.PUBLISH_FILE_PATH + "/" + URLDecoder.decode(fileName,"GBK"));
response.reset();
response.setContentType("application/octet-stream; charset=GBK");
response.addHeader("Content-Disposition", "attachment; filename=" + CourseDetailBusiness.transfer(URLDecoder.decode(fileName,"GBK"),"GBK","ISO-8859-1"));
response.setContentLength((int) file.length());
byte[] buffer = new byte[4096];
BufferedOutputStream output = null;
BufferedInputStream input = null;
// 写缓冲区:
try {
output = new BufferedOutputStream(response.getOutputStream());
input = new BufferedInputStream(new FileInputStream(file));
int n = (-1);
while ((n = input.read(buffer, 0, 4096)) > -1) {
output.write(buffer, 0, n);
}
response.flushBuffer();
}
catch (Exception e) {
} // maybe user cancelled download
finally {
if (input != null) input.close();
if (output != null) output.close();
}
说明:
1。文件名在数据库中保存的编码为URLEncode
2.在list.jsp显示的时候多了一次encode,不知为什么,不encode一次还不行,实际上是第二次编码了
- jsp实现文件下载与中文文件名乱码问题解决
- jsp实现文件下载与中文文件名乱码问题解决
- jsp实现文件下载与中文文件名乱码问题解决
- jsp实现文件下载与中文文件名乱码问题解决
- jsp实现文件下载与中文文件名乱码问题解决
- jsp实现文件下载与中文文件名乱码问题解决
- jsp实现文件下载与中文文件名乱码问题解决
- 实现文件下载与中文文件名乱码问题解决
- jsp实现文件下载与中文文件名乱码问题(转)
- IE浏览器下载文件中文文件名乱码问题解决
- 文件下载中文文件名乱码
- jsp下载中文文件名文件
- jsp实现中文文件名下载
- JSP、Struts2下载中文文件名乱码问题
- JSP、Struts2下载中文文件名乱码问题
- JSP、Struts2下载中文文件名乱码问题
- JSP、Struts2下载中文文件名乱码问题
- 用response对象实现文件下载,解决中文文件名乱码。
- ntfs+oracle+2003+asp.net 网站发布
- 说‘标准’话‘垄断’
- 12.1.5 网络监控
- Java解析XML文件的四种方法
- 深入理解iPhone屏幕双缓冲技术
- jsp实现文件下载与中文文件名乱码问题解决
- MSDN收藏
- 12.1.6 综合监控工具——top
- EVC入门之一: 实现远程调试的最佳硬件途径
- JAVA进阶:一个简单Thread缓冲池的实现
- 12.2 syslog日志
- 12.2.1 syslog简介
- ASP.NET文件上传下载
- oracle 表空间 表权限 用户