论坛首页 Java企业应用论坛

IE7中附件链接名称过长导致乱码

浏览 2384 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2009-02-16   最后修改:2009-02-18
使用IE7浏览器,打开附件链接时,超过15个汉字会出现9D%这样的乱码文件名,而且下载后的文件无法打开。
如下代码当stepFile_names[i]长度超过15个汉字时在IE6是正常的,在IE7下会出现9D%乱码,且附件无法打开:
<a href=’<%=request.getContextPath()%>/<%=stepFile_paths[i]%>’ target=’_tab’><%=stepFile_names[i]%></a>

对此问题,由于无法缩减文件名称,故采用输出流办法解决。
即将上面代码进行判断
<%
for ( int i = 0; i < stepFile_paths.length; i++ )
{
String fileName = URLEncoder.encode(stepFile_names[i].substring(0, stepFile_names[i].lastIndexOf(".")), "UTF-8");
if (fileName.length() > 150) 
{
%> 
	<a href="<%=request.getContextPath() %>/producetask/download/Download.jsp?fileUrl=<%=stepFile_paths[i] %>&fileName=<%=stepFile_names[i] %>" target="_tab"><%=stepFile_names[i]%></a>
<%
}
else
{
%>
	<a href='<%=request.getContextPath() %>/<%=stepFile_paths[i].replaceAll("/frameweb/|/newframeweb/","/")%>' target="_tab"><%=stepFile_names[i]%></a>
<%
}

从代码上可以看出,当文件名长度超出限制时,通过Download.jsp进行处理(由于UTF-8编码的汉字使用9个字节进行存储,而15*9<150<16*9,故使用150进行判断)
在Download.jsp中,通过将附件读取至输出流中,返回至客户端,以达到解决问题的目的。代码如下:
	OutputStream output=response.getOutputStream();
	response.reset();
	response.setContentType(Download.getContentType(fileName.substring(fileName.lastIndexOf(".") + 1)) + ";charset=GBK");
	String s = fileName.substring(0, fileName.lastIndexOf("."));
	String name = new String(s.getBytes("GBK"), "ISO8859_1") + fileName.substring(fileName.lastIndexOf("."));
	response.addHeader("Content-Disposition", "inline;filename=" + name);
	Download.copyFile(rootPath + fileUrl, output);

copyFile方法主要是将rootPath+fileUrl路径的文件内容读取至output输出流中。
至此,该问题解决,但以上解决办法会增加内存负担,因为文件内容在客户端打开而未下载时,其一直保存在内存中。考虑该问题出现几率较小,且未超出限定长度时不会采用该处理方式。
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics