栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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;

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();
    } 
  }
}

下载一个百度logo 测试一下

 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 从网上下载文件的几种方式实例代码详解,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对考高分网网站的支持!

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/144623.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号