根据图片地址判断图片是否有效
需求
项目调用第三方接口获得很多图片信息,需要对这些图片信息进行处理,图片无效的生成excel表格反馈给第三方
解决方案
1.新建spring项目,目录结构如下
2.具体业务代码如下
@Value("${demo.savePath}")
public String savePath;
@Value("${demo.table-name}")
private String tableName;
@Value("${demo.condition}")
private String condition;
@Autowired
JdbcTemplate jdbcTemplate;
List<Map> imgList = new ArrayList();
@Override
public void checkImage(String[] args) {
//判断图片是否有效
try {
//从数据库取得数据,tableName为配置表名
String sql = "select * from " + tableName + " where 1=1 " + condition + " ";
log.info("sql=" + sql);
List<Map<String, Object>> imgResult = jdbcTemplate.queryForList(sql);
log.info("图片共" + imgResult.size() + "张");
//判断图片是否有效
threadDealData(imgResult);
//无效的生成excel
list2Excel(imgList);
} catch (Exception e) {
e.printStackTrace();
}
}
/*
* 线程判断图片是否存在
* */
public void threadDealData(List<Map<String, Object>> imgResult) {
long start = System.currentTimeMillis();
// 每500条数据开启一条线程
int threadSize = 500;
// 总数据条数
int dataSize = imgResult.size();
// 线程数
int threadNum = dataSize / threadSize + 1;
// 定义标记,过滤threadNum为整数
boolean special = dataSize % threadSize == 0;
// 创建一个线程池
ExecutorService exec = Executors.newFixedThreadPool(threadNum);
// 定义一个任务集合
List<Callable<Integer>> tasks = new ArrayList<>();
Callable<Integer> task = null;
List<Map<String, Object>> cutList = null;
// 确定每条线程的数据
for (int i = 0; i < threadNum; i++) {
if (i == threadNum - 1) {
if (special) {
break;
}
cutList = imgResult.subList(threadSize * i, dataSize);
} else {
cutList = imgResult.subList(threadSize * i, threadSize * (i + 1));
}
final List<Map<String, Object>> listStr = cutList;
task = new Callable<Integer>() {
@Override
public Integer call() throws Exception {
for (Map<String, Object> item : listStr) {
Map out = new HashMap();
try {
String imgUrl = (String) item.get("p_picurl");
String bId = String.valueOf(item.get("b_id"));
String picTime = String.valueOf(item.get("p_pictime"));
// log.info(imgUrl);
if (ObjectUtils.isEmpty(imgUrl)) {
out.put(“imgUrl”, imgUrl);
out.put(“bId”, bId);
out.put(“picTime”, picTime);
imgList.add(out);
} else {
boolean isExist = new UrlResource(imgUrl).exists();
if (!isExist) {
out.put(“imgUrl”, imgUrl);
out.put(“bId”, bId);
out.put(“picTime”, picTime);
imgList.add(out);
log.info(“问题图片地址:” + imgUrl);
}
}
} catch (Exception e) {
log.error(e.getMessage());
}
}
return 1;
}
};
// 这里提交的任务容器列表和返回的Future列表存在顺序对应的关系
tasks.add(task);
}
try {
List<Future> results = exec.invokeAll(tasks);
} catch (InterruptedException e) {
log.error(e.getMessage());
}
// 关闭线程池
exec.shutdown();
log.info(“执行任务结束, 消耗了 :” + (System.currentTimeMillis() - start) + “毫秒”);
}
public String list2Excel(List<Map> imgList) throws Exception {
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
SimpleDateFormat sdf1 = new SimpleDateFormat("yyyyMMddhhmmss");
Workbook wb = new XSSFWorkbook();
//标题行抽出字段
String[] title = {"序号", "问题图片地址", "批次号", "拍照时间", "记录时间"};
//设置sheet名称,并创建新的sheet对象
String sheetName = "问题图片汇总";
Sheet stuSheet = wb.createSheet(sheetName);
//获取表头行
Row titleRow = stuSheet.createRow(0);
//创建单元格,设置style居中,字体,单元格大小等
CellStyle style = wb.createCellStyle();
Cell cell = null;
//把已经写好的标题行写入excel文件中
for (int i = 0; i < title.length; i++) {
cell = titleRow.createCell(i);
cell.setCellValue(title[i]);
cell.setCellStyle(style);
}
//把从数据库中取得的数据一一写入excel文件中
Row row = null;
for (int i = 0; i < imgList.size(); i++) {
//创建list.size()行数据
row = stuSheet.createRow(i + 1);
//把值一一写进单元格里
//设置第一列为自动递增的序号
row.createCell(0).setCellValue(i + 1);
row.createCell(1).setCellValue(String.valueOf(imgList.get(i).get("imgUrl")));
row.createCell(2).setCellValue(String.valueOf(imgList.get(i).get("bId")));
row.createCell(3).setCellValue(String.valueOf(imgList.get(i).get("picTime")));
row.createCell(4).setCellValue(sdf.format(new Date()));
}
//设置单元格宽度自适应,在此基础上把宽度调至1.5倍
for (int i = 0; i < title.length; i++) {
stuSheet.autoSizeColumn(i, true);
stuSheet.setColumnWidth(i, stuSheet.getColumnWidth(i) * 15 / 10);
}
//获取配置文件中保存对应excel文件的路径,本地也可以直接写成F:excel/stuInfoExcel路径
String folderPath = savePath;
//创建上传文件目录
File folder = new File(folderPath);
//如果文件夹不存在创建对应的文件夹
if (!folder.exists()) {
folder.mkdirs();
}
//设置文件名
String fileName = sdf1.format(new java.util.Date()) + sheetName + ".xlsx";
String filePath = folderPath + File.separator + fileName;
OutputStream fileOut = new FileOutputStream(filePath);
wb.write(fileOut);
fileOut.close();
//返回文件保存全路径
log.info("文件保存位置为:" + filePath + ",请查看");
return filePath;
}