利用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 本地保存下载文件的路径 ...
随机推荐
- Python CAN
/********************************************************************************* * Python CAN * 说明 ...
- caffe安装编译问题-ImportError: No module named google.protobuf.internal
问题描述 ~/Downloads/caffe$ python Python (default, Dec , ::) [GCC ] on linux2 Type "help", &q ...
- VM中 Ubuntu14.04 中Samba的安装配置和使用
以前都是在Redhat 上搭建samba,现在改成在ubuntu12.04 上搭建发现 sudo /etc/init.d/samba restart 始终不行,原来,现在ubuntu下,启动服务要用 ...
- 51Nod 1066:Bash游戏 (巴什博弈)
1066 Bash游戏 基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题 收藏 关注 有一堆石子共有N个.A B两个人轮流拿,A先拿.每次最少拿1颗,最多拿K颗,拿到 ...
- utf-8编码的csv文件,用excel打开乱码,解决办法,在输出前加 0xEF,0xBB,0xBF三个char
转自 http://blog.csdn.net/zcmssd/article/details/6086649 是由于输出的CSV文件中没有BOM. 什么是BOM? 在UCS 编码中有一个叫做”ZERO ...
- echar生成雷达图
function createRadarChart(indicatorData, personData) { var myChart = echarts.init(document.getElemen ...
- 如何彻底卸载Jenkins(Windows版本)
起因: 最近在做持续集成测试过程中遇到一个问题,之前部署的Jenkins管理员密码忘了之后无法登陆,而且删除掉tomcat下webapps文件夹中的Jenkins目录后,再次安装Jenkins后相关的 ...
- RK3288 mipi屏调试流程
CPU:RK3288 系统:Android 5.1 1.修改kernel/arch/arm/configs/rockchip_defconfig,打开mipi屏开关 # CONFIG_LCD_GENE ...
- 解决解密时出现"要解密的数据的长度无效" 或 "填充无效无法被移除" 的错误
1.首先排除数据库中读取加密后的字段是否被强制截断. 2.AES加密后的byte[]首先应用base64( Convert.ToBase64String)编码一次,若直接用utf8的话会报上述错误,若 ...
- shell 前台进程组的选择
控制命令如ctrl+c,ctrl+d等命令是会kill到前台进程组的,这个过程和bash进程还有tty驱动有关系. 在终端执行命令,在bash进程看来都是在执行job,然后fork出子进程来执行这些j ...