java 从网上下载文件的几种方式
2021/11/23 22:40:18
本文主要是介绍java 从网上下载文件的几种方式,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!
package com.github.pandafang.tool; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.net.URL; import java.nio.channels.Channels; import java.nio.channels.FileChannel; import java.nio.channels.ReadableByteChannel; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.StandardCopyOption; import org.apache.commons.io.FileUtils; /** * 文件工具类 * @author panda fang * @date 2017-08-26 * @version 1.0 */ public class FileTool { /** * 使用传统io stream 下载文件 * @param url * @param saveDir * @param fileName */ public static void download(String url, String saveDir, String fileName) { BufferedOutputStream bos = null; InputStream is = null; try { byte[] buff = new byte[8192]; is = new URL(url).openStream(); File file = new File(saveDir, fileName); file.getParentFile().mkdirs(); bos = new BufferedOutputStream(new FileOutputStream(file)); int count = 0; while ( (count = is.read(buff)) != -1) { bos.write(buff, 0, count); } } catch (IOException e) { e.printStackTrace(); } finally { if (is != null) { try { is.close(); } catch (IOException e) { e.printStackTrace(); } } if (bos != null) { try { bos.close(); } catch (IOException e) { e.printStackTrace(); } } } } /** * 利用 commonio 库下载文件,依赖Apache Common IO ,官网 https://commons.apache.org/proper/commons-io/ * @param url * @param saveDir * @param fileName */ public static void downloadByApacheCommonIO(String url, String saveDir, String fileName) { try { FileUtils.copyURLToFile(new URL(url), new File(saveDir, fileName)); } catch (IOException e) { e.printStackTrace(); } } /** * 使用NIO下载文件, 需要 jdk 1.4+ * @param url * @param saveDir * @param fileName */ public static void downloadByNIO(String url, String saveDir, String fileName) { ReadableByteChannel rbc = null; FileOutputStream fos = null; FileChannel foutc = null; try { rbc = Channels.newChannel(new URL(url).openStream()); File file = new File(saveDir, fileName); file.getParentFile().mkdirs(); fos = new FileOutputStream(file); foutc = fos.getChannel(); foutc.transferFrom(rbc, 0, Long.MAX_VALUE); } catch (IOException e) { e.printStackTrace(); } finally { if (rbc != null) { try { rbc.close(); } catch (IOException e) { e.printStackTrace(); } } if (foutc != null) { try { foutc.close(); } catch (IOException e) { e.printStackTrace(); } } } } /** * 使用NIO下载文件, 需要 jdk 1.7+ * @param url * @param saveDir * @param fileName */ public static void downloadByNIO2(String url, String saveDir, String fileName) { try (InputStream ins = new URL(url).openStream()) { Path target = Paths.get(saveDir, fileName); Files.createDirectories(target.getParent()); Files.copy(ins, target, StandardCopyOption.REPLACE_EXISTING); } catch (IOException e) { e.printStackTrace(); } } }
public static void main(String[] args) { FileTool.downloadByNIO2("http://www.baidu.com/img/bd_logo1.png", "/home/panda/picture", "baidu_logo.png"); System.out.println("done..."); }
这篇关于java 从网上下载文件的几种方式的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!
- 2024-10-05小米13T Pro系统合集:性能与摄影的极致融合,值得你升级的系统ROM
- 2024-10-01基于Python+Vue开发的医院门诊预约挂号系统
- 2024-10-01基于Python+Vue开发的旅游景区管理系统
- 2024-10-01RestfulAPI入门指南:打造简单易懂的API接口
- 2024-10-01初学者指南:了解和使用Server Action
- 2024-10-01Server Component入门指南:搭建与配置详解
- 2024-10-01React 中使用 useRequest 实现数据请求
- 2024-10-01使用 golang 将ETH账户的资产平均分散到其他账户
- 2024-10-01JWT用户校验课程:从入门到实践
- 2024-10-01Server Component课程入门指南