flex httpservice与java通讯的中文乱码解决。

来源:互联网 发布:免费手机qq机器人软件 编辑:程序博客网 时间:2024/05/23 15:44

利用url方式传中文参数,需要进行url编码才行,比较麻烦。

利用form方式传中文参数,不需要进行编码,比较简单。只需要注意两点即可:

1、flex默认编码是utf-8,因此java后台设置对request的解码也为utf-8,即可获得中文,不用管tomcat中间服务器的编码是什么。

2、flex中httpservice的send方式不同,url方式为dbsvc1.request.type="cmd";  form方式为:dbsvc1.send(参数对象...),flex自动会把object对象转为form形式进行提交,具体如下:

                    //dbsvc1.request.type="cmd";

                    //dbsvc1.request.sql=_sql;
                    var oo:Object=new Object();  //!!!必须以form形式提交,否则url方式中文无法正确接收,需要编码
                    oo.type="cmd";
                    oo.sql=_sql;

                    dbsvc1.send(oo);  //flex默认编码是utf-8,只要java后台设置request的解码,即可获得中文,不用管tomcat中间服务器的编码是什么。


java后台:

    public void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        request.setCharacterEncoding("UTF-8");  //设置request解码为utf-8,flex前台默认也是utf-8
        Tdb db=new Tdb();
        db.init_dbcfg(request);
        String format=request.getParameter("format")==null?"":request.getParameter("format").toString();
        String type=request.getParameter("type")==null?"":request.getParameter("type").toString();
      。。。。。

        response.setCharacterEncoding("UTF-8");
        response.getWriter().print(data); 



原创粉丝点击