使用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()。

你可能感兴趣的:(java)