主要是一些流操作吧,之前用得少,这次写了就先保存起来,代码如下:

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下载图片相关推荐

  1. 使用HttpClient下载图片常用代码,以及下载失败原因

    最近一直在完善云相册项目,寻找各种bug,昨天突然发现,图片上传经常出现错误,错误有可能与网速有关,但是这种错误却是致命的,我们不能保证所有人的网速都很稳定,于是我就仔细看了下代码,发现异常都是在此处 ...

  2. java 下载图片流_java下载图片(通用)httpClient,io流

    httpClient下载图片 public static void downImage(CloseableHttpClient client, String imgUrl, String savePa ...

  3. 安卓案例:基于HttpClient下载文本与图片

    安卓案例:基于HttpClient下载文本与图片 一.利用HttpClient访问网络资源 1.创建http请求(get方式.post方式)  2.创建http客户端 

  4. java中批量下载图片(httpClient)

    如题. java中使用httpClient批量下载图片,代码如下: import java.io.File; import java.io.IOException; import java.io.In ...

  5. Android开发之根据Service的生命周期特点后台连接网络下载图片(源代码分享)

    上一章讲到Service在后台启动后不会自动销毁掉,其销毁的方式有两种一个是在外部使用stopService()方法,一个就是在继承Service的类下调用stopSelf(),那么应该何时调用sto ...

  6. android+nutz后台如何上传和下载图片

    android+nutz后台如何上传和下载图片 发布于 588天前  作者 yummy222  428 次浏览  复制  上一个帖子  下一个帖子  标签: 无 最近在做一个基于android的app ...

  7. 网络请求以及网络请求下载图片的工具类 android开发java工具类

    2019独角兽企业重金招聘Python工程师标准>>> package cc.jiusan.www.utils;import org.apache.http.HttpEntity; ...

  8. java假设模拟请求重新启动路由器(网络爬虫经常使用),还有java怎样下载图片

    我们假设在公司或家里使用网络爬虫去抓取自己索要的一些数据的时候,经常对方的站点有defence机制,会给你的http请求返回500错误,仅仅要是同样IP就请求不到数据,这时候我们仅仅能去重新启动路由器 ...

  9. java访问https链接下载图片

    java访问https链接下载图片 一.通过maven引入https工具包 <dependency><groupId>org.apache.httpcomponents< ...

  10. java异步下载图片_CMS项目实现异步图片下载

    create table CMS_IMAGES ( id LONG PRIMARY KEY, --id wsrc varchar2(256),--远程图片地扯 lsrc varchar2(256),- ...

最新文章

  1. 构建富互联网应用程序监控工作流和流程(1)
  2. windows 程序设计_windows程序设计基础(第二章)——2.5 第一个代码实例
  3. jvm配置参数,查看大对象直接分配到老年代
  4. 嵌入式OS入门笔记-以RTX为案例:一.简介
  5. Autodesk Revit DB Link 中文理解
  6. python组件的react实现_【React源码解读】- 组件的实现
  7. Python Day10 MySQL 01
  8. pytorch中mask操作之torch.masked_select
  9. Leetcode怎么调试java代码,LeetCode–正则表达式匹配
  10. 新手如何快速学习单片机
  11. php 获取array keys,php数组函数序列之
  12. Python : Arrow、Pyarrow库、以及与Julia互读
  13. java工具类专利申请文档_Java工具类 (3)------WordUtils------利用Poi根据模板生成新的word文档...
  14. 阿里云商标注册价格和费用
  15. 古诗词学习-归园田居+牧童+凉州词·其一+马诗+逢雪宿芙蓉山主人+凉州词+别董大+枫桥夜泊+滁州西涧+渔歌子+塞下曲
  16. GeoServer style(sld)中文乱码解决方法
  17. javascript开发简易画板
  18. 记一次360众测仿真实战靶场考核WP
  19. SRAM/DRAM优缺点对比
  20. 查看表空间、schema和表空间下所有表相关理解

热门文章

  1. 蓝桥杯2019c语言b组试题,2020年7月B组C++蓝桥杯真题试水
  2. MY CSDB BLOG 第一篇
  3. 《代码整洁之道》第14章 逐步改进 的代码片段
  4. 最全面的Linux命令大全出炉了
  5. matlab数据变成一列数据,用MATLAB处理EXCEL中一列共100000个数据,请问如何将数据导入并将数据做正态曲线拟合...
  6. 在endnote中制作GB/T7714《文后参考文献著录规则》的输出格式 及 编辑Output Styles中特殊符号说明
  7. 华为ensp 交换机vlan配置
  8. Object C中文件后缀名
  9. xcode12 导入SwiftyJSON
  10. Python数据解析