jee中文名图片+tomcat ==> 中文乱码的另类处理(未成功)

一、java

1.一、tomcat 7 默认是 ISO-8859-1编码(单字节编码)浏览器

1.二、若是使用这个编码的话,(我的猜想)tomcat没法管理相关的中文名的文件(包括 中文名的图片 等)tomcat

1.三、∴ 须要设置 tomcat的编码为 能容纳中文的 编码方式。服务器

 

二、jsp

尝试倒腾的测试代码:ide

思路:原本,utf-8页面(jsp) 传来的jpg文件名是 utf-8编码的,传到服务器后找不到相应的图片。因而,想经过 过滤器,转换 编码,使得tomcat可以找到 服务器上相应的图片。函数

过滤器的 doFilter函数:测试

 1     @Override
 2     public void doFilter(ServletRequest _request, ServletResponse _response,
 3             FilterChain _chain) throws IOException, ServletException
 4     {
 5         // ZC: 打印全部的传入参数
 6     /*    Enumeration<String> enum1 = _request.getParameterNames();
 7         while (enum1.hasMoreElements())
 8         {
 9             String strName = enum1.nextElement().toString();
10             String strValue = _request.getParameter(strName);
11             System.out.println(strName+"--------------"+strValue);
12         }
13         System.out.println("");
14     */
15     /*    
16         String strCeShi = "测试";
17         byte[] bytes = strCeShi.getBytes("gbk");
18         System.out.println("GBK :");
19         for (int i=0; i<bytes.length; i++)
20             System.out.println(Integer.toString((int)bytes[i] & 0xFF, 16));
21         System.out.println("");
22         
23         bytes = strCeShi.getBytes("utf-8");
24         System.out.println("UTF-8 :");
25         for (int i=0; i<bytes.length; i++)
26             System.out.println(Integer.toString((int)bytes[i] & 0xFF, 16));
27         System.out.println("");
28         
29         bytes = strCeShi.getBytes("iso-8859-1");
30         System.out.println("ISO-8859-1 :");
31         for (int i=0; i<bytes.length; i++)
32             System.out.println(Integer.toString((int)bytes[i] & 0xFF, 16));
33         System.out.println("");
34         
35         strCeShi = "測試";
36         bytes = strCeShi.getBytes("big5");
37         System.out.println("BIG5 :");
38         for (int i=0; i<bytes.length; i++)
39             System.out.println(Integer.toString((int)bytes[i] & 0xFF, 16));
40         System.out.println("");
41         
42         // 字节流(地址从低到高)
43         //    GBK :             b2 e2 ca d4
44         //    UTF-8 :         e6 b5 8b e8 af 95
45         //    ISO-8859-1 :     3f 3f            (ZC: 这个明显不对了哇)
46         //    big5(測試) :        b4 fa b8 d5
47     //*/    
48         
49         String str1 = ((HttpServletRequest)_request).getRequestURI();
50         System.out.println("str1 : "+str1);
51         String str2 = URLDecoder.decode(str1, "utf-8"); //  unescape() decodeURI() decodeURIComponent() 
52         System.out.println("str2 : "+str2);
53         
54         // ZC: 下面测试发现,当浏览器请求页面时,能够经过下面的方式转到别的页面(或者直接是图片都行)
55         // ZC: 请求某个图片时,也能够跳转到别的图片
56         String strUTF8in = "/JpgNameWithChinese/%E6%B5%8B%E8%AF%95.jpg";
57         //String strUTF8in = "/JpgNameWithChinese/";
58         if (0 == strUTF8in.compareToIgnoreCase(str1))
59         {
60             System.out.println("== 0");
61             
62             //String strCeShi = "测试";
63             //String str4 = URLEncoder.encode(strCeShi, "utf-8")+".jpg";
64             //*
65             byte[] bytesUTF8 = new byte[6];
66             bytesUTF8[0] = (byte)0xE6;
67             bytesUTF8[1] = (byte)0xB5;
68             bytesUTF8[2] = (byte)0x8B;
69             bytesUTF8[3] = (byte)0xE8;
70             bytesUTF8[4] = (byte)0xAF;
71             bytesUTF8[5] = (byte)0x95;
72             
73             String strUTF8 = new String(bytesUTF8, "utf-8");
74             strUTF8 += ".jpg";
75             //*/
76             /*
77             byte[] bytesGBK = new byte[4];
78             bytesGBK[0] = (byte)0xb2;
79             bytesGBK[1] = (byte)0xe2;
80             bytesGBK[2] = (byte)0xca;
81             bytesGBK[3] = (byte)0xd4;
82             
83             String strGBK = new String(bytesGBK, "GBK");
84             strGBK += ".jpg";
85             //*/
86             
87             //_request.getRequestDispatcher("a.jsp").forward(_request, _response);
88             //_request.getRequestDispatcher("CeShi.jpg").forward(_request, _response);
89             //_request.getRequestDispatcher(strUTF8).forward(_request, _response);
90             //_request.getRequestDispatcher(strGBK).forward(_request, _response);
91             
92             _request.getRequestDispatcher("测试.jpg").forward(_request, _response);
93         }
94         else
95             _chain.doFilter(_request, _response);//放行
96     }

 

通过的折腾:编码

  一、对 "((HttpServletRequest)_request).getRequestURI()" 作各类编码转换的折腾spa

  二、将 tomcat的 编码方式 设置为 "gbk"/"utf-8"

 

获得的不理想的结论:

  一、无论怎么弄,想要 tomcat可以找到 中文的图片,至少 tomcat得是 可以容纳中文的编码方式,不然就是扯淡...

  二、因为上一条结论,那还作什么编码转换啊...直接 网页页面 和 tomcat 都设置成 utf-8 就好了

  三、暂时(20151207)的理解是这样...之后看,还有别的理解不...

 

测试代码保存于:百度云 CodeSkill33 --> 所有文件 > java_测试_code_zc --> JpgNameWithChinese__Work_20151207_1612.rar