日期:2014-05-20  浏览次数:20743 次

我的这段代码为什么中文会乱码 ?



public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

response.setContentType("text/html");
request.setCharacterEncoding("gbk");
response.setContentType("text/html;charset=gbk");
response.setHeader("Cache-Control", "no-cache,no-store");
response.setHeader("Expires", "Thu, 01 Dec 1994 16:00:00 GMT");
PrintWriter out = response.getWriter();

String filename = request.getParameter("filename");

URL urlfile;
BufferedReader in;
String content = "";
String inputLine;
try {
urlfile = new URL(filename);
InputStreamReader ss = new InputStreamReader(urlfile.openStream());
System.out.println("code="+ss.getEncoding());
in = new BufferedReader(new InputStreamReader(urlfile.openStream()));
inputLine =new String(in.readLine());
while (inputLine != null) {
content += inputLine;
inputLine = in.readLine();
}
out.println(content);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

}

------解决方案--------------------
在你的Tomcat安装文件夹里的conf目录下有个server.xml,找到这一行
<Connector
port="8080" maxHttpHeaderSize="8192"
maxThreads="150" minSpareThreads="25" maxSpareThreads="75"
enableLookups="false" redirectPort="8443" acceptCount="100"
connectionTimeout="20000" disableUploadTimeout="true" URIEncoding="GBK"/>
看看这个URIEncoding设置