利用goole guava 下载文件到本地
package com.road.crawler.meizitu.crawler; import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL; import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import com.google.common.base.Strings;
import com.google.common.io.ByteStreams;
import com.google.common.io.Closer;
import com.google.common.io.Files; /**
* 下载图片到指定目录
*
*
*/
public class DowloadImage { private static Log log = LogFactory.getLog(DowloadImage.class); /**
* 下载图片到指定目录
*
* @param parentPath 指定目录
* @param imgUrl 图片地址
* @return 下载文件地址
*/
public static String download(String parentPath, String imgUrl) {
if(Strings.isNullOrEmpty(imgUrl) || Strings.isNullOrEmpty(parentPath)) {
return null;
}
if(imgUrl.length() > 500) {
return null;
}
Closer closer = Closer.create();
try {
File imageDir = new File(parentPath);
if(!imageDir.exists()) {
imageDir.mkdirs();
}
String fileName = StringUtils.substringBefore(imgUrl, "?");
fileName = StringUtils.substringAfterLast(fileName, "/");
File imageFile = new File(imageDir, fileName);
InputStream in = closer.register(new URL(imgUrl).openStream());
Files.write(ByteStreams.toByteArray(in), imageFile);
return imageFile.getAbsolutePath();
} catch(Exception ex) {
ex.printStackTrace();
log.error("image download error :"+imgUrl);
return null;
} finally {
try {
closer.close();
} catch (IOException e) {
closer = null;
}
}
}
/**
* 下载图片到指定目录
*
* @param parentPath 指定目录
* @param fileName 图片名称
* @param in 输入流
* @return 下载文件地址
*/
public static String download(String parentPath, String fileName, InputStream in) {
Closer closer = Closer.create();
try {
File imageDir = new File(parentPath);
if(!imageDir.exists()) {
imageDir.mkdirs();
}
File imageFile = new File(imageDir, fileName);
Files.write(ByteStreams.toByteArray(in), imageFile);
return imageFile.getAbsolutePath();
} catch(Exception ex) {
ex.printStackTrace();
return null;
} finally {
try {
closer.close();
} catch (IOException e) {
closer = null;
}
}
} public static void main(String[] args) {
System.out.println(DowloadImage.download("d:\\", "https://ss3.baidu.com/-fo3dSag_xI4khGko9WTAnF6hhy/image/h%3D300/sign=09cd05db104c510fb1c4e41a50582528/b8389b504fc2d5620bbc0bfeed1190ef76c66c69.jpg"));
}
}
利用goole guava 下载文件到本地的更多相关文章
- PHP实现远程下载文件到本地
PHP实现远程下载文件到本地 投稿:hebedich 字体:[增加 减小] 类型:转载 经常写采集器发布接口需要使用到远程附件的功能,所以自己写了一个PHP远程下载文件到本地的函数,一般情况下已经 ...
- PHP CURL实现远程下载文件到本地
<?php //$result=httpcopy('http://www.phpernote.com/image/logo.gif'); echo '<pre>';print_r($ ...
- 在Xshell中上传下载文件到本地(linux中从多次ssh登录的dbserver里面的文件夹)
在Xshell中上传下载文件到本地(linux中从多次ssh登录的dbserver里面的文件夹) 1 列出所有需要copy的sh文件 -bash-4.1$ ll /mysqllog/osw/*.sh ...
- 使用xshell从远程服务器下载文件到本地
XSHELL工具上传文件到Linux以及下载文件到本地(Windows) Xshell很好用,然后有时候想在windows和linux上传或下载某个文件,其实有个很简单的方法就是rz,sz.首先你的L ...
- 利用隐藏 iframe 下载文件
在开发项目中遇到问题:下载文件后台报错,下载文件的页面会出现空白或异常信息,需要解决. 解决方法:利用隐藏iframe下载文件 3:对于a标签,采用target属性方法 <a target=&q ...
- C#使用WebClient下载文件到本地目录
C#使用WebClient下载文件到本地目录. 1.配置本地目录路径 <appSettings> <!--文件下载目录--> <add key="Downloa ...
- Js点击按钮下载文件到本地(兼容多浏览器)
实现点击 用纯 js(非jquery) 下载文件到本地 自己尝试,加网上找了好久未果,如: window.open(url) location.href=url form表单提交 ifr ...
- 【转】XSHELL下直接下载文件到本地(Windows)
XSHELL下直接下载文件到本地(Windows) http://www.cnblogs.com/davytitan/p/3966606.html
- 通过指定的 url 去网络或者文件服务器下载文件到本地某个文件夹
/** * 从网络Url中下载文件 * @param urlStr 指定的url * @param fileName 下载文件到本地的名字 * @param savePath 本地保存下载文件的路径 ...
随机推荐
- I.MX6 working note for high efficiency
/**************************************************************************** * I.MX6 working note f ...
- php 加载字体 并保存成图片
// Set the content-type header("Content-type: image/png"); // Create the image $im = image ...
- 结构体:HASH表模板
这种 HASHMAP 就是一个链式前向星的表: 其中: init 函数:hashmap 创建初始化: check 函数:寻找 hash 表中是否有需要查找的值,若有则返回 1 ,否则返回 0 :遍历方 ...
- 做Global Admin
globaladmin.4budget@nokia.com 10:22 https://ultima.int.net.nokia.com/eedb/Solutions/tools/ManageUser ...
- 纯CSS绘制三角形(各种角度)类似于使用字符画法,根据位移不同,也可以使用两个元素画出三角边框
我们的网页因为 CSS 而呈现千变万化的风格.这一看似简单的样式语言在使用中非常灵活,只要你发挥创意就能实现很多比人想象不到的效果.特别是随着 CSS3 的广泛使用,更多新奇的 CSS 作品涌现出来. ...
- DD_belatedPNG解决IE6下PNG不透明问题
使用方法: 首先下载JS文件:http://dillerdesign.com/experiment/DD_belatedPNG/ 之后在页面中引用代码: <!--[if IE 6]>< ...
- django 获取前端获取render模板渲染后的html
function GetProxyServerByGroup(ths, action){ var _html = $.ajax({ url: "/nginx/get_proxy_server ...
- bzoj1042硬币购物
题目:http://www.lydsy.com/JudgeOnline/problem.php?id=1042 dp预处理+容斥原理. 先预处理求出无限制的各面值的组成方案数 f (完全背包). 求s ...
- 查看 linux cpu 、内存、服务器型号和序列号、磁盘、raid 的信息
yum -y install dmidecode 查看cpu的型号: 查看cpu的颗数:dmidecode -t processor |grep "Version"dmidecod ...
- serialize unserialize
转自 http://www.cnblogs.com/yeer/archive/2009/03/25/1421161.html php函数serialize()与unserialize() seri ...