出现在我的WEB项目中,我把问题化小就是这样子。
String a="中文";
String b="aaaa";
String a = new String(a.getBytes("ISO-8859-1"),
"UTF-8");
String b=new String(a.getBytes("ISO-8859-1"),
"UTF-8");
其中b来自网页页面获得
String c=a+b;
然后输出结果会产生乱码,我知道编码方式肯定不一样,但是我已经都转成UTF-8编码方式了,为什么还是出现乱码。求解

解决方案 »

  1.   

    package servlet;import java.io.File;
    import java.io.IOException;
    import java.io.PrintWriter;
    import java.io.UnsupportedEncodingException;
    import java.util.Iterator;
    import java.util.List;import javax.servlet.ServletConfig;
    import javax.servlet.ServletContext;
    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.FileUploadException;
    import org.apache.commons.fileupload.disk.DiskFileItemFactory;
    import org.apache.commons.fileupload.servlet.ServletFileUpload;import check.CheckResourceName;import dao.StudentDao;
    import dao.daoImpl.StudentDaoImpl;
    import entity.Source;
    import entity.Student;
    import exceptions.DataAccessException;
    import exceptions.NameExistsException;public class DoPublishSource extends HttpServlet { private String resourcePath;
    private String tempResourcePath; /**
     * Constructor of the object.
     */
    public DoPublishSource() {
    super();
    } /**
     * Destruction of the servlet. <br>
     */
    public void destroy() {
    super.destroy(); // Just puts "destroy" string in log
    // Put your code here
    }
    public void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
    doPost(request, response);
    } public void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException { response.setContentType("text/html;chartset=utf-8");
    request.setCharacterEncoding("UTF-8");
    response.setCharacterEncoding("UTF-8");
    PrintWriter pw = response.getWriter();
    long max_size = 16 * 1024 * 1024;
    DiskFileItemFactory diskFactory = new DiskFileItemFactory();
    diskFactory.setSizeThreshold(4 * 1024);
    diskFactory.setRepository(new File(tempResourcePath)); ServletFileUpload upload = new ServletFileUpload(diskFactory);
    upload.setSizeMax(-1);
    try {
    List fileItems = upload.parseRequest(request);
    Iterator iter = fileItems.iterator();
    while (iter.hasNext()) {
    FileItem item = (FileItem) iter.next();
    if (item.isFormField()) {
    } else {
    String fileName = item.getName();
    int index = fileName.indexOf("\\");
    fileName = fileName.substring(index + 1, fileName.length());
    if (!CheckResourceName.checkResourceNameFormat(fileName)) {
    request.setAttribute("msg", "文件名的长度在20个字符内,请改变文件名");
    request.getRequestDispatcher("/WEB-INF/msg.jsp")
    .forward(request, response);
    return;
    }
    if (CheckResourceName.checkResourceNameExist(fileName)) {
    request.setAttribute("msg", "文件名已经存在,请改变文件名");
    request.getRequestDispatcher("/WEB-INF/msg.jsp")
    .forward(request, response);
    return;
    }
    StudentDao sd = new StudentDaoImpl();
    Source source = new Source();
    source.setSourceName(fileName);
    source.setSid(((Student) request.getSession().getAttribute(
    "student")).getId());
    sd.publishSource(source);
    long fileSize = item.getSize();
    if ("".equals(fileName) && fileSize == 0) {
    System.out.println("文件为空");
    return;
    }
    System.out.println(fileName);
    System.out.println(resourcePath);
    fileName = new String(fileName.getBytes("ISO-8859-1"),
    "utf-8");
    String name=resourcePath + "/" + fileName;
    File resourceFile = new File(name);
    System.out.println(name);
    // System.out.println(resourceFile.toString());
    item.write(resourceFile);
    pw.println("文件保存完毕!");
    pw.println("文件大小为:" + fileSize);
    }
    }
    } catch (FileUploadException e) {
    e.printStackTrace();
    } catch (DataAccessException e) {
    e.printStackTrace();
    request.setAttribute("msg", "数据库访问异常");
    request.getRequestDispatcher("/WEB-INF/msg.jsp").forward(request,
    response);
    return;
    } catch (Exception e) {
    e.printStackTrace();
    } } public void init() throws ServletException {
    // Put your code here
    } @Override
    public void init(ServletConfig config) throws ServletException {
    super.init(config);
    resourcePath = config.getInitParameter("resourcePath");
    tempResourcePath = config.getInitParameter("tempResourcePath");
    ServletContext context = getServletContext();
    resourcePath = context.getRealPath(resourcePath);
    tempResourcePath = context.getRealPath(tempResourcePath);
    try {
    resourcePath = new String(resourcePath.getBytes("ISO-8859-1"),
    "UTF-8");
    tempResourcePath = new String(tempResourcePath
    .getBytes("ISO-8859-1"), "UTF-8");
    } catch (UnsupportedEncodingException e) {
    e.printStackTrace();
    } if (!(new File(resourcePath)).isDirectory()) {
    new File(resourcePath).mkdirs();
    }
    if (!(new File(tempResourcePath)).isDirectory()) {
    new File(tempResourcePath).mkdirs();
    }
    }}
    这是程序
      

  2.   

    file:/D:/software/apache-tomcat-6.0.29-windows-x86/apache-tomcat-6.0.29/webapps/classSystem/WEB-INF/databaseParams.xml-----
    jdbc:mysql://127.0.0.1:3306/classsystem?useUnicode=true&characterEncoding=UTF-8
    jdbc:mysql://127.0.0.1:3306/classsystem?useUnicode=true&characterEncoding=UTF-8
    jdbc:mysql://127.0.0.1:3306/classsystem?useUnicode=true&characterEncoding=UTF-8
    jdbc:mysql://127.0.0.1:3306/classsystem?useUnicode=true&characterEncoding=UTF-8
    jdbc:mysql://127.0.0.1:3306/classsystem?useUnicode=true&characterEncoding=UTF-8
    JavaScript的例子.zip
    D:\software\apache-tomcat-6.0.29-windows-x86\apache-tomcat-6.0.29\webapps\classSystem\resourcePath
    D:\software\apache-tomcat-6.0.29-windows-x86\apache-tomcat-6.0.29\webapps\classSystem\resourcePath/JavaScript???.zip
    java.io.FileNotFoundException: D:\software\apache-tomcat-6.0.29-windows-x86\apache-tomcat-6.0.29\webapps\classSystem\resourcePath\JavaScript???.zip (文件名、目录名或卷标语法不正确。)
    at java.io.FileOutputStream.open(Native Method)
    at java.io.FileOutputStream.<init>(FileOutputStream.java:179)
    at java.io.FileOutputStream.<init>(FileOutputStream.java:131)
    at org.apache.commons.fileupload.disk.DiskFileItem.write(DiskFileItem.java:449)
    at servlet.DoPublishSource.doPost(DoPublishSource.java:140)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:637)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:298)
    at org.apache.coyote.http11.Http11AprProcessor.process(Http11AprProcessor.java:861)
    at org.apache.coyote.http11.Http11AprProtocol$Http11ConnectionHandler.process(Http11AprProtocol.java:579)
    at org.apache.tomcat.util.net.AprEndpoint$Worker.run(AprEndpoint.java:1584)
    at java.lang.Thread.run(Thread.java:619)
      

  3.   

    不知道你在运行的时候,是哪句报错了,也不知道你的项目用的是什么编码,但是有一点肯定,那就是你编码后有乱码。
    你的项目是utf8的编码么?如果是,为什么要对这个编码?String a = new String(a.getBytes("ISO-8859-1"),"UTF-8");
    这个本来就是uft8的编码了啊,如果不是,为什么要都转换成utf8的编码?!
    String b=new String(a.getBytes("ISO-8859-1"),
    "UTF-8");
    你跟踪调试下,看是你转换编码前就是乱码,还是转换后才变成乱码的了...........
      

  4.   

    看下String a="中文";所在文件的编码格式。