Java從網(wǎng)上下載文件的幾種方式實(shí)例代碼詳解

廢話不多說了,直接給大家貼代碼了,具體代碼如下所示;

創(chuàng)新互聯(lián)公司是一家集網(wǎng)站建設(shè),大埔企業(yè)網(wǎng)站建設(shè),大埔品牌網(wǎng)站建設(shè),網(wǎng)站定制,大埔網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營(yíng)銷,網(wǎng)絡(luò)優(yōu)化,大埔網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競(jìng)爭(zhēng)力。可充分滿足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶成長(zhǎng)自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。

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 {
  /**
   * 使用傳統(tǒng)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 ,官網(wǎng) 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();
    } 
  }
}

下載一個(gè)百度logo 測(cè)試一下

 public static void main(String[] args) {
    FileTool.downloadByNIO2("/upload/otherpic45/112132.png", "/home/panda/picture", "baidu_logo.png");
    System.out.println("done...");
  }

總結(jié)

以上所述是小編給大家介紹的Java 從網(wǎng)上下載文件的幾種方式實(shí)例代碼詳解,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)創(chuàng)新互聯(lián)網(wǎng)站的支持!

網(wǎng)站標(biāo)題:Java從網(wǎng)上下載文件的幾種方式實(shí)例代碼詳解
地址分享:http://www.muchs.cn/article18/ijcjgp.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站設(shè)計(jì)、云服務(wù)器、虛擬主機(jī)小程序開發(fā)、微信小程序、移動(dòng)網(wǎng)站建設(shè)

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

成都做網(wǎng)站