原文:http://www.blogjava.net/duduli/archive/2008/11/11/239845.html
昨天让这个乱码问题弄了很久,一大早就开始想要怎么解决才好。
很简单上传页面,jsp上传页面代码
1
<
form
action
="/struts2/UploadServlet"
method
="post"
enctype
="multipart/form-data"
>
2 用户名: < input type ="text" name ="username" >< br >
3 密 码: < input type ="password" name ="password" >< br >
4 文件1: < input type ="file" name ="file1" >< br >
5 文件2: < input type ="file" name ="file2" >< br >
6 < input type ="submit" value ="提交" >
7 </ form >
下面是UploadServlet代码
2 用户名: < input type ="text" name ="username" >< br >
3 密 码: < input type ="password" name ="password" >< br >
4 文件1: < input type ="file" name ="file1" >< br >
5 文件2: < input type ="file" name ="file2" >< br >
6 < input type ="submit" value ="提交" >
7 </ form >
1
@SuppressWarnings("serial")
2 public class UploadServlet extends HttpServlet {
3
4 @SuppressWarnings({ "unchecked", "deprecation" })
5 public void doPost(HttpServletRequest request, HttpServletResponse response)
6 throws ServletException, IOException {
7// 设置工厂
8 DiskFileItemFactory factory = new DiskFileItemFactory();
9 String path = request.getRealPath("/upload");
10// 设置文件存储位置
11 factory.setRepository(new File(path));
12// 设置大小,如果文件小于设置大小的话,放入内存中,如果大于的话则放入磁盘中
13 factory.setSizeThreshold(1024*1024);
14
15 ServletFileUpload upload = new ServletFileUpload(factory);
16// 这里就是中文文件名处理的代码,其实只有一行,serheaderencoding就可以了
17 upload.setHeaderEncoding("utf-8");
18 /*String enCoding = request.getCharacterEncoding();
19 if(enCoding != null){
20 upload.setHeaderEncoding(enCoding);
21 }*/
22
23 try {
24 List<FileItem> list = upload.parseRequest(request);
25 for(FileItem item : list){
26// 判断是不是上传的文件,如果不是得到值,并设置到request域中
27// 这里的item.getfieldname是得到上传页面上的input上的name
28 if(item.isFormField()){
29 String name = item.getFieldName();
30 String value =item.getString("utf-8");
31 System.out.println(name);
32 System.out.println(value);
33 request.setAttribute(name, value);
34 }
35// 如果是上传的文件,则取出文件名,
36 else{
37 String name = item.getFieldName();
38 String value = item.getName();
39 System.out.println(name);
40 System.out.println(value);
41// 得到不要地址的文件名,不同的浏览器传递的参数不同,有的直接传递文件名,而又的把文件地址一起传递过来
42// 使用substring方法可以统一得到文件名而不得到文件位置
43 int start = value.lastIndexOf("\\");
44 String fileName = value.substring(start + 1);
45 request.setAttribute(name, fileName);
46// 写文件到path目录,文件名问filename
47 item.write(new File(path,fileName));
48 }
49 }
50 }
51
52 catch (FileUploadException e) {
53 e.printStackTrace();
54 } catch (Exception e) {
55 e.printStackTrace();
56 }
57// 跳转到显示结果页面
58 request.getRequestDispatcher("upload/result2.jsp").forward(request, response);
59 }
60
61}
用EL表达式显示输出
2 public class UploadServlet extends HttpServlet {
3
4 @SuppressWarnings({ "unchecked", "deprecation" })
5 public void doPost(HttpServletRequest request, HttpServletResponse response)
6 throws ServletException, IOException {
7// 设置工厂
8 DiskFileItemFactory factory = new DiskFileItemFactory();
9 String path = request.getRealPath("/upload");
10// 设置文件存储位置
11 factory.setRepository(new File(path));
12// 设置大小,如果文件小于设置大小的话,放入内存中,如果大于的话则放入磁盘中
13 factory.setSizeThreshold(1024*1024);
14
15 ServletFileUpload upload = new ServletFileUpload(factory);
16// 这里就是中文文件名处理的代码,其实只有一行,serheaderencoding就可以了
17 upload.setHeaderEncoding("utf-8");
18 /*String enCoding = request.getCharacterEncoding();
19 if(enCoding != null){
20 upload.setHeaderEncoding(enCoding);
21 }*/
22
23 try {
24 List<FileItem> list = upload.parseRequest(request);
25 for(FileItem item : list){
26// 判断是不是上传的文件,如果不是得到值,并设置到request域中
27// 这里的item.getfieldname是得到上传页面上的input上的name
28 if(item.isFormField()){
29 String name = item.getFieldName();
30 String value =item.getString("utf-8");
31 System.out.println(name);
32 System.out.println(value);
33 request.setAttribute(name, value);
34 }
35// 如果是上传的文件,则取出文件名,
36 else{
37 String name = item.getFieldName();
38 String value = item.getName();
39 System.out.println(name);
40 System.out.println(value);
41// 得到不要地址的文件名,不同的浏览器传递的参数不同,有的直接传递文件名,而又的把文件地址一起传递过来
42// 使用substring方法可以统一得到文件名而不得到文件位置
43 int start = value.lastIndexOf("\\");
44 String fileName = value.substring(start + 1);
45 request.setAttribute(name, fileName);
46// 写文件到path目录,文件名问filename
47 item.write(new File(path,fileName));
48 }
49 }
50 }
51
52 catch (FileUploadException e) {
53 e.printStackTrace();
54 } catch (Exception e) {
55 e.printStackTrace();
56 }
57// 跳转到显示结果页面
58 request.getRequestDispatcher("upload/result2.jsp").forward(request, response);
59 }
60
61}
1
<
body
>
2 用户名:${requestScope.username } < br >
3 密 码:${requestScope.password } < br >
4 文件1 :${requestScope.file1 } < br >
5 文件2 :${requestScope.file2 } < br >
6 </ body >
2 用户名:${requestScope.username } < br >
3 密 码:${requestScope.password } < br >
4 文件1 :${requestScope.file1 } < br >
5 文件2 :${requestScope.file2 } < br >
6 </ body >
其实很简单的设置就可以把中文件上传,并正确显示正确的中文文件名。
在网上找了一点资料,但是都写得很少,没有把完整的写出来。
所以把它写出来,让大家少走点弯路。