利用goole guava 下载文件到本地

本文介绍了一个简单的图片下载器实现方法,该下载器可以将图片从指定URL下载到本地指定目录,并提供了通过输入流下载的功能。
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"));
    }
}

  

转载于:https://www.cnblogs.com/zyzcj/p/8085714.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值