java 從網上下載檔案的幾種方式,java下載檔案

來源:互聯網
上載者:User

java 從網上下載檔案的幾種方式,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();        }             }}

 

下載一個百度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...");    }

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.