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

使用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输出流中。
至此,该问题解决,但以上解决办法会增加内存负担,因为文件内容在客户端打开而未下载时,其一直保存在内存中。考虑该问题出现几率较小,且未超出限定长度时不会采用该处理方式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值