日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

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

發(fā)布時間:2024/9/19 java 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java 文件下载详解_Java 从网上下载文件的几种方式实例代码详解 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

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

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

}

}

}

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

}

總結(jié)

以上所述是小編給大家介紹的Java 從網(wǎng)上下載文件的幾種方式實例代碼詳解,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對我們網(wǎng)站的支持!

時間: 2017-08-26

與50位技術(shù)專家面對面20年技術(shù)見證,附贈技術(shù)全景圖

總結(jié)

以上是生活随笔為你收集整理的java 文件下载详解_Java 从网上下载文件的几种方式实例代码详解的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。