访问第二次从 URLConnection 获取输入流时出现 504 异常。当我在访问特定 url 时重新启动我的系统时,它运行良好,但是在第二次访问时它会引发错误。
笔记 :
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());
}