我request ,response 的encoding 都设置了,但是都没有用,还是乱码,很多乱码,其他的应该都没有问题的
网上查到,要调用这个方法
setHeaderEncoding方法
由于浏览器在提交FORM表单时,会将普通表单中填写的文本内容传递给服务器,对于文件上传字段,除了传递原始的文件内容外,还要传递其文件路径名等信息,如后面的图1.3所示。不管FORM表单采用的是“application/x-www-form-urlencoded”编码,还是“multipart/form-data”编码,它们仅仅是将各个FORM表单字段元素内容组织到一起的一种格式,而这些内容又是由某种字符集编码来表示的。关于浏览器采用何种字符集来编码FORM表单字段中的内容,请参看笔者编著的《深入体验java Web开发内幕——核心基础》一书中的第6.9.2的讲解,“multipart/form-data”类型的表单为表单字段内容选择字符集编码的原理和方式与“application/x-www-form-urlencoded”类型的表单是相同的。FORM表单中填写的文本内容和文件上传字段中的文件路径名在内存中就是它们的某种字符集编码的字节数组形式,Apache文件上传组件在读取这些内容时,必须知道它们所采用的字符集编码,才能将它们转换成正确的字符文本返回。
对于浏览器上传给WEB服务器的各个表单字段的描述头内容,Apache文件上传组件都需要将它们转换成字符串形式返回,setHeaderEncoding 方法用于设置转换时所使用的字符集编码,其原理与笔者编著的《深入体验java Web开发内幕——核心基础》一书中的第6.9.4节讲解的ServletRequest.setCharacterEncoding方法相同。setHeaderEncoding 方法的完整语法定义如下:
public void setHeaderEncoding(String encoding)
其中,encoding参数用于指定将各个表单字段的描述头内容转换成字符串时所使用的字符集编码。
注意:如果读者在使用Apache文件上传组件时遇到了中文字符的乱码问题,一般都是没有正确调用setHeaderEncoding方法的原因。但是这个方法应该怎么调用呢???下面是我的SERVLET
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
public class UpLoad2 extends HttpServlet { private String uploadPath = "D:\\upload\\";
private String tempPath = "D:\\uploadtmp\\";
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { doPost(request,response);
} public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//setHeaderEncoding("utf-8");
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charser=utf-8");
PrintWriter out = response.getWriter();
DiskFileItemFactory factory = new DiskFileItemFactory();
// maximum size that will be stored in memory
factory.setSizeThreshold(4096);
// the location for saving data that is larger than getSizeThreshold()
factory.setRepository(new File(tempPath));
ServletFileUpload upload = new ServletFileUpload(factory);
// maximum size before a FileUploadException will be thrown
upload.setSizeMax(1000000); List fileItems;
try {
fileItems = upload.parseRequest(request);
// assume we know there are two files. The first file is a small
// text file, the second is unknown and is written to a file on
// the server
Iterator i = fileItems.iterator();
while(i.hasNext()){
FileItem fi = (FileItem)i.next();
if(!fi.isFormField()){
//get filename on the client
String name = fi.getString();
long size = fi.getSize();
if((name==null||name.equals(""))&&size==0){
continue;
}
name = name.substring(name.lastIndexOf("\\")+1,name.length());
System.out.println(name);
// write the file
fi.write(new File(uploadPath + name));
}
else{
String field = ((FileItem) i).getFieldName();//表单域名 String value = ((FileItem) i).getString("GBK");
}
}
}catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}}
请高手帮帮忙
网上查到,要调用这个方法
setHeaderEncoding方法
由于浏览器在提交FORM表单时,会将普通表单中填写的文本内容传递给服务器,对于文件上传字段,除了传递原始的文件内容外,还要传递其文件路径名等信息,如后面的图1.3所示。不管FORM表单采用的是“application/x-www-form-urlencoded”编码,还是“multipart/form-data”编码,它们仅仅是将各个FORM表单字段元素内容组织到一起的一种格式,而这些内容又是由某种字符集编码来表示的。关于浏览器采用何种字符集来编码FORM表单字段中的内容,请参看笔者编著的《深入体验java Web开发内幕——核心基础》一书中的第6.9.2的讲解,“multipart/form-data”类型的表单为表单字段内容选择字符集编码的原理和方式与“application/x-www-form-urlencoded”类型的表单是相同的。FORM表单中填写的文本内容和文件上传字段中的文件路径名在内存中就是它们的某种字符集编码的字节数组形式,Apache文件上传组件在读取这些内容时,必须知道它们所采用的字符集编码,才能将它们转换成正确的字符文本返回。
对于浏览器上传给WEB服务器的各个表单字段的描述头内容,Apache文件上传组件都需要将它们转换成字符串形式返回,setHeaderEncoding 方法用于设置转换时所使用的字符集编码,其原理与笔者编著的《深入体验java Web开发内幕——核心基础》一书中的第6.9.4节讲解的ServletRequest.setCharacterEncoding方法相同。setHeaderEncoding 方法的完整语法定义如下:
public void setHeaderEncoding(String encoding)
其中,encoding参数用于指定将各个表单字段的描述头内容转换成字符串时所使用的字符集编码。
注意:如果读者在使用Apache文件上传组件时遇到了中文字符的乱码问题,一般都是没有正确调用setHeaderEncoding方法的原因。但是这个方法应该怎么调用呢???下面是我的SERVLET
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Iterator;
import java.util.List;import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
public class UpLoad2 extends HttpServlet { private String uploadPath = "D:\\upload\\";
private String tempPath = "D:\\uploadtmp\\";
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { doPost(request,response);
} public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//setHeaderEncoding("utf-8");
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charser=utf-8");
PrintWriter out = response.getWriter();
DiskFileItemFactory factory = new DiskFileItemFactory();
// maximum size that will be stored in memory
factory.setSizeThreshold(4096);
// the location for saving data that is larger than getSizeThreshold()
factory.setRepository(new File(tempPath));
ServletFileUpload upload = new ServletFileUpload(factory);
// maximum size before a FileUploadException will be thrown
upload.setSizeMax(1000000); List fileItems;
try {
fileItems = upload.parseRequest(request);
// assume we know there are two files. The first file is a small
// text file, the second is unknown and is written to a file on
// the server
Iterator i = fileItems.iterator();
while(i.hasNext()){
FileItem fi = (FileItem)i.next();
if(!fi.isFormField()){
//get filename on the client
String name = fi.getString();
long size = fi.getSize();
if((name==null||name.equals(""))&&size==0){
continue;
}
name = name.substring(name.lastIndexOf("\\")+1,name.length());
System.out.println(name);
// write the file
fi.write(new File(uploadPath + name));
}
else{
String field = ((FileItem) i).getFieldName();//表单域名 String value = ((FileItem) i).getString("GBK");
}
}
}catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}}
请高手帮帮忙
还试过:ServletFileUpload upload = new ServletFileUpload(factory);
upload.setCharacterEncoding("utf-8");
网上是这么说的,而且ServletFileUload的确有这个方法。
还有转换字符编码类型:String name = fi.getString();
byte[] b=new name.getBytes("ISO-8809-1") ;
name=new String(b,"gb2312");
可是都没有成功,LZ找到解决方法了么?