jspsmartupload上傳文件名中文亂碼和超鏈接傳遞路徑參數encodeURIComponent加解碼

最近在使用jspsmartupload上傳文件功能,最後的問題是文件名如果是奇數箇中文,則最後一個現實亂碼,因爲jspsmartupload不支持中文,很頭疼,各種方式都解決不了,最後,修改了jspsmartupload源碼,可以使用無亂碼,保證頁面各處是utf-8.

jsp

<div class="importKnowledge">
   <form name="excelform" id="excelform" action="./servlet/UploadPdf"  method="post" enctype="multipart/form-data">
<input type="file" name="file" size="50" /><br/>
*課件作者:<input type="text" name="author" size="24" value="111"/><br/><br/>
*本人姓名:<input type="text" name="uploadAuthor" size="24" value="222" />
                   <button type="submit">提交</button>
</form>
</div>

servlet

public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
response.setContentType("text/html;charset=UTF-8");

//上傳文件的jar包裏面
SmartUpload su = new SmartUpload();
   PageContext pageContext = JspFactory.getDefaultFactory().getPageContext(this,request,response,"",true,8192,true); 
   su.initialize(pageContext);
   su.setMaxFileSize(1024*1024*10);     
   //設置文件大小
   su.setTotalMaxFileSize(1024*1024*100);  
try {
su.upload();

String authorTemp = su.getRequest().getParameter("author");
String uploadAuthorTemp = su.getRequest().getParameter("uploadAuthor");
authorTemp = (authorTemp==null?"":authorTemp);
uploadAuthorTemp = (uploadAuthorTemp==null?"":uploadAuthorTemp);
System.out.println("author:"+authorTemp+",uploadAuthorTemp:"+uploadAuthorTemp);

//服務器所在路徑
   String filePath=this.getServletConfig().getServletContext().getRealPath("/");
   //文件保存路徑
String url = filePath+"admin\\uploadfile\\";
//如果路徑不存在,則創建2個路徑
java.io.File f = new java.io.File(url);
if(!f.exists()){
if(f.mkdirs()){
System.out.println(url+"文件夾創建成功!");
}
}
System.out.println("file path:"+url);
su.save(url);
//jxl獲取文件名,// 2003版本
String fileNameTemp = su.getFiles().getFile(0).getFileName();
System.out.println(",file name:"+fileNameTemp);
int insertResult = insert2DB(fileNameTemp,url,authorTemp,uploadAuthorTemp,510);
if(1==insertResult){
System.out.println("insert success!");
}else{
System.out.println("insert error!");
}
} catch (SmartUploadException e) {
e.printStackTrace();
}
}

另外,還遇到了個問題,在jsp頁面中,使用超鏈接,並需要傳遞文件路徑如,xxx.jsp?path=xxx,特殊字符有問題,必須encodeURIComponent加碼,也可以其他形式加碼,

jsp

"<a target='_blank' href='<%=request.getContextPath()%>/admin/sysadmin/readPDF.jsp?filePath="+encodeURIComponent(filePath)+"'>"+exam[i].name+"</a>"

java

 //對encodeURIComponent做解碼,否則中文亂碼!
String strPdfPath = new String(filePath.getBytes("ISO8859-1"),"utf-8"); 


OK!



發佈了56 篇原創文章 · 獲贊 5 · 訪問量 16萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章