import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL; import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils; public class ImgDownload { public static void main(String[] args) {
downloadHttpUrl("https://www.baidu.com/img/bd_logo1.png", "D:/test/", "test.png");
} public static String downloadHttpUrl(String url, String dir, String fileName) {
try {
URL httpurl = new URL(url);
File f = new File(dir + fileName);
FileUtils.copyURLToFile(httpurl, f);
} catch (Exception e) {
e.printStackTrace();
return null;
}
return dir + fileName;
} public static FileOutputStream openOutputStream(File file) throws IOException {
if (file.exists()) {
if (file.isDirectory()) {
throw new IOException("File '" + file + "' exists but is a directory");
}
if (file.canWrite() == false) {
throw new IOException("File '" + file + "' cannot be written to");
}
} else {
File parent = file.getParentFile();
if (parent != null && parent.exists() == false) {
if (parent.mkdirs() == false) {
throw new IOException("File '" + file + "' could not be created");
}
}
}
return new FileOutputStream(file);
} public static void copyURLToFile(URL source, File destination) throws IOException {
InputStream input = source.openStream();
try {
FileOutputStream output = openOutputStream(destination);
try {
IOUtils.copy(input, output);
} finally {
IOUtils.closeQuietly(output);
}
} finally {
IOUtils.closeQuietly(input);
}
} }

java IOUtils下载图片的更多相关文章

  1. java假设模拟请求重新启动路由器(网络爬虫经常使用),还有java怎样下载图片

    我们假设在公司或家里使用网络爬虫去抓取自己索要的一些数据的时候,经常对方的站点有defence机制,会给你的http请求返回500错误,仅仅要是同样IP就请求不到数据,这时候我们仅仅能去重新启动路由器 ...

  2. 关于Java/Kotlin下载图片,图片打开不能显示问题探究

    图片下载其实是个很简单的功能,通过IO流从在线地址获取流,之后将流输出到文件即可完成下载功能,但是,最近我发现某个网站中的图片下载成功,但是打开却是无法打开,这让我迷惑,百度上根本就没有人说清楚 今天 ...

  3. 用java页面下载图片

    try { //根据String形式创建一个URL对象 String filePath = materialProductWorks.getDownloadImageUrl(); URL url = ...

  4. java远程下载图片

    从别的网站复制文章的时候,要把图片下载到我们网站,再把图片地址换成我们网站的地址 <img id="mbkenHUwhWeOj9U8K6c8LlAXaes3oXit-M4SnmRvB4 ...

  5. java压缩下载图片并以zip流的形式下载到客户端

    /** * * @param page * @param rows * @param works * @return * @author ffwwzz 下载zip * @throws IOExcept ...

  6. Java HttpURLConnection 下载图片 图片全是“加密图片”文字,怎么解决?

    package com.qzf.util; import java.io.FileOutputStream;import java.io.IOException;import java.io.Inpu ...

  7. 记录java ftp下载图片只有96KB的问题

    public InputStream downloadFile(String path) { if(StringUtils.isBlank(path)) { return null; } connne ...

  8. Java部分总结图片版(已经加上原图链接下载!!!)

    Java基础知识图片版(原图下载链接)

  9. Java学习笔记——File类之文件管理和读写操作、下载图片

    Java学习笔记——File类之文件管理和读写操作.下载图片 File类的总结: 1.文件和文件夹的创建 2.文件的读取 3.文件的写入 4.文件的复制(字符流.字节流.处理流) 5.以图片地址下载图 ...

随机推荐

  1. Git命令大总结(纯手办)

    Git完整命令手册地址:http://git-scm.com/docs PDF版命令手册地址:github-git-cheat-sheet.pdf 1.git config -l查看全局用户信息配置 ...

  2. Linux学习-函式库管理

    动态与静态函式库 首先我们要知道的是,函式库的类型有哪些?依据函式库被使用的类型而分为两大类,分别是静态 (Static) 与动态 (Dynamic) 函式库两类. 静态函式库的特色: 扩展名:(扩展 ...

  3. css 透明度使用

    设置元素整体透明度: div{ opacity: 0.5; } 设置背景色透明度 div{ background: rgba(0,0,0,0.5); }

  4. python算法-汉诺塔问题

    汉诺塔问题   初始状态: 思考:当盘子的个数是3的时候,大家写出移动顺序 移动的步骤: 3个盘子,从a到c 1.前面两个盘子,从a到b 1)把前面一个盘子,从a到c a->c 2)把第二个盘子 ...

  5. Selenium WebDriver-通过断言页面是否存在某些关键字来确定页面按照预期加载

    #encoding=utf-8 import unittest import time import chardet from selenium import webdriver class Visi ...

  6. you build it,you run it

    this article is almostly about  a book named Migrating_to_Microservices_Databases, and it's just the ...

  7. DS博客作业06--图

    1.本周学习总结 1.1.思维导图 1.2.谈谈你对图结构的认识及学习体会 本章学习了图结构的相关知识,图形结构属于复杂的非线性数据结构,在实际应用中很多问题可以用图来描述.在图结构中,每个元素可以有 ...

  8. 【bzoj1307】玩具 单调栈

    题目描述 小球球是个可爱的孩子,他喜欢玩具,另外小球球有个大大的柜子,里面放满了玩具,由于柜子太高了,每天小球球都会让妈妈从柜子上拿一些玩具放在地板上让小球球玩. 这天,小球球把所有的N辆玩具摆成一排 ...

  9. UVA11367 Full Tank? 【分层图最短路】

    题目 After going through the receipts from your car trip through Europe this summer, you realised that ...

  10. Spring和redis简单测试demo

    1.1 首先创建一个Maven工程 File --> New --> Other,然后选择Maven目录下的Maven Project,如下图: 然后在弹出的面板中选择配置后,下一步即可, ...