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 从网上下载文件的几种方式的更多相关文章

  1. 【文件下载】Java下载文件的几种方式

    [文件下载]Java下载文件的几种方式  摘自:https://www.cnblogs.com/sunny3096/p/8204291.html 1.以流的方式下载. public HttpServl ...

  2. 从后端接口下载文件的2种方式:get方式、post方式

    从后端接口下载文件的2种方式 一.get方式 直接使用: location.href='http://www.xxx.com/getFile?params1=xxx&params2=xxxx' ...

  3. Java下载文件的几种方式

    转发自博客园Sunny的文章 1.以流的方式下载 public HttpServletResponse download(String path, HttpServletResponse respon ...

  4. java 下载文件的两种方式和java文件的上传

    一:以网络的方式下载文件 try { // path是指欲下载的文件的路径. File file = new File(path); // 以流的形式下载文件. InputStream fis = n ...

  5. Asp.Net 下载文件的几种方式

    asp.net下载文件几种方式 protected void Button1_Click(object sender, EventArgs e) { /* 微软为Response对象提供了一个新的方法 ...

  6. php下载文件的一种方式

    <?php ob_start(); // $file_name="cookie.jpg"; $file_name="abc.jpg"; //用以解决中文不 ...

  7. asp.net 浏览器下载文件的四种方式

    // 方法一:TransmitFile实现下载 protected void Button1_Click(object sender, EventArgs e) { Response.ContentT ...

  8. C#从服务器下载文件的四种方式

    //方法一:TransmitFile实现下载 string fileName = "ss.docx"; //客户端预设的文件名,导出时可修改  string filePath = ...

  9. Java IO读写大文件的几种方式及测试

    读取文件大小:1.45G 第一种,OldIO: public static void oldIOReadFile() throws IOException{ BufferedReader br = n ...

随机推荐

  1. Codeforces - tag::dp 大合集 [占坑 6 / inf]

    Gym - 100753J 某国家仅有金币和银币两种货币,起汇率为g,纪念品市场有n个商人和商品,商人结帐只用银币,并且把一堆银币装在袋子里,分为三种类型,分别按向下/向上/四舍五入取整(其中向上的优 ...

  2. 使用webbench工具测试网站访问压力

    介绍 Webbench是一个在Linux下使用的网站压测工具.它使用fork()模拟多个客户端 同时访问我们设定的URL,测试网站在压力下工作的性能, 最多可以模拟3万个并发连接去测试网站的负载能力. ...

  3. win 10安装应用程序提示Error 1317的解决方法

    打开windows防火墙 关闭文件夹权限访问保护

  4. linux 安装python

    wget https://www.python.org/ftp/python/3.6.0/Python-3.6.0.tgz #下载安装包 tar zxvf Python-3.6.0.tgz #解压 . ...

  5. 04-oracle时间函数

    add_months(sysdate,x)x月之后的日期:last_day(sysdate)指定日期所在月份的最后一天:next_day(sysdate,'星期x')当前日期后的下一个星期x: mon ...

  6. ionic3 pop到指定页面

    this.navCtrl.getViews().forEach(element => { if(element.name == 'JiecheHomePage'){ this.navCtrl.p ...

  7. React条件性渲染

    React条件性渲染的方式和Vue是不同的,之前用vue做项目时觉得vue是在是强大,通过v-if就可以选择性的渲染组件,另外,对于列表的渲染更是方便,一个v-for就可以进行快速的渲染,但是Reac ...

  8. LINUX安装UNZIP

    安装完linux ,发现没有UNZIP,没办法,重新安装. 1.获取unzip源码 sudo wget http://downloads.sourceforge.net/infozip/unzip55 ...

  9. git学习笔记6

    打标签 git tag -m "Say bye-bye to all previous practice." old_practice //引号里是注释 本地删除不是真的删除,对暂 ...

  10. mongodb数据库还原

    ./mongorestore -h -u myhuiqu -p Huiqu.com@ --authenticationDatabase /