1. 程式人生 > >java下載中文及空格變加號解決方案

java下載中文及空格變加號解決方案

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");