httpclient4下载图片 java实现[通俗易懂]

httpclient4下载图片 java实现[通俗易懂]有时候需要从网上抓取一下图片jpg、png等,也可以抓取zip等,这样就需要写程序才能达到想要的效果,下面是用httpclient4做一个工具类,非常的好用packagecom.wamei.tool;importjava.awt.image.BufferedImage;importjava.io.File;importjava.io.FileOutputStream;

大家好,又见面了,我是你们的朋友全栈君。

有时候需要从网上抓取一下图片jpg、png等,也可以抓取zip等,这样就需要写程序才能达到想要的效果,

下面是用httpclient4做一个工具类,非常的好用

package com.wamei.tool;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.util.UUID;

import com.wamei.util.ImageUtil;
import com.wamei.util.JsonResponseHelper;
import com.wamei.util.SystemConstants;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.log4j.Logger;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;

/**
 * Created by qixuan.chen on 2016/8/18.
 */
public class ImageDownloadUtil {

    private static final Logger logger = Logger.getLogger(ImageDownloadUtil.class);

    public static String download(HttpServletRequest request,String url, String savePath, Integer width, Integer height) {
        HttpClient httpclient = new DefaultHttpClient();
        String picSrc = "";
        String picType = url.substring(url.lastIndexOf(".")+1,url.length());
        String fileName = UUID.randomUUID().toString().replace("-", "")+"."+picType;
        String path = request.getSession().getServletContext().getRealPath(savePath+fileName);
        File storeFile = null;
        try {
            HttpGet httpget = new HttpGet(url);

            //伪装成google的爬虫
            httpget.setHeader("User-Agent", "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)");
            // Execute HTTP request
            logger.info("executing request: " + httpget.getURI());
            HttpResponse response = httpclient.execute(httpget);
            storeFile = new File(path);
            FileOutputStream output = new FileOutputStream(storeFile);

            // 得到网络资源的字节数组,并写入文件
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                InputStream instream = entity.getContent();
                try {
                    byte b[] = new byte[1024];
                    int j = 0;
                    while( (j = instream.read(b))!=-1){
                        output.write(b,0,j);
                    }
                    output.flush();
                    output.close();
                } catch (IOException ex) {
                    // In case of an IOException the connection will be released
                    // back to the connection manager automatically
                    throw ex;
                } catch (RuntimeException ex) {
                    // In case of an unexpected exception you may want to abort
                    // the HTTP request in order to shut down the underlying
                    // connection immediately.
                    httpget.abort();
                    throw ex;
                } finally {
                    // Closing the input stream will trigger connection release
                    try { instream.close(); } catch (Exception ignore) {}
                }
                if (storeFile.exists()) {
                    BufferedImage newImage = ImageUtil.getFileImage(storeFile, width, height);
                    ImageIO.write(newImage, picType, storeFile);
                    picSrc = "http://"+ JsonResponseHelper.serverAddress+"/wamei/"+savePath+fileName;
                }

            }

        } catch (Exception e) {
            logger.error(e.getMessage(), e);
        } finally {
            httpclient.getConnectionManager().shutdown();
        }


        return picSrc;

    }

    public static void main(String[] args) throws MalformedURLException {
        //抓取下面图片的测试
        //ImageDownloadUtil.download("http://blog.goyiyo.com/wp-content/uploads/2012/12/6E0E8516-E1DC-4D1D-8B38-56BDE1C6F944.jpg", "d:/aaa.jpg");
    }
}

参考代码:

package com.yododo.fds.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.log4j.Logger;

public class JpgDownloadUtil {
/**
* Logger for this class
*/
private static final Logger logger = Logger.getLogger(JpgDownloadUtil.class);

public static void download(String url, String filePathName) {
HttpClient httpclient = new DefaultHttpClient();
try {
HttpGet httpget = new HttpGet(url);

//伪装成google的爬虫JAVA问题查询
httpget.setHeader("User-Agent", "Mozilla/5.0 (compatible; Googlebot/2.1; +http://www.google.com/bot.html)");
// Execute HTTP request
System.out.println("executing request " + httpget.getURI());
HttpResponse response = httpclient.execute(httpget);

File storeFile = new File(filePathName);
FileOutputStream output = new FileOutputStream(storeFile);

// 得到网络资源的字节数组,并写入文件
HttpEntity entity = response.getEntity();
if (entity != null) {
InputStream instream = entity.getContent();
try {
byte b[] = new byte[1024];
int j = 0;
while( (j = instream.read(b))!=-1){
output.write(b,0,j);
}
output.flush();
output.close();
} catch (IOException ex) {
// In case of an IOException the connection will be released
// back to the connection manager automatically
throw ex;
} catch (RuntimeException ex) {
// In case of an unexpected exception you may want to abort
// the HTTP request in order to shut down the underlying
// connection immediately.
httpget.abort();
throw ex;
} finally {
// Closing the input stream will trigger connection release
try { instream.close(); } catch (Exception ignore) {}
}
}

} catch (Exception e) {
logger.error(e.getMessage(), e);
} finally {
httpclient.getConnectionManager().shutdown();
}
}

public static void main(String[] args) throws MalformedURLException {

//抓取下面图片的测试
JpgDownloadUtil.download("http://blog.goyiyo.com/wp-content/uploads/2012/12/6E0E8516-E1DC-4D1D-8B38-56BDE1C6F944.jpg", "c:/aaa.jpg");
}
}

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/163282.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号