HttpClient下载图片
主要是一些流操作吧,之前用得少,这次写了就先保存起来,代码如下:
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Queue;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.LinkedBlockingQueue;import org.apache.http.HttpEntity;
import org.apache.http.TruncatedChunkException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.apache.poi.xssf.usermodel.XSSFCell;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;import com.alibaba.fastjson.JSONObject;/*** 图片下载* @author xiaowei 2018年3月5日 下午1:42:36*/
public class DownloadPicH5 {private static final String CERT_URL = "http://***/cifCert/queryCifCert";private static final String PIC_URL = "http://***/show?uniqueKey=";private static final String RESOURCE_PATH = "/home/dev/***.xlsx";private static final String ERROR_PATH = "/home/dev/error.txt";private static final String EXAMPLE_PIC_PATH = "/home/dev/example.jpg";private static final String PIC_POSTFIX = ".jpg";private static final String PIC_FACE = "正面" + PIC_POSTFIX;private static final String PIC_BACK = "反面" + PIC_POSTFIX;private static final String PIC_HAND = "手持照" + PIC_POSTFIX;private static List<Cust> errorList = new ArrayList<>();static byte[] bytes = new byte[1024];// 预加载样例图片static {try {InputStream in = new FileInputStream(new File(EXAMPLE_PIC_PATH));in.read(bytes);in.close();} catch (IOException e) {e.printStackTrace();}}public static void main(String[] args) throws Exception {LinkedBlockingQueue<Cust> queue = new LinkedBlockingQueue<>(230000);// ArrayBlockingQueue 为有界队列// ArrayBlockingQueue<Cust> queue = new ArrayBlockingQueue<>(230000);// List<Cust> list = new ArrayList<>(5000);FileInputStream excelFileInputStream = new FileInputStream(RESOURCE_PATH);// XSSFWorkbook 就代表一个 Excel 文件// 创建其对象,就打开这个 Excel 文件XSSFWorkbook workbook = new XSSFWorkbook(excelFileInputStream);// 输入流使用后,及时关闭!这是文件流操作中极好的一个习惯!excelFileInputStream.close();// XSSFSheet 代表 Excel 文件中的一张表格// 我们通过 getSheetAt(0) 指定表格索引来获取对应表格// 注意表格索引从 0 开始!XSSFSheet sheet = workbook.getSheetAt(0);// 开始循环表格数据,表格的行索引从 0 开始// employees.xlsx 第一行是标题行,我们从第二行开始, 对应的行索引是 1// sheet.getLastRowNum() : 获取当前表格中最后一行数据对应的行索引String str;for (int rowIndex = 0; rowIndex <= sheet.getLastRowNum(); rowIndex++) {// XSSFRow 代表一行数据XSSFRow row = sheet.getRow(rowIndex);if (row == null) {continue;}XSSFCell nameCell = row.getCell(0);str = nameCell.getStringCellValue();// queue.add(new Cust(str, str.substring(str.length() - 19, str.length())));queue.put(new Cust(str, str.substring(str.length() - 19, str.length())));}// 操作完毕后,记得要将打开的 XSSFWorkbook 关闭workbook.close();final CountDownLatch latch = new CountDownLatch(128);for (int i = 0; i < 128; i++) {new Thread(new Runnable() {public void run() {while (!queue.isEmpty()) {// 保存图片getPicInfo(queue);}latch.countDown();}}).start();}latch.await();System.out.println("SUCCESS");// 保存失败记录if (!errorList.isEmpty()) {String error = JSONObject.toJSONString(errorList);File tempFile = new File(ERROR_PATH);FileOutputStream outStream = new FileOutputStream(tempFile);outStream.write(error.getBytes());outStream.close();}}public static byte[] readInputStream(InputStream inStream) throws Exception {ByteArrayOutputStream outStream = new ByteArrayOutputStream();//创建一个Buffer字符串 byte[] buffer = new byte[1024];//每次读取的字符串长度,如果为-1,代表全部读取完毕 int len = 0;//使用一个输入流从buffer里把数据读取出来 while ((len = inStream.read(buffer)) != -1) {//用输出流往buffer里写入数据,中间参数代表从哪个位置开始读,len代表读取的长度 outStream.write(buffer, 0, len);}//关闭输入流 inStream.close();//把outStream里的数据写入内存 return outStream.toByteArray();}/*** 获取图片相关信息*/public static void getPicInfo(Queue<Cust> queue) {Cust cust = queue.poll();String path = "/home/dev/h5/" + cust.resNo + "/";// 创建文件夹File file = new File(path);if (!file.exists()) {file.mkdirs();}// // 由于前期调试程序,所以存在重新下载文件的情况// // 当三个文件均存在时,跳过该客户// String pathIgnore = path + PIC_FACE;// File imageFile = new File(pathIgnore);// boolean fileExist = imageFile.exists();// if (fileExist) {// pathIgnore = path + PIC_BACK;// imageFile = new File(pathIgnore);// fileExist = imageFile.exists();// if (fileExist) {// pathIgnore = path + PIC_HAND;// imageFile = new File(pathIgnore);// fileExist = imageFile.exists();// if (fileExist)// return;// }//// }try {// 1、get cert fileNoString response = null;CloseableHttpClient httpClient = HttpClients.createDefault();HttpPost post = new HttpPost(CERT_URL);StringEntity entity = new StringEntity(new Req(cust.custNo).toString(), "UTF-8");entity.setContentType("application/json");post.setEntity(entity);CloseableHttpResponse httpResponse = httpClient.execute(post);HttpEntity httpEntity = httpResponse.getEntity();response = EntityUtils.toString(httpEntity, "UTF-8");List<Cert> certList = JSONObject.parseObject(response).getJSONArray("respData").toJavaList(Cert.class);// 2、 download fileboolean type1Exist = false;boolean type2Exist = false;boolean type5Exist = false;for (Cert cert : certList) {HttpGet get = new HttpGet(PIC_URL + cert.fileNo);CloseableHttpResponse respose = null;switch (cert.certType) {case "1":download(httpClient, get, respose, path, PIC_FACE, cust);type1Exist = true;break;case "2":download(httpClient, get, respose, path, PIC_BACK, cust);type2Exist = true;break;case "5":download(httpClient, get, respose, path, PIC_HAND, cust);type5Exist = true;break;default:break;}}if (!type1Exist) {File tempFile = new File(path + PIC_FACE);FileOutputStream outStream = new FileOutputStream(tempFile);outStream.write(bytes);outStream.close();}if (!type2Exist) {File tempFile = new File(path + PIC_BACK);FileOutputStream outStream = new FileOutputStream(tempFile);outStream.write(bytes);outStream.close();}if (!type5Exist) {File tempFile = new File(path + PIC_HAND);FileOutputStream outStream = new FileOutputStream(tempFile);outStream.write(bytes);outStream.close();}} catch (Exception e) {errorDo(cust);}}/*** 真正下载并保存图片的地方*/public static void download(CloseableHttpClient httpClient, HttpGet get,CloseableHttpResponse response, String path, String name, Cust cust) {// 文件已存在,跳过下载File imageFile = new File(path + name);if (imageFile.exists()) {return;}try {response = httpClient.execute(get);} catch (Exception e) {e.printStackTrace();}HttpEntity entity = response.getEntity();try {InputStream instream = entity.getContent();byte[] data = readInputStream(instream);//创建输出流 FileOutputStream outStream = new FileOutputStream(imageFile);//写入数据 outStream.write(data);//关闭输出流 outStream.close();} catch (TruncatedChunkException te) {errorDo(cust);} catch (Exception e) {e.printStackTrace();}}/*** 网络错误等下载异常时,调用该方法重试下载数次* @description 此处代码是复制getPicInfo方法,可以提取并优化,暂不做处理了。*/public static void errorDo(Cust cust) {// 创建文件夹String path = "/home/dev/h5/" + cust.resNo + "/";File file = new File(path);if (!file.exists()) {file.mkdirs();}for (int i = 0; i < 3; i++) {try {// 1、get cert fileNoString response = null;CloseableHttpClient httpClient = HttpClients.createDefault();HttpPost post = new HttpPost(CERT_URL);StringEntity entity = new StringEntity(new Req(cust.custNo).toString(), "UTF-8");entity.setContentType("application/json");post.setEntity(entity);CloseableHttpResponse httpResponse = httpClient.execute(post);HttpEntity httpEntity = httpResponse.getEntity();response = EntityUtils.toString(httpEntity, "UTF-8");List<Cert> certList = JSONObject.parseObject(response).getJSONArray("respData").toJavaList(Cert.class);// 2、 download fileboolean type1Exist = false;boolean type2Exist = false;boolean type5Exist = false;for (Cert cert : certList) {HttpGet get = new HttpGet(PIC_URL + cert.fileNo);CloseableHttpResponse respose = null;switch (cert.certType) {case "1":download(httpClient, get, respose, path, PIC_FACE, cust);type1Exist = true;break;case "2":download(httpClient, get, respose, path, PIC_BACK, cust);type2Exist = true;break;case "5":download(httpClient, get, respose, path, PIC_HAND, cust);type5Exist = true;break;default:break;}}if (!type1Exist) {File tempFile = new File(path + PIC_FACE);FileOutputStream outStream = new FileOutputStream(tempFile);outStream.write(bytes);outStream.close();}if (!type2Exist) {File tempFile = new File(path + PIC_BACK);FileOutputStream outStream = new FileOutputStream(tempFile);outStream.write(bytes);outStream.close();}if (!type5Exist) {File tempFile = new File(path + PIC_HAND);FileOutputStream outStream = new FileOutputStream(tempFile);outStream.write(bytes);outStream.close();}return;} catch (Exception e) {e.printStackTrace();}}errorList.add(cust);}
}/*** 客户类,保存客户的resNo、custNo* @author xiaowei 2018年3月5日 上午11:38:10*/
class Cust {String resNo;String custNo;public Cust(String resNo, String custNo) {this.resNo = resNo;this.custNo = custNo;}@Overridepublic String toString() {return "Cust [resNo=" + resNo + ", custNo=" + custNo + "]";}}/*** 用来包装作为http请求对象,主要功能是toString方法。* @description 可用其他方法实现* @author xiaowei 2018年3月5日 上午11:40:02*/
class Req implements Serializable {private static final long serialVersionUID = 1L;String customerNo;public Req(String customerNo) {this.customerNo = customerNo;}public String getCustomerNo() {return customerNo;}public void setCustomerNo(String customerNo) {this.customerNo = customerNo;}@Overridepublic String toString() {return JSONObject.toJSONString(this);}
}/*** 身份信息类,用来接收客户的文件信息* @author xiaowei 2018年3月5日 上午11:41:29*/
class Cert {String id;String customerNo;String customerName;String certType;String fileNo;String fileDesc;public String getId() {return id;}public void setId(String id) {this.id = id;}public String getCustomerNo() {return customerNo;}public void setCustomerNo(String customerNo) {this.customerNo = customerNo;}public String getCustomerName() {return customerName;}public void setCustomerName(String customerName) {this.customerName = customerName;}public String getCertType() {return certType;}public void setCertType(String certType) {this.certType = certType;}public String getFileNo() {return fileNo;}public void setFileNo(String fileNo) {this.fileNo = fileNo;}public String getFileDesc() {return fileDesc;}public void setFileDesc(String fileDesc) {this.fileDesc = fileDesc;}@Overridepublic String toString() {return "Cert [id=" + id + ", customerNo=" + customerNo + ", customerName=" + customerName+ ", certType=" + certType + ", fileNo=" + fileNo + ", fileDesc=" + fileDesc + "]";}}
HttpClient下载图片相关推荐
- 使用HttpClient下载图片常用代码,以及下载失败原因
最近一直在完善云相册项目,寻找各种bug,昨天突然发现,图片上传经常出现错误,错误有可能与网速有关,但是这种错误却是致命的,我们不能保证所有人的网速都很稳定,于是我就仔细看了下代码,发现异常都是在此处 ...
- java 下载图片流_java下载图片(通用)httpClient,io流
httpClient下载图片 public static void downImage(CloseableHttpClient client, String imgUrl, String savePa ...
- 安卓案例:基于HttpClient下载文本与图片
安卓案例:基于HttpClient下载文本与图片 一.利用HttpClient访问网络资源 1.创建http请求(get方式.post方式) 2.创建http客户端
- java中批量下载图片(httpClient)
如题. java中使用httpClient批量下载图片,代码如下: import java.io.File; import java.io.IOException; import java.io.In ...
- Android开发之根据Service的生命周期特点后台连接网络下载图片(源代码分享)
上一章讲到Service在后台启动后不会自动销毁掉,其销毁的方式有两种一个是在外部使用stopService()方法,一个就是在继承Service的类下调用stopSelf(),那么应该何时调用sto ...
- android+nutz后台如何上传和下载图片
android+nutz后台如何上传和下载图片 发布于 588天前 作者 yummy222 428 次浏览 复制 上一个帖子 下一个帖子 标签: 无 最近在做一个基于android的app ...
- 网络请求以及网络请求下载图片的工具类 android开发java工具类
2019独角兽企业重金招聘Python工程师标准>>> package cc.jiusan.www.utils;import org.apache.http.HttpEntity; ...
- java假设模拟请求重新启动路由器(网络爬虫经常使用),还有java怎样下载图片
我们假设在公司或家里使用网络爬虫去抓取自己索要的一些数据的时候,经常对方的站点有defence机制,会给你的http请求返回500错误,仅仅要是同样IP就请求不到数据,这时候我们仅仅能去重新启动路由器 ...
- java访问https链接下载图片
java访问https链接下载图片 一.通过maven引入https工具包 <dependency><groupId>org.apache.httpcomponents< ...
- java异步下载图片_CMS项目实现异步图片下载
create table CMS_IMAGES ( id LONG PRIMARY KEY, --id wsrc varchar2(256),--远程图片地扯 lsrc varchar2(256),- ...
最新文章
- 操作系统结构-外核结构
- Json对象与Json字符串的转化、JSON字符串与Java对象的转换
- hikaridatasource 加密后登陆不上_渗透测试 | 突破前端JS加密限制
- 【Java13】cookiesession(登陆案例(2)),jsp(登录案例(3))
- 设置二进制或者16/32位的某一位的值
- Python字符串| isdigit()方法与示例
- python函数的目的与意义_Python函数__new__及__init__作用及区别解析
- Centos6.4系统局域网服务之DHCP
- android 商米扫码sdk,商米收银机|外接扫码枪问题:以T2为例
- adprw指令通讯案例_PLC编程入门,编写控制程序必掌握选型指令
- python绘制中国_用Python画中国地图(下)
- 给大家推荐几本JAVA相关书籍
- 概念模型,逻辑模型,物理模型
- 【Linux】文件的编码语系转换与换行符转换
- 【Python】司徒卢威函数
- 51Nod1367 完美森林 贪心
- dtree做权限控制
- 小白避坑系列--Ubuntu1804 RTX3060显卡实现双屏
- 从0开始学java_从0开始学java-变量
- :linux内核编译过程的最终总结版
热门文章
- java软件工程师工作业绩_JAVA软件工程师个人简历范文
- 兄弟9150cdn换硒鼓清零抹粉_兄弟打印机粉盒硒鼓怎么清零?这里有详细的步骤...
- 高响应比优先调度算法以及其优缺点
- 例题:最高响应比优先调度算法
- arcgis xml 下载 切片_如何下载谷歌地球影像的 ArcGIS Server 缓存切片(瓦片)
- python版代码整洁之道
- linux vi 替换字符串,vi字符串替换命令
- 李宏毅机器学习笔记——深度学习
- 2022美赛M奖经验总结(1)分工职责篇(附论文)
- dmx512如何帧同步_DMX512协议的前世今生 数据格式及传输简介