我正在使用HttpURLConnection下载xml文件。我可以在我的网络浏览器中看到该文件,加载需要10秒以上。但我最终可以看到内容。但是我无法通过我的java代码下载它。好像我的所有SetTimeOut()都没有真正起作用。这是我的代码,请帮助:
HttpURLConnection con = (HttpURLConnection) laURL.openConnection();
con.setRequestMethod("GET");
con.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
con.setConnectTimeout(15*1000);
con.setReadTimeout(15*1000);
InputStream ins = con.getInputStream();
InputStreamReader isr = new InputStreamReader(ins);
BufferedReader in = new BufferedReader(isr);
String inputLine;
while ((inputLine = in.readLine()) != null)
{
System.out.println(inputLine);
}
in.close();
答案 0 :(得分:0)
嗯......你能把它下载为二进制文件吗?
试试这个:
con.setRequestProperty("content-type", "binary/data");
try (InputStream in = con.getInputStream(); FileOutputStream out = new FileOutputStream(destinationPath))
{
byte[] b = new byte[1024];
int count;
while ((count = in.read(b)) > 0)
{
out.write(b, 0, count);
}
}