2012-08-07 118 views
0

我想將一個.txt文件發佈到我的系統上的本地tomcat webserver。 但是,當我嘗試做一個職位,然後我得到一個錯誤:未找到。 源文件存在,但即使在那之後,我得到這個錯誤。 您能否讓我知道我在這裏做錯了什麼。我在下面粘貼了我的代碼。HTTP POST not working.Getting Not Found error

File file = new File("C:\\xyz\\test.txt");  
    URL url = new URL("http://localhost:8080/process/files");  
    urlconnection = (HttpURLConnection) url.openConnection();  
    urlconnection.setDoOutput(true);  
    urlconnection.setDoInput(true); 
    urlconnection.setRequestMethod("POST"); 

    BufferedOutputStream bos = new BufferedOutputStream(urlconnection.getOutputStream()); 
    BufferedInputStream bis = new BufferedInputStream(new FileInputStream(file)); 
    int i;  // read byte by byte until end of stream  
    while ((i = bis.read()) >0) { 
     bos.write(i);  
     } 
    bos.close(); 

    System.out.println(((HttpURLConnection)urlconnection).getResponseMessage()); 
    } catch(Exception ae) 
    { 
     ae.printStackTrace(); 
    } 
    try { 
     InputStream inputStream; 
       int responseCode=((HttpURLConnection)urlconnection).getResponseCode();  
       if ((responseCode>= 200) &&(responseCode<=202)) { 
        inputStream = ((HttpURLConnection)urlconnection).getInputStream();  
        int j;  
        while ((j = inputStream.read()) >0) { 
         System.out.println("------ TESTING ------");  
         }  
        } else { 
         inputStream = ((HttpURLConnection)urlconnection).getErrorStream(); 
         } 
       ((HttpURLConnection)urlconnection).disconnect(); 
      } catch (IOException e) { // TODO Auto-generated catch block 
          e.printStackTrace(); 
     } 
    } 

您能否讓我知道這裏出了什麼問題。 現在我很長一段時間在這個問題上摸不着頭腦。

感謝 Vikeng

+0

可以顯示堆棧跟蹤 – kgautron 2012-08-07 13:38:28

+0

您需要創建多部分帖子。你知道有些庫可以完成你手動做的所有工作,對嗎?他們會使這項任務更容易。 – 2012-08-07 13:39:49

回答

0

您要發佈帖子需要指向一個servlet或類似的URL。您不能僅通過發送POST請求將文件上傳到目錄 - POST請求必須由某些東西來處理。

+0

謝謝邁克爾。是的你是對的,發佈請求需要通過servlet,jsp或類似的方式處理。我有點困惑,因爲我剛接觸到這個HTTP PUT/POST概念,我被告知要在遠程服務器上執行此操作。 – vikeng21 2012-08-11 04:10:59

相關問題