Chinaunix首页 | 论坛 | 博客
  • 博客访问: 273983
  • 博文数量: 757
  • 博客积分: 40040
  • 博客等级: 大将
  • 技术积分: 4935
  • 用 户 组: 普通用户
  • 注册时间: 2008-09-09 12:37
文章分类

全部博文(757)

文章存档

2011年(1)

2008年(756)

我的朋友

分类:

2008-09-09 12:42:01

    近有个项目下需要接收POST过来的XML数据包,在struts中的Action方法中,调用request.getInputStream() 发现无法读到任何数据。 以下是POST数据的程序代码: 代码
    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();这个是读取程序代码: 代码
    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()。

【责编:landy】

--------------------next---------------------

阅读(179) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~