我想將一個.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
可以顯示堆棧跟蹤 – kgautron 2012-08-07 13:38:28
您需要創建多部分帖子。你知道有些庫可以完成你手動做的所有工作,對嗎?他們會使這項任務更容易。 – 2012-08-07 13:39:49