利用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 本地保存下载文件的路径 ...
随机推荐
- 如何查看linux命令行操作的历史记录-linux
前言 由于刚开始学习linux,对命令行不熟悉,可以查看使用过的命令行历史记录,熟悉命令行并熟练操作,对命令行进行深入地理解. 系统环境 OS:ubuntu16.04. 操作过程 在主文件夹目录即ho ...
- CodeForces - 1093G:Multidimensional Queries (线段树求K维最远点距离)
题意:给定N个K维的点,Q次操作,或者修改点的坐标:或者问[L,R]这些点中最远的点. 思路:因为最后一定可以表示维+/-(x1-x2)+/-(y1-y2)+/-(z1-z2)..... 所以我们可以 ...
- BZOJ1076: [SCOI2008]奖励关【状压DP+期望DP】
Description 你正在玩你最喜欢的电子游戏,并且刚刚进入一个奖励关.在这个奖励关里,系统将依次随机抛出k次宝物, 每次你都可以选择吃或者不吃(必须在抛出下一个宝物之前做出选择,且现在决定不吃的 ...
- hdu 1203 dp(关于概率的```背包?)
题意:一个人手里有一笔钱 n ,有 m 所大学,分别知道这些大学的投简历花费和被录取概率,因为钱数有限,只能投一部分学校,问被录取的概率最大有多大. 这题除去计算概率以外就是一个 0 1 背包问题,所 ...
- ballerina 学习二十六 项目docker 部署&& 运行(二)
ballerina 从发布,到现在官方文档的更新也是很给力的,同时也有好多改进,越来越好用了 可以参考官方文档 https://ballerina.io/learn/by-guide/restful- ...
- hasura graphql server event trigger 试用
hasura graphql server 是一个很不错的graphql 引擎,当前版本已经支持event triiger 了 使用此功能我们可以方便的集成webhook功能,实现灵活,稳定,快捷的消 ...
- php header运用细节
http://www.111cn.net/phper/php-function/55872.htm http://blog.sina.com.cn/s/blog_7298f36f01011dxv.ht ...
- web.xml中context-param详解
<context-param> <param-name>contextConfigLocation</param-name> <param-value> ...
- C语言面试题4
第二部分:程序代码评价或者找错 1.下面的代码输出是什么,为什么?void foo(void){ unsigned int a = 6; int b = -20; (a+b > ...
- 使用Apriori进行关联分析(二)
书接上文(使用Apriori进行关联分析(一)),介绍如何挖掘关联规则. 发现关联规则 我们的目标是通过频繁项集挖掘到隐藏的关联规则. 所谓关联规则,指通过某个元素集推导出另一个元素集.比如有一个频繁 ...