使用request.getInputStream() 获取POST数据

近有个项目下需要接收POST过来的XML数据包,在struts中的Action方法中,调用request.getInputStream() 发现无法读到任何数据。 以下是POST数据的程序代码: Java代码
            URL url = new URL(urlBuf);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url
                    .openConnection();
            httpURLConnection.setDoOutput(true);
            httpURLConnection.setDoInput(true);
            httpURLConnection.setRequestMethod("POST");         httpURLConnection.setConnectTimeout(5000);
            httpURLConnection.setReadTimeout(5000);
            httpURLConnection.connect();
            BufferedWriter out = new BufferedWriter(new OutputStreamWriter(
                    httpURLConnection.getOutputStream(), "GBK"));
            out.write(word);
            out.flush();
           view plaincopy to clipboardprint?


           URL url = new URL(urlBuf);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url
                    .openConnection();
            httpURLConnection.setDoOutput(true);
            httpURLConnection.setDoInput(true);
            httpURLConnection.setRequestMethod("POST");         httpURLConnection.setConnectTimeout(5000);
            httpURLConnection.setReadTimeout(5000);
            httpURLConnection.connect();
            BufferedWriter out = new BufferedWriter(new OutputStreamWriter(
                    httpURLConnection.getOutputStream(), "GBK"));
            out.write(word);
            out.flush();

    URL url = new URL(urlBuf);
      HttpURLConnection httpURLConnection = (HttpURLConnection) url
        .openConnection();
      httpURLConnection.setDoOutput(true);
      httpURLConnection.setDoInput(true);
      httpURLConnection.setRequestMethod("POST");   httpURLConnection.setConnectTimeout(5000);
      httpURLConnection.setReadTimeout(5000);
      httpURLConnection.connect();
      BufferedWriter out = new BufferedWriter(new OutputStreamWriter(
        httpURLConnection.getOutputStream(), "GBK"));
      out.write(word);
      out.flush();

这个是读取程序代码: Java代码
    BufferedReader br = new BufferedReader(new InputStreamReader((ServletInputStream)request.getInputStream()));
            String line = null;
            StringBuilder sb = new StringBuilder();
            while((line = br.readLine())!=null){
                sb.append(line);
            }
    view plaincopy to clipboardprint?
    BufferedReader br = new BufferedReader(new InputStreamReader((ServletInputStream)request.getInputStream()));
            String line = null;
            StringBuilder sb = new StringBuilder();
            while((line = br.readLine())!=null){
                sb.append(line);
            }

    BufferedReader br = new BufferedReader(new InputStreamReader((ServletInputStream)request.getInputStream()));
      String line = null;
      StringBuilder sb = new StringBuilder();
      while((line = br.readLine())!=null){
       sb.append(line);
      }

开始还以为是犯了什么低级错误,不断调试查找原因,无果。baidu上搜索了下,众说纷纭,也没有很好的解决方案。我也左思右想,文件上传也是在Action立面处理的,组件肯定用到inpputStream一点问题没有,没可能这里不行,后来灵光一闪,在POST程序中加入: Java代码
    httpURLConnection.setRequestProperty("content-type", "text/html");
    view plaincopy to clipboardprint?
    httpURLConnection.setRequestProperty("content-type", "text/html");

    httpURLConnection.setRequestProperty("content-type", "text/html");,

测试成功! 根据以上现象,我们不难看出struts对没有指定content-type的request请求,封装时候作了一些处理,导致无法在Action中获取request.getInputStream() 和 request.getReader()。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值