1 中文在Servlet页面显示成乱码      response.setContentType("text/html;charset=GB2312"); 
     response.setCharacterEncoding("GB2312"); 
     request.setCharacterEncoding("GB2312"); 这些代码都加上了,也试过换成gbk,utf-8 但都不行,即使直接用out.println输出的中文也是乱码的只能在server.xml 里面设置 URIEncoding="GBK"/> 才能正常显示但是这样设置后,原来能正常显示的jsp页面显示中文时就变成乱码了2 在Servlet里面调用SmartUpload下载文件时出现的异常本来应该是下载文件的,结果变成是下载Servlet本身输出的页面关键代码如下:      //获取jsp页面常用对象
     HttpSession session = request.getSession();
 JspFactory _jspxFactory = null;
       PageContext pageContext = null;
       _jspxFactory = JspFactory.getDefaultFactory();
       pageContext = _jspxFactory.getPageContext(this,request,response,"",true,8192,true); //再将本地内容写到客户端
SmartUpload su = new SmartUpload();
su.initialize(pageContext);
System.out.println("localfile = " + LocalFileName);
su.setContentDisposition(null);
su.downloadFile("F:\\Program Files\\Apache Software Foundation\\Tomcat 6.0\\logs\\stdout_20090707.log");
后面这段代码是我从jsp页面中拷过来的,在jsp页面中,这段代码工作正常

解决方案 »

  1.   

    1.         response.setContentType("text/html;charset=GB2312"); 
             response.setCharacterEncoding("GB2312"); 
             request.setCharacterEncoding("GB2312"); 
    以上需要放在所有对request和response调用的前面。否则没有用。servlet容器默认使用ISO-8859-1的编码,如果不修改server.xml中的配置,那整个应用的编码就是ISO-8859-1(默认情况下的JSP/Servlet的编码就是).修改了Server.xml中的编码配置后,JSP页面中的编码并没有修改,容器和JSP页面的编码方式不同,就会导致乱码的出现。这时,JSP页面可以使用JSP指令来改变页面编码方式:<%@ page pageEncoding="gb2312"%>;Servlet可以使用LZ列出的方法,在response/request没有被使用前执行(其实如果容器的默认编码为GB2312,那request和response的编码方式就是GB2312,这是因为这两个对象是由容器来创建的);2.以前遇到过类似问题。但是需要看到你的SERVLET代码才知道原因。以前的代码中使用servlet来实现AJAX异步时,返回给AJAX的json会被下载,而不是直接给XMLHttpRequest
      

  2.   

    谢谢1F 果然把这段代码提前就行了SERVLET的代码如下,没用到什么高级技术。看了一下日志 居然有异常
    java.lang.IllegalStateException: getWriter() has already been called for this response
    在执行downloadFile函数过程中发生的
    package test;import java.util.*;
    import java.io.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    import com.jspsmart.upload.SmartUpload;
    import javax.servlet.jsp.*;public class CFileUpDown_servlet extends HttpServlet
    {
    public CFileUpDown_servlet() 
    {
    super();
    }

    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;charset=GB2312"); 
         response.setCharacterEncoding("GB2312"); 
         request.setCharacterEncoding("GB2312"); 
         
         //获取jsp页面常用对象
         HttpSession session = request.getSession();
     JspFactory _jspxFactory = null;
           PageContext pageContext = null;
           _jspxFactory = JspFactory.getDefaultFactory();
           pageContext = _jspxFactory.getPageContext(this, request, response, "", true, 8192, true);
           
       try
           {
         PrintWriter out = response.getWriter();
         
         out.println("<html><head><title>");
         out.println("This is my first Servlet");
         out.println("</title></head><body>");
         out.println("<h1>请稍候...</h1>"); //获取文件动作和文件内容
    String FileAction = (String) request.getParameter("FileAction");
    String FileContent = (String) request.getParameter("FileContent"); 

    out.println("<BR>first<BR>");
    //区分动作进行处理
    if (FileAction.equals("SaveAs"))
    {
    //如果是另存为
    try
    {
    //先将文件内容写到本地
    /////////////////////////////////////关键代码
    //再将本地内容写到客户端
    out.println("</body></html>");
    SmartUpload su = new SmartUpload();
    su.initialize(pageContext);
    su.setContentDisposition(null);
    su.downloadFile("F:\\Program Files\\Apache Software Foundation\\Tomcat 6.0\\logs\\stdout_20090707.log");
    /*
    //调用下载文件jsp页面
    RequestDispatcher rd = request.getRequestDispatcher("do_download.jsp?downloadfilename=" + LocalFileName);
    rd.forward(request, response);
    */
    }
    catch (Exception e)
    {
    e.printStackTrace();
    }


    } out.flush();
    out.close();
        
         } 
        finally
        {
         _jspxFactory.releasePageContext(pageContext);
    }
       }
       
       public static void main(String[] argv)
       {
           System.out.println("CFileUpDown");
       }
    }
      

  3.   

    发现是out造成的,如果没有PrintWriter out = response.getWriter();就没问题有没有办法在调用SmartUpload对象之前先临时关闭out 在调用后恢复呢?
      

  4.   

    我把out.println全移到smartupload调用后面
    但发现一个郁闷的情况 点击另存为后虽然顺利弹出保存对话框 但是却停留在原来的页面 不能转到Servlet输出页面
      

  5.   

     最根本的解决方法就是加上过滤器,彻底
    类文件package base;import java.io.IOException;import javax.servlet.Filter;
    import javax.servlet.FilterChain;
    import javax.servlet.FilterConfig;
    import javax.servlet.ServletException;
    import javax.servlet.ServletRequest;
    import javax.servlet.ServletResponse;public class EncodingFilter implements Filter {
    protected FilterConfig config; protected String Encoding = null; public void init(FilterConfig config) throws ServletException { this.config = config;
    this.Encoding = config.getInitParameter("Encoding"); } public void doFilter(ServletRequest request, ServletResponse response,
    FilterChain chain) throws IOException, ServletException { if (request.getCharacterEncoding() == null) {
    if (Encoding != null) {
    request.setCharacterEncoding(Encoding);
    response.setCharacterEncoding(Encoding);
    }
    }
    chain.doFilter(request,response);
    }
    public void destroy() {}
    }
    web.xml <filter>
    <filter-name>Filter</filter-name>
    <filter-class>
    base.EncodingFilter<!-- 过滤器类 -->//你的包名称和类名称
    </filter-class>
    <init-param>
    <param-name>Encoding</param-name>
    <param-value>GBK</param-value>
    </init-param>
    </filter>
    <filter-mapping>
    <filter-name>Filter</filter-name>
    <url-pattern>/*</url-pattern>
    </filter-mapping>
      

  6.   

    下载好象和浏览器的版本有关,MZ的版本会出现这个问题,IE高版本不会出现这个问题,如果没办法的话,可以改一下你的servlet结构。不用out
      

  7.   

    我好像记得SmartUpload对支持中文好像是有问题的。
      

  8.   


    不用out用什么代替输出网页呢?