package com.huimv.manager.controller; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; import com.huimv.manager.dao.entity.ImagePathEntity; import com.huimv.manager.service.IImageService; import com.huimv.manager.utils.DateUtil; import com.huimv.manager.utils.ImageByteUtil; //import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer; import com.huimv.manager.utils.Result; import com.huimv.manager.utils.ResultCode; import lombok.extern.slf4j.Slf4j; import org.apache.commons.compress.archivers.ArchiveException; import org.apache.commons.compress.archivers.ArchiveOutputStream; import org.apache.commons.compress.archivers.ArchiveStreamFactory; import org.apache.commons.compress.archivers.zip.ZipArchiveEntry; import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream; //import org.apache.tomcat.util.http.fileupload.FileUtils; import org.apache.commons.io.FileUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.MediaType; import org.springframework.web.bind.annotation.*; import javax.imageio.ImageIO; import javax.servlet.ServletOutputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.awt.image.BufferedImage; import java.io.*; //import java.sql.Timestamp; import java.util.Date; import java.util.List; /** * @Project : huimv.shiwan * @Package : com.huimv.biosafety.uface.controller * @Description : TODO * @Version : 1.0 * @Author : ZhuoNing * @Create : 2020-12-25 **/ @Slf4j @CrossOrigin @RestController @RequestMapping("/imageManager") public class ImageManagerController { @Autowired private IImageService imageService; @Autowired private DateUtil dateUtil; protected HttpServletResponse response; /** * @Method : getLastImageAiResult * @Description : 读取最新的图像AI识别记录 * @Params : [] * @Return : java.lang.String * @Author : ZhuoNing * @Date : 2021/1/22 * @Time : 10:00 */ @RequestMapping(value = "/getLastImageAiResult", method = RequestMethod.GET) public String getLastImageAiResult() { return JSONObject.toJSON(imageService.getLastImage()).toString(); } /** * @Method : getNextImageAiResult * @Description : 读取下一条图像AI识别记录 * @Params : [] * @Return : java.lang.String * @Author : ZhuoNing * @Date : 2021/1/22 * @Time : 10:01 */ @RequestMapping(value = "/getNextImageAiResult", method = RequestMethod.GET) public String getNextImageAiResult() { // return JSONObject.toJSON(imageService.getNextImage()).toString(); } /** * @Method : listImage * @Description : * @Params : [startDate, endDate, pageNo, pageSize] * @Return : com.alibaba.fastjson.JSONArray * @Author : ZhuoNing * @Date : 2021/1/22 * @Time : 16:28 */ @Deprecated @RequestMapping(value = "/listImage", method = RequestMethod.GET) public JSONObject listImage(@RequestParam(value = "startDate", required = true) String startDate, @RequestParam(value = "endDate", required = true) String endDate, @RequestParam(value = "catalog", required = false) String catalog, @RequestParam(value = "pageNo", required = true) int pageNo, @RequestParam(value = "pageSize", required = true) int pageSize) { log.info("startDate>>" + startDate); log.info("endDate>>" + endDate); log.info("catalog>>"+catalog); log.info("pageNo>>" + pageNo); log.info("pageSize>>" + pageSize); // 搜索记录时,catalog是非必要字段; // 导出图片时,catalog是必要字段; // List imageList = imageService.getImageList(startDate, endDate, pageNo, pageSize,catalog); JSONArray imageJa = new JSONArray(); for (ImagePathEntity imageEntity : imageList) { // JSONObject dataJo = (JSONObject) JSONObject.toJSON(imageEntity); JSONObject imageJo = new JSONObject(); imageJo.put("aiData", imageEntity.getAiData()); imageJo.put("aiResult", imageEntity.getAiResult()); imageJo.put("aiTimeUsed", imageEntity.getAiTimeUsed()); imageJo.put("createDate", dateUtil.formatTimestamp(imageEntity.getCreateDate())); imageJo.put("effective", imageEntity.getEffective()); imageJo.put("fileName", imageEntity.getFileName()); imageJo.put("id", imageEntity.getId()); imageJo.put("imgPath", imageEntity.getImgPath()); imageJo.put("name", imageEntity.getName()); imageJo.put("relativePath", imageEntity.getRelativePath()); imageJo.put("pigQuantity", imageEntity.getPigQuantity()); imageJa.add(imageJo); } // 读取记录数 int total = imageService.getTotal(startDate, endDate); JSONObject outJo = new JSONObject(); outJo.put("data", imageJa); outJo.put("total", total); return outJo; } /** * @Method : previousImage * @Description : 读取前一张 * @Params : [id] * @Return : com.alibaba.fastjson.JSONObject * @Author : ZhuoNing * @Date : 2021/1/23 * @Time : 19:09 */ @RequestMapping(value = "/getPreviousImage", method = RequestMethod.GET) public Result getPreviousImage(@RequestParam(value = "imageId", required = true) int imageId) { // ImagePathEntity imagePathEntity = imageService.getOneImage("previous", imageId); if(imagePathEntity == null){ return new Result(10001,"未查询到任何记录.",false); }else{ return new Result(ResultCode.SUCCESS,(JSONObject)JSONObject.toJSON(imagePathEntity)); } } /** * @Method : nextImage * @Description : 读取下一张图片 * @Params : [id] * @Return : com.alibaba.fastjson.JSONObject * @Author : ZhuoNing * @Date : 2021/1/23 * @Time : 19:09 */ @RequestMapping(value = "/getNextImage", method = RequestMethod.GET) public Result getNextImage(@RequestParam(value = "imageId", required = true) int imageId) { // ImagePathEntity imagePathEntity = imageService.getOneImage("next", imageId); if(imagePathEntity == null){ return new Result(10001,"未查询到任何记录.",false); }else{ return new Result(ResultCode.SUCCESS,(JSONObject)JSONObject.toJSON(imagePathEntity)); } } @Deprecated @RequestMapping("/exportPicture") public void exportPicture(HttpServletRequest request, HttpServletResponse response) throws Exception { //定义根路径 String rootPath = request.getRealPath("/"); //创建文件 File file = new File(rootPath + "temp_download"); //判断文件是否存在,如果不存在,则创建此文件夹 if (!file.exists()) { file.mkdir(); } String name = "图片压缩包下载"; String fileName = name + new Date().getTime(); String zipFileName = fileName + ".zip"; File zipFile = null; String path = rootPath + "temp_download"; //调用工具类获取图片 byte[] data = ImageByteUtil.image2byte("F:\\blank.jpg"); //new一个文件对象用来保存图片,默认保存当前工程根目录 if (data != null) { File imageFile = new File(path + File.separator + fileName + ".jpg"); //创建输出流 FileOutputStream outStream = new FileOutputStream(imageFile); //写入数据 outStream.write(data); //关闭输出流 outStream.close(); } try { /* * 用JDK自带的zipentry压缩后,压缩包中的文件名中文乱码,故放弃此种方法,使用commons-compress-1.6.jar包 * ZipEntry zipEntry = new ZipEntry(new * String(txtFile.getName().getBytes("GB2312"),"ISO-8859-1")); * zos.putNextEntry(zipEntry); ZipOutputStream zos = new * ZipOutputStream(zipFos); ZipEntry zipEntry = new ZipEntry(new * String(txtFile.getName().getBytes("GB2312"),"ISO-8859-1")); zos. * putNextEntry(zipEntry); * zos.write(FileUtils.readFileToByteArray(txtFile)); zos.flush(); * zos.close(); */ //获取创建好的图片文件 File imageFile = new File(path + "/" + fileName + ".jpg"); // 打成压缩包 zipFile = new File(path + "/" + zipFileName); FileOutputStream zipFos = new FileOutputStream(zipFile); ArchiveOutputStream archOut = new ArchiveStreamFactory().createArchiveOutputStream(ArchiveStreamFactory.ZIP, zipFos); if (archOut instanceof ZipArchiveOutputStream) { ZipArchiveOutputStream zos = (ZipArchiveOutputStream) archOut; ZipArchiveEntry zipEntry = new ZipArchiveEntry(imageFile, imageFile.getName()); zos.putArchiveEntry(zipEntry); zos.write(FileUtils.readFileToByteArray(imageFile)); zos.closeArchiveEntry(); zos.flush(); zos.close(); } // 压缩完删除txt文件 if (imageFile.exists()) { imageFile.delete(); } // 输出到客户端 OutputStream out = null; out = response.getOutputStream(); response.reset(); response.setHeader("Content-Disposition", "attachment;filename=" + new String(zipFileName.getBytes("GB2312"), "ISO-8859-1")); response.setContentType("application/octet-stream; charset=utf-8"); response.setCharacterEncoding("UTF-8"); out.write(FileUtils.readFileToByteArray(zipFile)); out.flush(); out.close(); // 输出客户端结束后,删除压缩包 if (zipFile.exists()) { zipFile.delete(); } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (ArchiveException e) { // TODO Auto-generated catch block e.printStackTrace(); } } /** * 压缩多个文件到一个zip压缩包中 * * @param request * @param response * @throws Exception * @description : * @date 2016-11-4 * @author liucong */ @RequestMapping("/zipMoreFiles") public void zipMoreFiles(HttpServletRequest request, HttpServletResponse response) throws Exception { //定义根路径 String rootPath = request.getRealPath("/"); //创建文件 File file = new File(rootPath + "temp_download"); //判断文件是否存在,如果不存在,则创建此文件夹 if (!file.exists()) { file.mkdir(); } String name = "图片压缩包下载"; String fileName = name + new Date().getTime(); String zipFileName = fileName + ".zip"; File zipFile = null; String path = rootPath + "temp_download"; //获取本地文件夹中图片集合,事先准备好图片 File files = new File("F:\\images\\"); //获取该文件夹中的图片文件数组 File[] filelist = files.listFiles(); // 打成压缩包 zipFile = new File(path + "/" + zipFileName); FileOutputStream zipFos = new FileOutputStream(zipFile); ArchiveOutputStream archOut = new ArchiveStreamFactory().createArchiveOutputStream(ArchiveStreamFactory.ZIP, zipFos); if (archOut instanceof ZipArchiveOutputStream) { ZipArchiveOutputStream zos = (ZipArchiveOutputStream) archOut; for (File imageFile : filelist) { ZipArchiveEntry zipEntry = new ZipArchiveEntry(imageFile, imageFile.getName()); zos.putArchiveEntry(zipEntry); zos.write(FileUtils.readFileToByteArray(imageFile)); zos.closeArchiveEntry(); zos.flush(); } zos.close(); } // 输出到客户端 OutputStream out = null; out = response.getOutputStream(); response.reset(); response.setHeader("Content-Disposition", "attachment;filename=" + new String(zipFileName.getBytes("GB2312"), "ISO-8859-1")); response.setContentType("application/octet-stream; charset=utf-8"); response.setCharacterEncoding("UTF-8"); out.write(FileUtils.readFileToByteArray(zipFile)); out.flush(); out.close(); // 输出客户端结束后,删除压缩包 if (zipFile.exists()) { zipFile.delete(); } } @RequestMapping("/exportImage") public void exportImage(HttpServletRequest request, HttpServletResponse response, @RequestParam(value = "startDate", required = false) String startDate, @RequestParam(value = "endDate", required = false) String endDate, @RequestParam(value = "imageIds", required = false) String imageIds) throws Exception { // if(imageIds == null || imageIds.trim().isEmpty()){ // log.error("输入参数[imageIds]不能为空."); // return ; // } log.info("输入参数[startDate]>>" + startDate); log.info("输入参数[endDate]>>" + endDate); log.info("输入参数[imageIds]>>" + imageIds); if (startDate == null && endDate == null && imageIds == null) { log.error("日期区间或指定图片导出二选一"); return; } // 读取图片文件 List imageList = imageService.getExportImageList(startDate, endDate, imageIds); if (imageList.size() == 0) { log.error("未读取到任何需要导出的图片."); return; } // System.out.println("imageList.size>>"+imageList.size()); int i = 0; File[] fileList = new File[imageList.size()]; for (ImagePathEntity imagePathEntity : imageList) { File imageFile = new File(imagePathEntity.getImgPath()); fileList[i++] = imageFile; } //定义根路径 String rootPath = request.getRealPath("/"); //创建文件 File file = new File(rootPath + "temp_download"); //判断文件是否存在,如果不存在,则创建此文件夹 if (!file.exists()) { file.mkdir(); } String name = "图片压缩包下载"; String fileName = name + new Date().getTime(); String zipFileName = fileName + ".zip"; File zipFile = null; String path = rootPath + "temp_download"; // 打成压缩包 zipFile = new File(path + "/" + zipFileName); FileOutputStream zipFos = new FileOutputStream(zipFile); ArchiveOutputStream archOut = new ArchiveStreamFactory().createArchiveOutputStream(ArchiveStreamFactory.ZIP, zipFos); if (archOut instanceof ZipArchiveOutputStream) { ZipArchiveOutputStream zos = (ZipArchiveOutputStream) archOut; for (File imageFile : fileList) { ZipArchiveEntry zipEntry = new ZipArchiveEntry(imageFile, imageFile.getName()); zos.putArchiveEntry(zipEntry); zos.write(FileUtils.readFileToByteArray(imageFile)); zos.closeArchiveEntry(); zos.flush(); } zos.close(); } // 输出到客户端 OutputStream out = null; out = response.getOutputStream(); response.reset(); response.setHeader("Content-Disposition", "attachment;filename=" + new String(zipFileName.getBytes("GB2312"), "ISO-8859-1")); response.setContentType("application/octet-stream; charset=utf-8"); response.setCharacterEncoding("UTF-8"); out.write(FileUtils.readFileToByteArray(zipFile)); out.flush(); out.close(); // 输出客户端结束后,删除压缩包 if (zipFile.exists()) { zipFile.delete(); } } /** * @Method : exportOneImage * @Description : 导出一张图片信息 * @Params : [imageId] * @Return : byte[] * @Author : ZhuoNing * @Date : 2021/1/23 * @Time : 19:53 */ @RequestMapping(value = "/exportOneImage", method = RequestMethod.GET, produces = MediaType.IMAGE_JPEG_VALUE) // @ResponseBody public void exportOneImage(HttpServletRequest request, HttpServletResponse response, @RequestParam(value = "imageId", required = true) String imageId) throws IOException { // 读取 ImagePathEntity imagePathEntity = imageService.getOneImageDetail(imageId); if (imagePathEntity == null) { log.error("imagePathEntity[" + imagePathEntity.toString() + "]==null"); } String imageName = imagePathEntity.getFileName(); String fileUrl = imagePathEntity.getImgPath(); File file = new File(fileUrl); if (file.exists()) { response.setContentType("application/force-download");// 设置强制下载不打开 response.addHeader("Content-Disposition", "attachment;fileName=" + imageName);// 设置文件名 byte[] buffer = new byte[1024]; FileInputStream fis = null; BufferedInputStream bis = null; try { fis = new FileInputStream(file); bis = new BufferedInputStream(fis); OutputStream os = response.getOutputStream(); int i = bis.read(buffer); while (i != -1) { os.write(buffer, 0, i); i = bis.read(buffer); } System.out.println("success"); } catch (Exception e) { e.printStackTrace(); } finally { if (bis != null) { try { bis.close(); } catch (IOException e) { e.printStackTrace(); } } if (fis != null) { try { fis.close(); } catch (IOException e) { e.printStackTrace(); } } } } } // @RequestMapping(value = "/exportOneImage",method = RequestMethod.GET,produces = MediaType.IMAGE_JPEG_VALUE) // @ResponseBody // public void exportOneImage(HttpServletRequest request,HttpServletResponse response,@RequestParam(value = "imageId",required = true) String imageId) throws IOException { // log.info("exportOneImage"); // // 读取 // ImagePathEntity imagePathEntity = imageService.getOneImageDetail(imageId); // System.out.println("imagePathEntity>>"+imagePathEntity); // if(imagePathEntity == null){ // log.error("imagePathEntity["+imagePathEntity.toString()+"]==null"); // } //// File file = new File(imagePathEntity.getImgPath()); //// File fileBase = new File("test.txt"); //// try { //// ServletOutputStream outputStream = response.getOutputStream(); //// FileInputStream inputStream = new FileInputStream(fileBase); //// response.setHeader("Content-Disposition", "attachment; filename=" + java.net.URLEncoder.encode("这里写用户下载时的文件名.txt", "UTF-8")); //// //注意, jdk8中还没有这个方法,使用jdk8的小伙伴要自己想办法复制流 //// inputStream.transferTo(outputStream); //// outputStream.close(); //// inputStream.close(); //// } catch (IOException e) { //// e.printStackTrace(); //// } // ///////////////////////////////////////////////////////////////// //// String path=pathTool.getUserAvatarPath()+fileName; // String path = imagePathEntity.getImgPath(); // System.out.println("path 2>>"+path); // String fileName = imagePathEntity.getFileName(); // System.out.println("fileName>>"+fileName); // OutputStream os = null; // InputStream is = null; // try { // String contentType=""; // String ext=""; // if(fileName.endsWith("jpg")) { // contentType="image/jpeg"; // ext="jpg"; // }else if(fileName.endsWith("png")) { // contentType="image/png"; // ext="png"; // }else if(fileName.endsWith("gif")) { // contentType="image/gif"; // ext="gif"; // } // System.out.println(" path>>"+path); // is=new FileInputStream(new File(path)); // BufferedImage image = ImageIO.read(is); // System.out.println(" contentType>>"+contentType); // response.setContentType(contentType); // os = response.getOutputStream(); // if (image != null) { // ImageIO.write(image,ext, os); // } // } catch (IOException e) { // e.printStackTrace(); // } finally { // if (is != null) { // is.close(); // } // if (os != null) { // os.flush(); // os.close(); // } // } // } /** * @Method : exportOneImage2 * @Description : 程序出错,不可用 * @Params : [imageId] * @Return : java.awt.image.BufferedImage * @Author : ZhuoNing * @Date : 2021/1/23 * @Time : 19:54 */ @Deprecated @RequestMapping(value = "/exportOneImage2", method = RequestMethod.GET, produces = MediaType.IMAGE_JPEG_VALUE) @ResponseBody public BufferedImage exportOneImage2(@RequestParam(value = "imageId", required = true) String imageId) throws IOException { // 读取 ImagePathEntity imagePathEntity = imageService.getOneImageDetail(imageId); if (imagePathEntity == null) { log.error("imagePathEntity[" + imagePathEntity.toString() + "]==null"); } return ImageIO.read(new FileInputStream(new File(imagePathEntity.getImgPath()))); } @Deprecated @RequestMapping(value = "/exportOneImage3", method = RequestMethod.GET, produces = MediaType.IMAGE_JPEG_VALUE) @ResponseBody public byte[] exportOneImage3(@RequestParam(value = "imageId", required = true) String imageId) throws IOException { // 读取 ImagePathEntity imagePathEntity = imageService.getOneImageDetail(imageId); if (imagePathEntity == null) { log.error("imagePathEntity[" + imagePathEntity.toString() + "]==null"); } File file = new File(imagePathEntity.getImgPath()); FileInputStream inputStream = new FileInputStream(file); byte[] bytes = new byte[inputStream.available()]; inputStream.read(bytes, 0, inputStream.available()); return bytes; } @Deprecated @RequestMapping(value = "/downloadPicture", method = RequestMethod.GET) public void downloadPicture(@RequestParam(value = "imageId", required = true) String imageId) { // 读取 ImagePathEntity imagePathEntity = imageService.getOneImageDetail(imageId); if (imagePathEntity == null) { log.error("imagePathEntity[" + imagePathEntity.toString() + "]==null"); } // 拼接完整图片路径。这里填写图片链接 // String urlPath = ""; // 获取图片文件后缀名 // String postfix = "." + StringUtils.substringAfterLast(url, "."); // 获取当前类的所在项目路径 // File directory = new File(""); // String courseFile; String srcPath; // File srcFile = null; FileInputStream fileInputStream = null; InputStream fis = null; OutputStream out = null; try { // courseFile = directory.getCanonicalPath(); // String fileName = "\\" + StringUtil.getUUID() + postfix; String fileName = imagePathEntity.getFileName(); System.out.println("fileName>>" + fileName); // 下载文件 // FileDownloadUtil.downloadHttpUrl(urlPath, courseFile, fileName); // srcPath = courseFile + fileName; // srcFile = new File(srcPath); srcPath = imagePathEntity.getImgPath(); System.out.println("srcPath>>" + srcPath); fileInputStream = new FileInputStream(srcPath); fis = new BufferedInputStream(fileInputStream); byte[] buffer = new byte[fis.available()]; fis.read(buffer); response.setContentType("application/octet-stream"); response.setHeader("Content-disposition", "attachment;filename=" + fileName); out = response.getOutputStream(); out.write(buffer); out.flush(); out.close(); } catch (Exception e) { e.printStackTrace(); } finally { try { if (fileInputStream != null) { fileInputStream.close(); } if (fis != null) { fis.close(); } if (out != null) { out.close(); } } catch (IOException e) { e.printStackTrace(); } } // 删除中间文件 // if (srcFile != null) { // System.out.println(FileDownloadUtil.deleteFile(srcFile)); // } } }