httpclient4下载图片 java实现[通俗易懂]
2023-06-13 09:12:41 时间
大家好,又见面了,我是你们的朋友全栈君。
有时候需要从网上抓取一下图片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");
}
}
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/163282.html原文链接:https://javaforall.cn
相关文章
- java中数组转列表_Java数组转list
- 二级java程序设计--Java SDK6.0的下载和操作[通俗易懂]
- java 实现 按位异或_Java 按位异或的性质及其妙用
- java下载文件或文件夹
- java工作流_Java 实现简单工作流
- 怎么下载java_怎样下载Java的环境?
- java课程设计成绩管理系统_Java课程设计–学生成绩管理系统
- md5 java 实现_MD5加密的Java实现
- java技术介绍_Java技术汇总
- java setproperty 未生效_Java System类setProperty()方法及示例[通俗易懂]
- JAVA数据库连接池_java与数据库的连接怎么实现
- java编码native2ascii下载_native2ascii.exe
- Java递归详解_java难不难学
- JAVA程序员简历模板_Java工程师简历模板
- java并发编程(1):Java多线程-基本线程类-基础知识复习笔记
- java实现文件下载详解编程语言
- Java与MySQL的实现协同(java和mysql)
- Java连接MySQL数据库的简单步骤(java如何连接mysql数据库)
- 实现Linux上的Java环境:从安装到配置(linux安装java环境)
- 数据库写入Java实现MySQL数据库的写入操作(java向mysql)
- 使用Java实现Redis数据存储(redis集成java)
- Java中使用Redis包实现高效缓存(redis包java)
- Java迭代Oracle实现数据库更高性能(java迭代oracle)
- 语句Java自动生成Oracle数据库查询语句(java生成oracle)
- 并发Redis锁保障Java并发性(redis锁实现java)