java通过流的方式获取参数乱码及编码问题

最近在写一个电商平台的项目,原本任务已经完成,但是淘宝做妖,限制数据输出,只能通过奇门的方式获取订单数据,故又多了一个模块任务。

通过奇门推数据的时候,你要获得推过来的数据,传统的方式是通过参数名获取其值,但是有个问题,这个时候你不知道奇门传过来的参数名是什么,而且数据是以xml的格式传过来的,故传统参数形式不行,想了很久,考虑用输入流的方式来获取数据;

主要思路:

1,获取输入流

2,将输入流转为字符串

3,解码

 

    @RequestMapping(value = "/getTradeList", method = RequestMethod.POST )
    @ResponseBody
    public String getTradeList(HttpServletRequest req ) throws IOException {
        req.setCharacterEncoding("UTF-8");
       String tradeInfos = "";
       try {
           BufferedReader  bufferedReader =  new BufferedReader( new InputStreamReader(req.getInputStream(),"utf-8"));  //读取参数流


           String nextLine = bufferedReader.readLine();
           while( nextLine != null )
           {
               System.out.println(req.getCharacterEncoding());
               tradeInfos += nextLine;
               nextLine = bufferedReader.readLine();
           }
           tradeInfos =URLDecoder.decode(tradeInfos, "UTF-8");//解码
           System.out.println(tradeInfos);

            //让浏览器用utf8来解析返回的数据
            response.setHeader("Content-type", "text/html;charset=UTF-8");
            //告诉servlet用UTF-8转码,而不是用默认的ISO-8859-1
            response.setCharacterEncoding("UTF-8");
            String data = "中国";
            PrintWriter pw = response.getWriter();
            pw.write(data+tradeInfos);  //这样将数据返回到了前端,且不会乱码
            pw.close();
       } catch (IOException e) {
           e.printStackTrace();
       }

    http中文乱码问题: http://www.sojson.com/blog/227.html

    servlet后台返回及乱码问题:http://blog.csdn.net/qq_36523667/article/details/78585229


    
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值