广告
返回顶部
首页 > 资讯 > 精选 >Java 从网上下载文件的几种方式实例代码详解
  • 569
分享到

Java 从网上下载文件的几种方式实例代码详解

java下载文件ava 2023-05-31 06:05:43 569人浏览 独家记忆
摘要

废话不多说了,直接给大家贴代码了,具体代码如下所示;package com.GitHub.pandafang.tool;import java.io.BufferedOutputStream;import java.io.File;impo

废话不多说了,直接给大家贴代码了,具体代码如下所示;

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;public class FileTool {    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();        }      }    }  }    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();    }  }    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();        }      }    }  }    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();    }   }}

--结束END--

本文标题: Java 从网上下载文件的几种方式实例代码详解

本文链接: https://www.lsjlt.com/news/223894.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作