有请高手出场! JSP下载文件时的文件名编码问题.
费话不多说,看代码:1.这是调用外代码:
<a href="util/downLoadFileServlet?path=文件路径&name=显示的文件名">
2.下面是Servlet:
response.reset();
response.setContentType("binary/octet-stream");
String path = request.getSession().getServletContext().getRealPath("");
path += "\\";
String file = request.getParameter("path");
String filedownload = path + file;
String filedisplay = new String(s.getBytes("iso-8859-1"),"utf-8");
filedisplay = URLEncoder.encode(filedisplay, "gb2312");
response.setHeader("Content-Disposition", "attachment;filename=" + filedisplay);
OutputStream outp = null;
FileInputStream in = null;
try
{
outp = response.getOutputStream();
in = new FileInputStream(filedownload);
byte[] b = new byte[1024];
int i = 0;
while ((i = in.read(b)) > 0)
{
outp.write(b, 0, i);
}
//outp.flush();
}
catch (Exception e)
{
System.out.println("Error!");
e.printStackTrace();
}
finally
{
if (in != null)
{
in.close();
in = null;
}
if (outp != null)
{
outp.close();
outp = null;
}
}
3.下载时主要的问题:
直接点击链接:弹出世界之窗的下载(图1),名字显示正常,点"下载"后文件下载成功,可是名字变为(图2)名字出错
如果在(图1)的文件名处手动输入"一二三.bmp"或COPY一下框里的内容再粘贴,下载后显示就正常了.
用右键目标另存为下载名字则变为(图3)
4.请高手帮帮忙,我只有20分,谢谢大家了!!