导出图片压缩包
@RequiresPermissions("face:image:export")
@Log(title = "图片信息", businessType = BusinessType.EXPORT)
@PostMapping("/export")
public void export(HttpServletResponse response, FaceImage faceImage)
{
    List<FaceImage> list = faceImageService.selectFaceImageList(faceImage);
    ZipOutputStream zipOutputStream = null;
    try {
        zipOutputStream = new ZipOutputStream(response.getOutputStream());
        for (FaceImage image: list) {
            String pictureUrl=image.getImgs();
            String fileName = pictureUrl.substring(pictureUrl.lastIndexOf("/") + 1);
            zipOutputStream.putNextEntry(new ZipEntry(fileName));

            URL url = new URL(pictureUrl);
            InputStream inputStream = new DataInputStream(url.openStream());

            byte[] buff = new byte[1024];
            int len;
            while ((len = inputStream.read(buff)) != -1) {
                zipOutputStream.write(buff, 0, len);
            }
            zipOutputStream.closeEntry();
            inputStream.close();
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        if (zipOutputStream != null) {
            try {
                zipOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

导入图片压缩包入口

@ApiOperation(value = "导入excel和图片zip包", notes = "导入excel和图片zip包")
@PostMapping("/import")
public AjaxResult importExcelPic(MultipartFile file, boolean updateSupport) throws Exception {

    StringBuffer sb = faceStudentService.importPicture(file);
    if (sb!= null&&sb.length()>0) {
        return AjaxResult.error(sb.toString());
    }else{
        return AjaxResult.success();
    }
}

导入图片压缩包具体方法

@Override
public StringBuffer importPicture(MultipartFile picture) throws Exception {
    String qrCodeZIPName = UUID.randomUUID() + ".zip";
    StringBuffer sb = new StringBuffer();
    File file= FileUtils.byte2File(picture.getBytes(), excelShortPath,qrCodeZIPName);
    String s1=file.getName();
    String prefix = qrCodeZIPName.substring(qrCodeZIPName.lastIndexOf("."));
    int num = prefix.length();//得到后缀名长度
    String s2 = s1.substring(0, s1.length() - num);//得到文件名。去掉了后缀
    List<FaceStudent> getOriginalPic = ZipUtil.getOriginalPic(excelShortPath + s1, excelShortPath + s2);
    List<FaceStudent> peopleDtoList =new ArrayList<>();
    List<String> errorList = new ArrayList<String>();
    List<String> existCode = new ArrayList<String>();
    for (int j = 0; j < getOriginalPic.size(); j++) {
        String fileName = getOriginalPic.get(j).getImgs();
        String fix = fileName.substring(fileName.lastIndexOf("."));
        int number = fix.length();//得到后缀名长度
        String fileOtherName = fileName.substring(0, fileName.length() - number);//得到文件名。去掉了后缀
        //根据文件名查找学工信息
        FaceStudent student=faceStudentMapper.selectFaceStudentByStuId(fileOtherName);
        if (student!=null) {
            //这里是存进数据库的url
            MultipartFile files=PictureUtils.getMultipartFile(getOriginalPic.get(j).getFile());
            R<SysFile> fileNames =remoteFileService.uploadMinio(files);
            student.setImgs(fileNames.getData().getUrl());
            if(StringUtils.isEmpty(fileNames.getData().getUrl())){
                errorList.add(fileOtherName);
            }else{
                peopleDtoList.add(student);
            }
        }else{
            existCode.add(fileOtherName);
        }
    }

    if (existCode != null && existCode.size() > 0) {
        sb.append("学号为 " + existCode + " 的学号不存在数据库。");
    }
    if (errorList != null && errorList.size() > 0) {
        sb.append("学号为 " + errorList + " 的图片不存在或没有人脸。");
    }
    List<String> errorString = batchAdd(peopleDtoList);
    if (errorString.size() == 0) {
        return sb;
    } else {
        sb.append("失败学号列表:" + errorString);
    }
    return sb;
}
private List<String> batchAdd(List<FaceStudent> peopleDtoList) {
    List<String> errorString = new LinkedList<String>();
    if (peopleDtoList.size() > 0 && peopleDtoList != null) {
        for (FaceStudent faceStudent : peopleDtoList) {
            faceStudentMapper.updateFaceStudent(faceStudent);
            FaceImage faceImage=new FaceImage();
            faceImage.setImgs(faceStudent.getImgs());
            faceImage.setStuId(faceStudent.getStuId());
            faceImage.setLabel("0");//工作照
            faceImageMapper.insertFaceImage(faceImage);
            //这里可以判断数据库是否有重叠数据,和一些具体业务
        }
    }
    return errorString;
}
Logo

快速构建 Web 应用程序

更多推荐