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 下载文件到本地的更多相关文章

  1. PHP实现远程下载文件到本地

    PHP实现远程下载文件到本地 投稿:hebedich 字体:[增加 减小] 类型:转载   经常写采集器发布接口需要使用到远程附件的功能,所以自己写了一个PHP远程下载文件到本地的函数,一般情况下已经 ...

  2. PHP CURL实现远程下载文件到本地

    <?php //$result=httpcopy('http://www.phpernote.com/image/logo.gif'); echo '<pre>';print_r($ ...

  3. 在Xshell中上传下载文件到本地(linux中从多次ssh登录的dbserver里面的文件夹)

    在Xshell中上传下载文件到本地(linux中从多次ssh登录的dbserver里面的文件夹) 1 列出所有需要copy的sh文件 -bash-4.1$ ll /mysqllog/osw/*.sh ...

  4. 使用xshell从远程服务器下载文件到本地

    XSHELL工具上传文件到Linux以及下载文件到本地(Windows) Xshell很好用,然后有时候想在windows和linux上传或下载某个文件,其实有个很简单的方法就是rz,sz.首先你的L ...

  5. 利用隐藏 iframe 下载文件

    在开发项目中遇到问题:下载文件后台报错,下载文件的页面会出现空白或异常信息,需要解决. 解决方法:利用隐藏iframe下载文件 3:对于a标签,采用target属性方法 <a target=&q ...

  6. C#使用WebClient下载文件到本地目录

    C#使用WebClient下载文件到本地目录. 1.配置本地目录路径 <appSettings> <!--文件下载目录--> <add key="Downloa ...

  7. Js点击按钮下载文件到本地(兼容多浏览器)

    实现点击 用纯 js(非jquery)  下载文件到本地 自己尝试,加网上找了好久未果,如: window.open(url)   location.href=url   form表单提交   ifr ...

  8. 【转】XSHELL下直接下载文件到本地(Windows)

    XSHELL下直接下载文件到本地(Windows) http://www.cnblogs.com/davytitan/p/3966606.html

  9. 通过指定的 url 去网络或者文件服务器下载文件到本地某个文件夹

    /** * 从网络Url中下载文件 * @param urlStr 指定的url * @param fileName 下载文件到本地的名字 * @param savePath 本地保存下载文件的路径 ...

随机推荐

  1. 如何查看Window10系统隐藏文件夹

    1 . 打开我的电脑(此电脑)等等诸如此类的称呼 2 . 点击左上角的查看选项 3 . 选中“隐藏的选项” 4 . 完成

  2. [LeetCode&Python] Problem 590. N-ary Tree Postorder Traversal

    Given an n-ary tree, return the postorder traversal of its nodes' values. For example, given a 3-ary ...

  3. HihoCoder - 1867: GCD (莫比乌斯容斥)

    Sample Input 6 1 6 2 5 3 4 Sample Output 10 You are given a {1, 2, ..., n}-permutation a[1], a[2], . ...

  4. 【BZOJ2558】Count on a tree

    又是因为傻逼错误浪费了半天时间 原题: 给定一棵N个节点的树,每个点有一个权值,对于M个询问(u,v,k),你需要回答u xor lastans和v这两个节点间第K小的点权.其中lastans是上一个 ...

  5. from表单的分向提交

    一:需求: 思路:document.form.action,表单分向提交,javascript提交表单同一个表单可以根据用户的选择,提交给不同的后台处理程序.即,表单的分向提交.如,在编写论坛程序时, ...

  6. python 获取excel文件的所有sheet名字

    当一个excel文件的sheet比较多时候, 这时候需要获取所有的sheet的名字. xl = pd.ExcelFile('foo.xls') xl.sheet_names # see all she ...

  7. nnlog模块使用

    安利一个好用的写日志模块,python自带的logging模块理解起来比较费劲,直接pip install nnlog即可 使用例子如下代码:   1 2 3 4 5 6 7 8 9 10 11 12 ...

  8. 什么是Asp.net Core?和 .net core有什么区别?(转)

    什么是Asp.Net core 我相信很多C# Developer已经对于.net core不算陌生了,就算没有正式使用相信也应该有所了解.微软在推出来.net core的同时为了方便一些原有的项目可 ...

  9. 零基础学习hadoop开发所必须具体的三个基础知识

    大数据hadoop无疑是当前互联网领域受关注热度最高的词之一,大数据技术的应用正在潜移默化中对我们的生活和工作产生巨大的改变.这种改变给我们的感觉是“水到渠成”,更为让人惊叹的是大数据已经仅仅是互联网 ...

  10. 这些 .Net and Core 相关的开源项目,你都知道吗?(持续更新中...)

    最近更新时间2017-12-28 序列化 Json.NET http://json.codeplex.com/Json.Net是一个读写Json效率比较高的.Net框架.Json.Net 使得在.Ne ...