117.info
人生若只如初见

Java实现下载文件的6种方式

  1. 使用URL类的openStream()方法下载文件:
URL url = new URL(fileUrl);
InputStream inputStream = url.openStream();
FileOutputStream outputStream = new FileOutputStream(savePath);
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
outputStream.close();
inputStream.close();
  1. 使用URLConnection类下载文件:
URL url = new URL(fileUrl);
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();
int responseCode = httpConn.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
InputStream inputStream = httpConn.getInputStream();
FileOutputStream outputStream = new FileOutputStream(savePath);
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
outputStream.close();
inputStream.close();
}
httpConn.disconnect();
  1. 使用Apache HttpClient库下载文件:
CloseableHttpClient httpClient = HttpClientBuilder.create().build();
HttpGet httpGet = new HttpGet(fileUrl);
CloseableHttpResponse response = httpClient.execute(httpGet);
HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream inputStream = entity.getContent();
FileOutputStream outputStream = new FileOutputStream(savePath);
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
outputStream.close();
inputStream.close();
}
httpClient.close();
response.close();
  1. 使用Java NIO下载文件:
URL url = new URL(fileUrl);
ReadableByteChannel channel = Channels.newChannel(url.openStream());
FileOutputStream outputStream = new FileOutputStream(savePath);
outputStream.getChannel().transferFrom(channel, 0, Long.MAX_VALUE);
outputStream.close();
channel.close();
  1. 使用Java IO和BufferedStream下载文件:
URL url = new URL(fileUrl);
BufferedInputStream inputStream = new BufferedInputStream(url.openStream());
FileOutputStream outputStream = new FileOutputStream(savePath);
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = inputStream.read(buffer)) != -1) {
outputStream.write(buffer, 0, bytesRead);
}
outputStream.close();
inputStream.close();
  1. 使用Apache Commons IO库下载文件:
URL url = new URL(fileUrl);
FileUtils.copyURLToFile(url, new File(savePath));

请注意,以上代码可能需要处理异常或添加进一步的逻辑来完善下载过程。

未经允许不得转载 » 本文链接:https://www.117.info/ask/fe937AzsLBg5eAVc.html

推荐文章

  • java中异常类会影响性能吗

    在Java中,异常类本身不会对性能产生显著影响。异常处理机制是为了在程序运行时处理错误或异常情况,而不是为了优化性能。然而,在使用异常时,需要注意以下几点...

  • java中异常类如何避免

    在Java中,避免异常的最好方法是编写健壮的代码并遵循一些最佳实践。以下是一些建议: 预期和处理异常:使用try-catch语句来捕获和处理可能发生的异常。确保处理...

  • java中异常类有哪些常见类型

    Java中的异常类主要分为两大类:受检异常(Checked Exceptions)和非受检异常(Unchecked Exceptions)。以下是具体的异常类型:
    受检异常(Checked Except...

  • java中异常类怎么捕获

    在Java中,我们使用try-catch语句来捕获异常。当程序执行过程中遇到异常时,Java运行时系统会抛出一个异常对象。你可以使用try块来包含可能引发异常的代码,然后...

  • c++中ADO添加记录的方法是什么

    在C++中使用ADO(ActiveX Data Objects)库来添加记录,需要以下几个步骤:1. 引入头文件和命名空间: #include #import "C:\Program Files\Common Files\System...

  • 云服务器除了搭建网站还可以干什么

    云服务器除了搭建网站,还可以用于以下用途:1. 应用托管:可以将应用程序部署到云服务器上,提供稳定可靠的运行环境。2. 数据存储:可以在云服务器上存储和管理...

  • ShellExecute函数返回值是什么

    ShellExecute函数的返回值是一个HINSTANCE类型的值,它表示执行操作的实例句柄。如果函数执行成功,返回值为大于32的值,其中包括HINSTANCE类型的实例句柄。如果...

  • django ObjectDoesNotExist 和 DoesNotExist的用法

    在Django中,ObjectDoesNotExist是异常类,用于表示查询对象不存在的情况。它是django.core.exceptions模块中的一个子类。
    通常情况下,当在查询数据时,如...