html上传文件http header有乱码解决方法

html上传文件http header有乱码
本帖最后由 abc45628 于 2015-03-30 11:20:43 编辑
服务器用的是Tomcat8,
上传页面是HTML,没有用JSP代码

<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<form action="fileUpload" method="post" enctype="multipart/form-data">
        <input type="hidden" name="isMultiUpload" value="false" >
选择文件:<input type="file" name="file" />
<input type="submit" value="上传" name="upload" />
</form>
</body>


servlet处理代码

protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletExceptionIOException {
req.setCharacterEncoding("utf-8");
resp.setContentType("text/html;charset=utf-8");
Part part = req.getPart("file");
String filename = getFilename(part);
PrintWriter out = resp.getWriter();
out.print("文件名:" + filename);
String a=req.getParameter("te");
out.print("文件名:" + a);
part.write(filename);
out.close();
}

private String getFilename(Part part) throws UnsupportedEncodingException {
String header = part.getHeader("Content-Disposition");
String pathname = header.substring(header.indexOf("filename=\"")+10,
header.lastIndexOf("\""));
String filename=pathname.substring(pathname.lastIndexOf("\\")+1);
return filename;
}


Tomcat服务器的server.xml

<Connector connectionTimeout="20000" port="8080" protocol="HTTP/1.1"
redirectPort="8443" useBodyEncodingForURI="true"/>

下面的乱码原来是“新建文本文档”,


Content-Disposition: form-data; name="file"; filename="C:\鏂板缓鏂囨湰鏂囨。.txt"
Content-Type: text/plain

为什么http header用的是GBK编码?通过useBodyEncodingForURI=true,而且我的页面charset=utf-8,这样的话上传数据不是应该以utf-8上传的吗?
补充一句,最终显示没有错,我只是纠结http header的编码。
------解决思路----------------------
pageEncoding:设置JSP源文件和响应正文中的字符集编码。
contentType:设置JSP源文件和响应正文的字符集编码及MIME类型。
 如果pageEncoding属性存在,那么JSP页面的字符编码方式就由pageEncoding决定,  
 否则就由contentType属性中的charset决定,如果charset也不存在,JSP页面的字符编码方式就采用 默认的ISO-8859-1。