JSP页面下载文件,文件名乱码问题。
点击下载,弹出框的文件名没有问题,但是打开就变乱码了。还好,里面的内容正常。我的代码:<%response.reset();StringATTACHMENTSTOREPA...
点击下载,弹出框的文件名没有问题,但是打开就变乱码了。还好,里面的内容正常。
我的代码:<%response.reset();
String ATTACHMENTSTOREPATH = File.separator + "local"
+ File.separator + "uploads" + File.separator + "wf"
+ File.separator + "cpi";
nt docId = 0;
int revNo = -1;
int attachmentNo = 0;
String store = null;
String filename = null;
String storageName = null;
try {
docId = Integer.parseInt(request.getParameter("docId"));
revNo = Integer.parseInt(request.getParameter("revNo"));
attachmentNo = Integer.parseInt(request.getParameter("attachmentNo"));
store = request.getParameter("store");
filename = new String(request.getParameter("filename").getBytes("ISO-8859-1"),"UTF-8");
storageName = request.getParameter("storageName");
} catch (NumberFormatException nfe) {
}
if (docId >= 0 && revNo > -1 && attachmentNo >= 0) {
String filePath = null;
filePath = ATTACHMENTSTOREPATH + File.separator + storageName;
filename = java.net.URLEncoder.encode(filename, "UTF-8")
.replace("+", "%20");
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition","attachment;filename=" + filename);
BufferedOutputStream bos = null;
BufferedInputStream bis = null;
try {
bos = new BufferedOutputStream(response.getOutputStream());
bis = new BufferedInputStream(new FileInputStream(filePath));
byte[] buffer = new byte[1024];
int n = -1;
while ((n = bis.read(buffer)) > -1) {
bos.write(buffer, 0, n);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (bis != null)
bis.close();
if (bos != null)
bos.close();
bis = null;
bos = null;
}
}
%>
我看了别人写的情况,基本上都是雷同。转载一个叫【古埃及法老】的代码。里面有一个什么:
CourseDetailBusiness.transfer(URLDecoder.decode(fileName,"GBK"),"GBK","ISO-8859-1");用意在转换字符集。
不知道这个方法实现了什么?
我改写的是:new String (java.net.URLDecoder.decode(fileName,"UTF-8").getBytes("UTF-8"),"ISO-8859-1");
但是失败了。求高人指教。 展开
我的代码:<%response.reset();
String ATTACHMENTSTOREPATH = File.separator + "local"
+ File.separator + "uploads" + File.separator + "wf"
+ File.separator + "cpi";
nt docId = 0;
int revNo = -1;
int attachmentNo = 0;
String store = null;
String filename = null;
String storageName = null;
try {
docId = Integer.parseInt(request.getParameter("docId"));
revNo = Integer.parseInt(request.getParameter("revNo"));
attachmentNo = Integer.parseInt(request.getParameter("attachmentNo"));
store = request.getParameter("store");
filename = new String(request.getParameter("filename").getBytes("ISO-8859-1"),"UTF-8");
storageName = request.getParameter("storageName");
} catch (NumberFormatException nfe) {
}
if (docId >= 0 && revNo > -1 && attachmentNo >= 0) {
String filePath = null;
filePath = ATTACHMENTSTOREPATH + File.separator + storageName;
filename = java.net.URLEncoder.encode(filename, "UTF-8")
.replace("+", "%20");
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition","attachment;filename=" + filename);
BufferedOutputStream bos = null;
BufferedInputStream bis = null;
try {
bos = new BufferedOutputStream(response.getOutputStream());
bis = new BufferedInputStream(new FileInputStream(filePath));
byte[] buffer = new byte[1024];
int n = -1;
while ((n = bis.read(buffer)) > -1) {
bos.write(buffer, 0, n);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (bis != null)
bis.close();
if (bos != null)
bos.close();
bis = null;
bos = null;
}
}
%>
我看了别人写的情况,基本上都是雷同。转载一个叫【古埃及法老】的代码。里面有一个什么:
CourseDetailBusiness.transfer(URLDecoder.decode(fileName,"GBK"),"GBK","ISO-8859-1");用意在转换字符集。
不知道这个方法实现了什么?
我改写的是:new String (java.net.URLDecoder.decode(fileName,"UTF-8").getBytes("UTF-8"),"ISO-8859-1");
但是失败了。求高人指教。 展开
3个回答
展开全部
你用Tomcat做JSP Container么?
如果是的话,请试着在 Server.xml <Connector 8080节点 添加URIEncoding="UTF-8"属性
还有就是浏览器 设置中设置充许以utf-8对地址栏编码。
如果是的话,请试着在 Server.xml <Connector 8080节点 添加URIEncoding="UTF-8"属性
还有就是浏览器 设置中设置充许以utf-8对地址栏编码。
追问
都设置成UTF-8了,IE7+JBOSS+JSF,弹出框上面的文件名是正确的,就是直接点打开就乱码了。继续跟进。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
打开名字乱码是正常,那是ie的临时文件名,保存不会就行
追问
有道理,但是还想人性化一点,打开后有用户可能就直接保存的,如果文件名是乱码,总会有些遗憾,想优化一下。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询