[已解决]SmartUpload乱码的问题
1、上传的文件名如果是中文,那么上传后就是乱码
<!-- upload.html --> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <form action=".\jsp\smartupload_demo01.jsp" method="post" enctype="multipart/form-data"> 请选择要上传的文件:<input type="file" name="pic"> <input type="submit" value="上传"> </form> </body> </html> <!-- upload.jsp --> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ page import="com.jspsmart.upload.SmartUpload" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <% String rootPath = application.getRealPath("/"); // System.out.println(rootPath); // @Debug SmartUpload smart = new SmartUpload() ; smart.initialize(pageContext) ; // 初始化上传操作 smart.upload() ; // 上传准备 smart.save(rootPath + "\\upload") ; // 文件保存 %> </body> <h2>上传成功,<a href="..\smartupload_demo01.html">返回</a></h2> </html>
2、如果是如下这种情况,获取的String parameter "name"也是乱码(输入中文的话)
upload.jsp
<%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%> <%@ page import="com.jspsmart.upload.SmartUpload" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Insert title here</title> </head> <body> <% request.setCharacterEncoding("UTF-8") ; %> <% String rootPath = application.getRealPath("/"); // System.out.println(rootPath); // @Debug SmartUpload smart = new SmartUpload() ; smart.initialize(pageContext) ; // 初始化上传操作 smart.upload() ; // 上传准备 String name = smart.getRequest().getParameter("uname"); // System.out.println(name); // @Debug SmartUpload smart.save(rootPath + "\\upload") ; // 文件保存 %> <h2>上传成功,<a href="..\smartupload_demo02.html">返回</a></h2> <h2>姓名:<%=name %></h2><br> <h2>request.getParameter("uname"): <%=request.getParameter("uname") %></h2> </body> </html>
upload.html
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <form action=".\jsp\smartupload_demo02.jsp" method="post" enctype="multipart/form-data"> 姓名:<input type="text" name="uname"><br> 照片:<input type="file" name="pic"><br> <input type="submit" value="上传"> <input type="reset" value="重置"> </form> </body> </html>
因为使用SmartUpload需要对form使用enctype,导致数据按照纯二进制的方式提交,因此会出现编码问题,而SmartUpload本身的API又没有提供对编码的解决方案。
3、解决方案
使用如下代码,完美解决:
SmartUpload smart = new SmartUpload() ; smart.setCharset("UTF-8"); // 或者你需要的编码
搜了一下
一种是修改SmartUpload的源码,让SmartUpload内部解决编码的转换问题,自己编译了再用
第二种就是选择另一款上传工具,比如fileupload
另外,采用JS或者别的方式来传递数据