欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

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

程序员文章站 2024-02-26 08:26:16
废话不多说了,直接给大家贴代码了,具体代码如下所示; package com.github.pandafang.tool; import java.io.buf...

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

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

下载一个百度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 从网上下载文件的几种方式实例代码详解,希望对大家有所帮助