訪問第二次從URLConnection獲取輸入流時發生504異常。當我的系統重新啓動時,即時通訊訪問特定的網址它的工作,但第二次訪問時,它會引發錯誤。訪問第二次 - > 504異常,而來自URLConnection的getInputStream()
注:
Using Tomcat 6, Java, JSP
代碼波紋管:
OutputStream outStream = null;
URLConnection uCon = null;
InputStream is = null;
try {
URL Url;
byte[] buf;
int ByteRead, ByteWritten = 0;
Url = new URL("www.sample.com/download/file.xml");
outStream = new BufferedOutputStream(new FileOutputStream("/home/temp/myfile.xml"));
uCon = Url.openConnection();
is = uCon.getInputStream();
buf = new byte[size];
while ((ByteRead = is.read(buf)) != -1) {
outStream.write(buf, 0, ByteRead);
ByteWritten += ByteRead;
}
System.out.println("Downloaded Successfully.");
is.close();
outStream.close();
} catch (Exception e) {
System.out.println("Required File is not there "+e.getMessage());
}
並且用於調試:不要使用catch(Exception e),因爲您無法確定拋出了哪個異常,單獨捕獲所有異常。 – puchmu 2013-02-28 11:45:24
此外,使用「catch(Exception e)」是一個禁止使用,它是一個市長異常處理反模式(谷歌未經檢查的例外),不應該像這樣使用! – puchmu 2013-02-28 13:07:49