Uploading a file, the file name is garbled. (netty file server)

This is indeed a good way.

 accept-charset is a rarely used form attribute, and it is also a very convenient solution to use it to implement form submission in pages with different encodings. documented here.

Background of the problem:

what is it, that is, ie submits garbled characters, Firefox, and Google are all good, but ie is messed up. . . , especially when the form submits the file. why. . F12 debugging ie (ie9) found that ie was not encoded and submitted directly with Chinese characters.
So the solution is to specify the encoding and tell the browser what encoding to use for parsing before submitting.
That is document.charset='utf-8' .
The following is written by someone else I found online. However, I took a detour, because I submitted it through ajax. After changing the code according to the following, it has no effect. Until I accidentally saw a question saying: the form sumbmit button is not garbled, but the ajax is garbled. . . . I had an epiphany.
So I put document.charset='utf-8' in
$("#id").ajaxSubmitfun({
beforeSumbmit: function() {
document.charset='utf-8';
}

})

jquery-form.js;
Sure enough! ! !
No need to differentiate between browsers.
You can get it directly in the background.
Of course, the safest way to do it in the background is new String(str.getBytes("ISO-8859-1"),"UTF-8");  UTF-8 is required in the above document.
Note that I was garbled when I submitted the file to get the file name.
Fortunately, when I took a detour, my thinking did not change, otherwise it would be troublesome.

Another idea is,
request.setcharacterencoding. This will be more troublesome, because different browsers, such as IE and non-IE, have different encodings, so you may have to judge and set them multiple times.
And the Httprequset encapsulated by netty does not seem to be well set up.
Here's someone else's answer:
Solution:

Of course, it's possible to use ICONV or the MB extension to convert the encoding yourself, but that's not what we want. 
A less common attribute is introduced in W3: accept-charset, which can be used to complete our needs. 
Write the following code in the GBK-encoded page: 
<form method="post" action="..." accept-charset="utf-8"> ... </form> 

Such code can be browsed normally in Firefox, etc. There is no problem under the browser, but when it comes to IE, the perverted browser is not good, we have to hack it by means of a bit of a stream: 
<form method="post" action="..." accept-charset=" utf-8" onsubmit="document.charset='utf-8';"> ... </form> 

The browser will do the rest.

This is indeed a good way.

 accept-charset 一个很少用到的表单属性,利用它实现在不同编码的页面里实现表单的提交也是一种很方便的解决方法。记录在此。

问题背景:

就是啥呢,就是,ie提交乱码,火狐,谷歌,都是好的,但是,ie乱了。。。,尤其是表单提交文件的时候。为啥。。f12调试ie(ie9)发现,ie没有进行编码直接用汉字提交了。
所以解决的思路是指定编码,提交前告诉浏览器用什么编码解析。
也就是document.charset='utf-8' 。
下面的是网上找的别人写的。但是,走了点弯路,因为我是ajax提交的,按照下面说的改代码后,一直没效果。直到无意中看到一个提问说:form sumbmit按钮提交不乱码,ajax乱码了。。。。我顿悟了。
于是我把document.charset='utf-8' 放到了
$("#id").ajaxSubmitfun({
beforeSumbmit:function(){
document.charset='utf-8' ;
}

})

jquery-form.js;
果然好了!!!
不用区分浏览器。
后台直接获取就可以了。
当然,后台最保险的做法是 new String(str.getBytes("ISO-8859-1"),"UTF-8"); UTF-8要于上面document.charset设置的保持一致
注意 我是提交文件 获取文件名的时候乱码。
好在,走弯路的时候思路没有变,否则就要麻烦了。

另外一个思路是,
request.setcharacterencoding。这个就要麻烦一些了,因为不同的浏览器,比如ie与非ie提交过来的编码就不一样,所以可能要判断一下,多次设置。
而且netty封装的Httprequset似乎不是很好设置。
下面是别人的答案:
解决方案:

当然了,可以自己使用ICONV或者MB扩展来转换编码,但这不是我们要的。 
在W3里介绍了一个不太常见的属性:accept-charset,用它可以完成我们的需求。 
在GBK编码的页面里编写如下代码: 
<form method="post" action="..." accept-charset="utf-8"> ... </form> 

如此的代码在Firefox等正常的浏览器下没有任何问题,但是遇到IE这个变态浏览器就不灵光了,我们还得用点不入流的手段Hack一下: 
<form method="post" action="..." accept-charset="utf-8" onsubmit="document.charset='utf-8';"> ... </form> 

剩下的工作浏览器会搞定。

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325443955&siteId=291194637