[英]HttpURLConnection and download large files
I use HttpURLConnection
to download files from a url. 我使用
HttpURLConnection
从URL下载文件。
URL obj = new URL(url);
HttpURLConnection con = (HttpURLConnection) obj.openConnection();
// optional default is GET
con.setRequestMethod("GET");
con.setRequestProperty("Cache-Control", "no-cache");
int responseCode = con.getResponseCode();
System.out.println("\nSending 'GET' request to URL : " + url);
System.out.println("Response Code : " + responseCode);
try {
InputStream inputStream = con.getInputStream();
FileOutputStream outputStream = new FileOutputStream("C:\\programs\\TRYFILE.csv");
int bytesRead = -1;
byte[] buffer = new byte[4096];
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
} catch(Exception e) {
} finally {
outputStream.close();
inputStream.close();
}
The code works to download small sized files (ie 25 KB). 该代码可用于下载小型文件(即25 KB)。 I didn't try to download large files (on the order of 100 MB), because the files from the particular URL are always small.
我没有尝试下载大文件(大约100 MB),因为来自特定URL的文件总是很小。
I want to know what happens if I try to download larger files with this code: will it continue to work or throw an exception? 我想知道使用此代码下载较大的文件会发生什么:它将继续工作还是会引发异常? Do I need to implement code (utilizing, say,
setConnectTimeout
or setReadTimeout
) for bigger files? 我是否需要为更大的文件实现代码(利用
setConnectTimeout
或setReadTimeout
)?
Is there a url you can suggest where I can try to download large file using this code? 是否有网址可以建议我在哪里可以尝试使用此代码下载大文件?
As suggested in the comments, create a large file yourself. 如注释中所建议,自己创建一个大文件。 To serve it over HTTP, the easiest way is probably to run Python 3 from the directory where you put the file -
要通过HTTP提供服务,最简单的方法可能是从放置文件的目录中运行Python 3-
python -m http.server
which will start a server on 8000. See this blog post for more details, or check out the python documentation. 它将在8000上启动服务器。有关更多详细信息,请参见此博客文章 ,或查看python文档。
Then you can test this yourself. 然后,您可以自己对此进行测试。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.