简体   繁体   English

Tomcat Servlet FileUpload和UTF-8

[英]Tomcat Servlet FileUpload and UTF-8

I am using commons FileUpload to get client headers. 我正在使用Commons FileUpload获取客户端头。 I am trying to use the header Strings to write txt content like: 我正在尝试使用标头字符串写txt内容,例如:

StringA; StringA; StringB; StringB; "ciryllic word" “西里尔词”

The server localization is ru_RU.utf-8 the client is cp1251... The result is I always get on my client (desktop app) 服务器本地化为ru_RU.utf-8,客户端为cp1251 ...结果是我总是在客户端上(桌面应用程序)

StringA; StringA; StringB; StringB; ????? ??????

instead of cyrillic characters in my server txt files lines. 而不是服务器txt文件行中的西里尔字符。 If I open txt with my IE 8 and watch the encoding the cyrillic content can be readable with utf-8 encoding only :( So my question is what should me do to make my servlet write ru_RU.utf-8 acceptable strings. I am testing it on windows so I need to know it for sure 如果我使用IE 8打开txt并观看编码,则只能使用utf-8编码读取西里尔字母的内容:(因此,我的问题是我应该怎么做才能使servlet编写ru_RU.utf-8可接受的字符串。我正在测试它在Windows上,所以我需要确定地知道它

Please help me to understand 请帮我理解

Any useful comment is appreciated 任何有用的评论表示赞赏

ServletOutputStream out =response.getOutputStream();
out.write((YOUrfileasstring).getBytes("UTF-8"));

I might be misunderstanding the question, but to answer this part - 我可能会误解这个问题,但要回答这一部分-

So my question is what should me do to make my servlet write ru_RU.utf-8 acceptable strings. 所以我的问题是我应该怎么做才能使我的servlet写ru_RU.utf-8可接受的字符串。

You should use either of two methods provided by the HttpServletResponse class. 您应该使用HttpServletResponse类提供的两种方法之一。 Those are: 那些是:

setContentType(), eg setContentType(),例如

    response.setContentType("text/html; charset=UTF-8");

or, 要么,

setCharacterEncoding(), eg setCharacterEncoding(),例如

    response.setCharacterEncoding("utf-8");

There are other examples for those methods and information on when to use them all over the place. 这些方法还有其他示例,以及何时使用这些方法的信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM