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!



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值