背景:项目测试时需要学生的照片信息,目前有20张可用图片;数据库中的有1000多个学生信息。要将这20张图片用这些学生的学号重命名。
解决方案如下:
借用考生的分页查询接口,获取kaoshengList, 通过refactorPC(kaoShengList)将这批图片重命名。
package com.zr.controller.dayin;
import com.zr.common.annotation.Log;
import com.zr.constant.SysCode;
import com.zr.model.KaoSheng;
import com.zr.service.ChengShiService;
import com.zr.service.TxmService;
import com.zr.utils.ResultMap;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.*;
import java.util.List;
@Api(tags = "图片重命名")
@RequestMapping("/tp")
@RestController
public class TP {
@Autowired
TxmService txmService;
@Autowired
MongoTemplate mongoTemplate;
@Autowired
ChengShiService chengShiService;
/**
* 获得所有需要打印条形码的考生信息的接口
*
* @param page 页码
* @param limit 数量
* @param ksno 考生号
* @param zjno 身份证号
* @param bmxh 报名序号
* @param xm 姓名
* @param xuexiao 学校
* @param xb 性别
* @param printStatus 打印状态
* @param qsbjbh 班级编号起始
* @param zzbjbh 班级编号终止
* @param qsbmxh 报名序号起始
* @param zzbmxh 报名序号终止
* @return
*/
@ApiOperation(value = "获得所有需要打印条形码的考生信息的接口")
@ApiImplicitParams({
@ApiImplicitParam(name = "page", value = "页码(大于0的整数)", required = true, dataType = "int", paramType = "query"),
@ApiImplicitParam(name = "limit", value = "数量(大于0的整数)", required = true, dataType = "int", paramType = "query"),
@ApiImplicitParam(name = "ksno", value = "考生号", required = false, dataType = "string", paramType = "query"),
@ApiImplicitParam(name = "zjno", value = "身份证号", required = false, dataType = "string", paramType = "query"),
@ApiImplicitParam(name = "bmxh", value = "报名序号", required = false, dataType = "string", paramType = "query"),
@ApiImplicitParam(name = "xm", value = "姓名", required = false, dataType = "string", paramType = "query"),
@ApiImplicitParam(name = "xuexiao", value = "学校", required = false, dataType = "string", paramType = "query"),
@ApiImplicitParam(name = "xb", value = "性别", required = false, dataType = "string", paramType = "query"),
@ApiImplicitParam(name = "printStatus", value = "打印状态", required = false, dataType = "string", paramType = "query"),
@ApiImplicitParam(name = "qsbjbh", value = "班级编号起始", required = false, dataType = "string", paramType = "query"),
@ApiImplicitParam(name = "zzbjbh", value = "班级编号终止", required = false, dataType = "string", paramType = "query"),
@ApiImplicitParam(name = "qsbmxh", value = "报名序号起始", required = false, dataType = "string", paramType = "query"),
@ApiImplicitParam(name = "zzbmxh", value = "报名序号终止", required = false, dataType = "string", paramType = "query")})
@GetMapping("/getAllByPage.do")
@Log(modelName = "条形码", action = "获得所有需要打印条形码的考生信息", description = "获得所有需要打印条形码的考生信息的接口")
public ResultMap getAllByPage(Integer page, Integer limit, String ksno, String zjno, String bmxh, String xm,
String xuexiao, String xb, String printStatus, String qsbjbh, String zzbjbh,
String qsbmxh, String zzbmxh) {
Map param = new HashMap();
param.put("startnum", (page - 1) * limit);
param.put("endnum", page * limit);
param.put("ksno", ksno);
param.put("zjno", zjno);
param.put("bmxh", bmxh);
param.put("xm", xm);
param.put("xuexiao", xuexiao);
param.put("xb", xb);
param.put("printStatus", printStatus);
param.put("qsbjbh", qsbjbh);
param.put("zzbjbh", zzbjbh);
param.put("qsbmxh", qsbmxh);
param.put("zzbmxh", zzbmxh);
try {
List kaoshengList = txmService.txm_getAllByPage(param);
String path="D:\\picture\\pic\\to_z\\29\\2901";
List filelist=traverseFolder1(path);
refactorPC(kaoshengList,filelist);
int count = txmService.txm_getAllByPage_count(param);
boolean isNext = page * limit < count ? true : false;
return new ResultMap>(SysCode.successCode, "查询成功", kaoshengList, count, page, limit, isNext);
} catch (Exception e) {
e.printStackTrace();
return new ResultMap(SysCode.errorCode, "查询失败");
}
}
public static void refactorPC(List list,List files) {
try {
//如果学生的数量大于需要重命名的照片的数量,将所有图片全部加载到缓存中;
//反之,只需要加载和学生数量相等的图片
int picCount=list.size()>files.size()?files.size():list.size();
Image[] array = new Image[picCount];
for(int i=0;i traverseFolder1(String path){
List fileList = new LinkedList();
int fileNum = 0;int folderNum = 0;
File file = new File(path);
if (file.exists()) {
LinkedList list = new LinkedList();
File[] files = file.listFiles();
for (File file2 : files) {
if (file2.isDirectory()) {
System.out.println("文件夹1:" + file2.getAbsolutePath());
list.add(file2);
folderNum++;
} else {
fileList.add(file2);
String filePath=file2.getAbsolutePath();
System.out.println("文件1:" + filePath);
fileNum++;
}
}
File temp_file;
while (!list.isEmpty()) {
temp_file = list.removeFirst();
files = temp_file.listFiles();
for (File file2 : files) {
if (file2.isDirectory()) {
System.out.println("文件夹:" + file2.getAbsolutePath());
list.add(file2);
folderNum++;
} else {
System.out.println("文件:" + file2.getAbsolutePath());
fileNum++;
}
}
}
} else {
System.out.println("文件不存在!");
}
System.out.println("文件夹共有:" + folderNum + ",文件共有:" + fileNum);
return fileList;
}
public static void main(String[] args) {
// String path="D:\\picture\\pic\\to_z\\29\\2901";
// traverseFolder1(path);
}
}
结果如下:
结果图片: