熱點推薦:
您现在的位置: 電腦知識網 >> 編程 >> Java編程 >> Java核心技術 >> 正文

使用request.getInputStream() 獲取POST數據

2022-06-13   來源: Java核心技術 

  近有個項目下需要接收POST過來的XML數據包在struts中的Action方法中調用requestgetInputStream() 發現無法讀到任何數據 以下是POST數據的程序代碼: Java代碼
    URL url = new URL(urlBuf);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url
                    openConnection();
            (true);
            (true);
            (POST);         ();
            ();
            ();
            BufferedWriter out = new BufferedWriter(new OutputStreamWriter(
                    () GBK));
            outwrite(word);
            outflush();
    view plaincopy to clipboardprint?
    URL url = new URL(urlBuf);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url
                    openConnection();
            (true);
            (true);
            (POST);         ();
            ();
            ();
            BufferedWriter out = new BufferedWriter(new OutputStreamWriter(
                    () GBK));
            outwrite(word);
            outflush();

  URL url = new URL(urlBuf);
      HttpURLConnection httpURLConnection = (HttpURLConnection) url
        openConnection();
      (true);
      (true);
      (POST);   ();
      ();
      ();
      BufferedWriter out = new BufferedWriter(new OutputStreamWriter(
        () GBK));
      outwrite(word);
      outflush();這個是讀取程序代碼: Java代碼
    BufferedReader br = new BufferedReader(new InputStreamReader((ServletInputStream)requestgetInputStream()));
            String line = null;
            StringBuilder sb = new StringBuilder();
            while((line = brreadLine())!=null){
                sbappend(line);
            }
    view plaincopy to clipboardprint?
    BufferedReader br = new BufferedReader(new InputStreamReader((ServletInputStream)requestgetInputStream()));
            String line = null;
            StringBuilder sb = new StringBuilder();
            while((line = brreadLine())!=null){
                sbappend(line);
            }

  BufferedReader br = new BufferedReader(new InputStreamReader((ServletInputStream)requestgetInputStream()));
      String line = null;
      StringBuilder sb = new StringBuilder();
      while((line = brreadLine())!=null){
       sbappend(line);
      }開始還以為是犯了什麼低級錯誤不斷調試查找原因無果baidu上搜索了下眾說紛纭也沒有很好的解決方案我也左思右想文件上傳也是在Action立面處理的組件肯定用到inpputStream一點問題沒有沒可能這裡不行後來靈光一閃在POST程序中加入: Java代碼
    (contenttype text/html);
    view plaincopy to clipboardprint?
    (contenttype text/html);

  (contenttype text/html);測試成功! 根據以上現象我們不難看出struts對沒有指定contenttype的request請求封裝時候作了一些處理導致無法在Action中獲取requestgetInputStream() 和 requestgetReader()


From:http://tw.wingwit.com/Article/program/Java/hx/201311/26268.html
  • 上一篇文章:

  • 下一篇文章:
  • 推薦文章
    Copyright © 2005-2022 電腦知識網 Computer Knowledge   All rights reserved.