这是servlet中的部分代码
取值的时候汉字的goodsType和goodsName都是乱码  如何解决额?
坐等高人解答..
request.setCharacterEncoding("utf-8"); response.setContentType("text/html;charset=utf-8");
   //接收从添加页面传递过来的参数  传递方式  get  将form表单里面的内容以get方式提交到本页面
 GoodsDTO good=new GoodsDTO();
 GoodsDAO dao=new GoodsDAO();
   PrintWriter out=response.getWriter();
String file=request.getParameter("picture");
try { // 对request对象进行解析 取出 上传时保存的数据
ServletFileUpload fileupload = new ServletFileUpload(
new DiskFileItemFactory());
// 解析 返回一个list list装有文件目录的list
List list = fileupload.parseRequest(request);
// 遍历
Iterator it = list.iterator();
while (it.hasNext()) {
FileItem fi = (FileItem) it.next();// 获得单个文件目录
String str = fi.getFieldName();// 获得从页面传递参数的name 不仅仅是文件的name

if (fi.isFormField()) {
String value = fi.getString();
String name = fi.getFieldName();
System.out.println(name + ":" + value);
} else {// 就是文件
// 获得文件的名字
String name = fi.getName();
System.out.println(name + "==========");
// 截取字符串获取文件的名字
// 获取最后一个“/”的位置
int n = name.lastIndexOf("\\");
// 开始截取
String filename = name.substring(n + 1);
// 将上传的文件写到指定位置
 File f = new File("C:/Users/Administrator/Desktop/shopping/WebRoot/football/" + filename);
 // 开始写
 fi.write(f);
} } } catch (FileUploadException e) {
e.printStackTrace();
} catch (Exception e) {
}finally{
 String goodsname=request.getParameter("goodsname");
   goodsname=new String(goodsname.getBytes("iso-8859-1"),"utf-8");
   String goodsprice=request.getParameter("goodsprice");
   String goodstype=request.getParameter("goodstype");
   goodstype=new String(goodstype.getBytes("iso-8859-1"),"utf-8");
   String amount=request.getParameter("amount");
   //将去到的值全部放在对象里面
  
System.out.println("上传的文件是:" + file);
   good.setGoodsName(goodsname);
   good.setGoodsPrice(goodsprice);
   good.setGoodsType(goodstype);
   int goodsamount=Integer.parseInt(amount);
   good.setGoodsAmount(goodsamount);
   good.setPicture(file);
  
  
   boolean flag=dao.add(good);
   if(flag){
   out.println("<script>");
   out.println("alert('恭喜你添加成功');");
   out.println("window.location.href='ShowServlet'");
   out.println("</script>");
   }

   
}

解决方案 »

  1.   

    页面时啥编码页面UTF-8
    js encodeURI(encodeURI("我"));
      

  2.   

    String goodstype=new String(request.getParameter("goodstype").getBytes("ISO-8859-1"),"GBK");
    把你需要获取的值都这样转一下就ok了
      

  3.   

    补充一句吧jsp页面编码集也变成gbk或者gb2312
      

  4.   

    String goodsname=request.getParameter("goodsname");
     goodsname=new String(goodsname.getBytes("iso-8859-1"),"utf-8");
    这句话和你说那方法一样 已经使用了 取值还是乱码
      

  5.   

    页面全部都是utf-8
    我连工程都转了utf-8还是乱码
      

  6.   

    我的页面编码集都是统一的utf-8
      

  7.   

    直接sevlet设置 request.setCharacterEncoding("gb2312");
     
      

  8.   

    用gbk我一直用的就是gbk,把编码格式都设置成gbk
      

  9.   

    还是这段代码  乱码问题解决了  可是finally{}里面的取值取不到是什么情况额?
      

  10.   

    场合:页面本身有中文的时候
    解决办法:servlet:resp.setContentType("text/html;charset=gbk");
    Jsp: <%@ page contentType="text/html;charset=gb2312"%>
    注意:一定要写在PrintWriter out = resp.getWriter();之前
    场合:解决get方式乱码问题:
    解决办法:修改server.xml URIEncoding="GBK"
    场合:解决post方式提交内容的乱码
    解决办法:request.setCharacterEncoding("GBK");
    注意:一定要写在存取第一个参数之前
    不要调用response.setCharacterEncoding("GBK");
    场合:<jsp:param name="user" value="<%=s%>"/>,url地址包含中文参数
    解决办法:<%request.setCharacterEncoding("GBK");%>
    注意: