java下载中文及空格变加号解决方案

来源:互联网 发布:中国债券市场数据 编辑:程序博客网 时间:2024/04/29 15:07

1.只是做了中文处理

前台jsp页面代码如下:

buildpre.append("<td><a href=\"down.action?fname="+preDeatil.getPafilename().replaceAll(" ", "&nbsp;")+"&fileName="+preDeatil.getPafileurlpath().replaceAll(" ", "&nbsp;")+"\">"+preDeatil.getPafilepath().replaceAll(" ", "&nbsp;")+"</a></td>\r\n");

后台java文件处理如下:

fileName = java.net.URLEncoder.encode(new String(fileName.getBytes("ISO-8859-1"), "UTF-8"), "UTF-8");

 

2.两者都做处理

 

前台jsp页面代码如下:

buildpre.append("<td><a onclick=\"downloadAttach(this)\"  href=\"javascript:void(0)\" fname="+preDeatil.getPafilename().replaceAll(" ", "&nbsp;")+" fileName="+preDeatil.getPafileurlpath().replaceAll(" ", "&nbsp;")+"\">"+preDeatil.getPafilepath().replaceAll(" ", "&nbsp;")+"</a></td>\r\n");

前如js代码如下:

//下载附件
 function downloadAttach(paramHref)
 {
     var attachName = paramHref.fname;
     var realAttachName = paramHref.fileName;
     /*document.getElementById("attachName").value=attachName;
     document.getElementById("realAttachName").value=realAttachName;
     document.getElementById("attachLoad").submit();*/
     //down.action?fname="+preDeatil.getPafilename().replaceAll(" ", "&nbsp;")+"&fileName="+preDeatil.getPafileurlpath().replaceAll(" ", "&nbsp;")+"\"
     window.location.href = "down.action?fname="+encodeURIComponent(attachName)+"&fileName="+encodeURIComponent(realAttachName);
     return false;
 }

后台java文件处理如下:

fileName = fileName.replace('+', ' ');// 将编码成+号的空格替换回来
        fileName = new String(fileName.getBytes("ISO-8859-1"), "UTF-8");

原创粉丝点击