浏览代码

修复导出单张图片

zhuoning 4 年之前
父节点
当前提交
ce663896d0

+ 239 - 131
huimv-video/huimv-video-manager/src/main/java/com/huimv/manager/controller/ImageManagerController.java

@@ -20,6 +20,7 @@ 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;
@@ -48,108 +49,103 @@ public class ImageManagerController {
     protected HttpServletResponse response;
 
     /**
-     * @Method      : getLastImageAiResult
+     * @Method : getLastImageAiResult
      * @Description : 读取最新的图像AI识别记录
-     * @Params      : []
-     * @Return      : java.lang.String
-     * 
-     * @Author      : ZhuoNing
-     * @Date        : 2021/1/22       
-     * @Time        : 10:00
+     * @Params : []
+     * @Return : java.lang.String
+     * @Author : ZhuoNing
+     * @Date : 2021/1/22
+     * @Time : 10:00
      */
-    @RequestMapping(value="/getLastImageAiResult",method = RequestMethod.GET)
-    public String getLastImageAiResult(){
+    @RequestMapping(value = "/getLastImageAiResult", method = RequestMethod.GET)
+    public String getLastImageAiResult() {
         return JSONObject.toJSON(imageService.getLastImage()).toString();
     }
 
     /**
-     * @Method      : getNextImageAiResult
+     * @Method : getNextImageAiResult
      * @Description : 读取下一条图像AI识别记录
-     * @Params      : []
-     * @Return      : java.lang.String
-     * 
-     * @Author      : ZhuoNing
-     * @Date        : 2021/1/22       
-     * @Time        : 10:01
+     * @Params : []
+     * @Return : java.lang.String
+     * @Author : ZhuoNing
+     * @Date : 2021/1/22
+     * @Time : 10:01
      */
-    @RequestMapping(value="/getNextImageAiResult",method = RequestMethod.GET)
-    public String getNextImageAiResult(){
+    @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
+     * @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 = "pageNo",required = true) int pageNo,@RequestParam(value = "pageSize",required = true) int pageSize){
-        log.info("startDate>>"+startDate);
-        log.info("endDate>>"+endDate);
-        log.info("pageNo>>"+pageNo);
-        log.info("pageSize>>"+pageSize);
+    @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 = "pageNo", required = true) int pageNo, @RequestParam(value = "pageSize", required = true) int pageSize) {
+        log.info("startDate>>" + startDate);
+        log.info("endDate>>" + endDate);
+        log.info("pageNo>>" + pageNo);
+        log.info("pageSize>>" + pageSize);
         //
-        List<ImagePathEntity> imageList = imageService.getImageList(startDate,endDate,pageNo,pageSize);
+        List<ImagePathEntity> imageList = imageService.getImageList(startDate, endDate, pageNo, pageSize);
         JSONArray imageJa = new JSONArray();
-        for(ImagePathEntity imageEntity:imageList){
+        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("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());
             imageJa.add(imageJo);
         }
         // 读取记录数
-        int total = imageService.getTotal(startDate,endDate);
+        int total = imageService.getTotal(startDate, endDate);
         JSONObject outJo = new JSONObject();
-        outJo.put("data",imageJa);
-        outJo.put("total",total);
+        outJo.put("data", imageJa);
+        outJo.put("total", total);
         return outJo;
     }
 
     /**
-     * @Method      : previousImage
+     * @Method : previousImage
      * @Description : 读取前一张
-     * @Params      : [id]
-     * @Return      : com.alibaba.fastjson.JSONObject
-     * 
-     * @Author      : ZhuoNing
-     * @Date        : 2021/1/23       
-     * @Time        : 19:09
+     * @Params : [id]
+     * @Return : com.alibaba.fastjson.JSONObject
+     * @Author : ZhuoNing
+     * @Date : 2021/1/23
+     * @Time : 19:09
      */
-    @RequestMapping(value = "/getPreviousImage",method = RequestMethod.GET)
-    public JSONObject getPreviousImage(@RequestParam(value = "imageId",required = true) int imageId){
-        return imageService.getOneImage("previous",imageId);
+    @RequestMapping(value = "/getPreviousImage", method = RequestMethod.GET)
+    public JSONObject getPreviousImage(@RequestParam(value = "imageId", required = true) int imageId) {
+        return imageService.getOneImage("previous", imageId);
     }
 
     /**
-     * @Method      : nextImage
+     * @Method : nextImage
      * @Description : 读取下一张图片
-     * @Params      : [id]
-     * @Return      : com.alibaba.fastjson.JSONObject
-     * 
-     * @Author      : ZhuoNing
-     * @Date        : 2021/1/23       
-     * @Time        : 19:09
+     * @Params : [id]
+     * @Return : com.alibaba.fastjson.JSONObject
+     * @Author : ZhuoNing
+     * @Date : 2021/1/23
+     * @Time : 19:09
      */
-    @RequestMapping(value = "/getNextImage",method = RequestMethod.GET)
-    public JSONObject getNextImage(@RequestParam(value = "imageId",required = true) int imageId){
+    @RequestMapping(value = "/getNextImage", method = RequestMethod.GET)
+    public JSONObject getNextImage(@RequestParam(value = "imageId", required = true) int imageId) {
         //
-        return imageService.getOneImage("next",imageId);
+        return imageService.getOneImage("next", imageId);
     }
 
     @Deprecated
@@ -158,13 +154,13 @@ public class ImageManagerController {
         //定义根路径
         String rootPath = request.getRealPath("/");
         //创建文件
-        File file = new File(rootPath+"temp_download");
+        File file = new File(rootPath + "temp_download");
         //判断文件是否存在,如果不存在,则创建此文件夹
-        if(!file.exists()){
+        if (!file.exists()) {
             file.mkdir();
         }
         String name = "图片压缩包下载";
-        String fileName = name+new Date().getTime();
+        String fileName = name + new Date().getTime();
         String zipFileName = fileName + ".zip";
         File zipFile = null;
         String path = rootPath + "temp_download";
@@ -172,8 +168,8 @@ public class ImageManagerController {
         //调用工具类获取图片
         byte[] data = ImageByteUtil.image2byte("F:\\blank.jpg");
         //new一个文件对象用来保存图片,默认保存当前工程根目录
-        if(data != null){
-            File imageFile = new File(path+File.separator+fileName+".jpg");
+        if (data != null) {
+            File imageFile = new File(path + File.separator + fileName + ".jpg");
             //创建输出流
             FileOutputStream outStream = new FileOutputStream(imageFile);
             //写入数据
@@ -195,7 +191,7 @@ public class ImageManagerController {
              * zos.close();
              */
             //获取创建好的图片文件
-            File imageFile = new File(path+"/"+fileName+".jpg");
+            File imageFile = new File(path + "/" + fileName + ".jpg");
             // 打成压缩包
             zipFile = new File(path + "/" + zipFileName);
             FileOutputStream zipFos = new FileOutputStream(zipFile);
@@ -241,27 +237,26 @@ public class ImageManagerController {
 
     /**
      * 压缩多个文件到一个zip压缩包中
-     * @description :
-     * @date 2016-11-4
-     * @author liucong
+     *
      * @param request
      * @param response
      * @throws Exception
-     *
-     *
+     * @description :
+     * @date 2016-11-4
+     * @author liucong
      */
     @RequestMapping("/zipMoreFiles")
-    public void zipMoreFiles(HttpServletRequest request,HttpServletResponse response) throws Exception{
+    public void zipMoreFiles(HttpServletRequest request, HttpServletResponse response) throws Exception {
         //定义根路径
         String rootPath = request.getRealPath("/");
         //创建文件
-        File file = new File(rootPath+"temp_download");
+        File file = new File(rootPath + "temp_download");
         //判断文件是否存在,如果不存在,则创建此文件夹
-        if(!file.exists()){
+        if (!file.exists()) {
             file.mkdir();
         }
         String name = "图片压缩包下载";
-        String fileName = name+new Date().getTime();
+        String fileName = name + new Date().getTime();
         String zipFileName = fileName + ".zip";
         File zipFile = null;
         String path = rootPath + "temp_download";
@@ -275,7 +270,7 @@ public class ImageManagerController {
         ArchiveOutputStream archOut = new ArchiveStreamFactory().createArchiveOutputStream(ArchiveStreamFactory.ZIP, zipFos);
         if (archOut instanceof ZipArchiveOutputStream) {
             ZipArchiveOutputStream zos = (ZipArchiveOutputStream) archOut;
-            for(File imageFile : filelist){
+            for (File imageFile : filelist) {
                 ZipArchiveEntry zipEntry = new ZipArchiveEntry(imageFile, imageFile.getName());
                 zos.putArchiveEntry(zipEntry);
                 zos.write(FileUtils.readFileToByteArray(imageFile));
@@ -302,41 +297,41 @@ public class ImageManagerController {
     }
 
     @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{
+    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.info("输入参数[startDate]>>" + startDate);
+        log.info("输入参数[endDate]>>" + endDate);
+        log.info("输入参数[imageIds]>>" + imageIds);
+        if (startDate == null && endDate == null && imageIds == null) {
             log.error("日期区间或指定图片导出二选一");
-            return ;
+            return;
         }
         // 读取图片文件
-        List<ImagePathEntity> imageList = imageService.getExportImageList(startDate,endDate,imageIds);
-        if(imageList.size()==0){
+        List<ImagePathEntity> 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){
+        for (ImagePathEntity imagePathEntity : imageList) {
             File imageFile = new File(imagePathEntity.getImgPath());
-            fileList[i++]=imageFile;
+            fileList[i++] = imageFile;
         }
         //定义根路径
         String rootPath = request.getRealPath("/");
         //创建文件
-        File file = new File(rootPath+"temp_download");
+        File file = new File(rootPath + "temp_download");
         //判断文件是否存在,如果不存在,则创建此文件夹
-        if(!file.exists()){
+        if (!file.exists()) {
             file.mkdir();
         }
         String name = "图片压缩包下载";
-        String fileName = name+new Date().getTime();
+        String fileName = name + new Date().getTime();
         String zipFileName = fileName + ".zip";
         File zipFile = null;
         String path = rootPath + "temp_download";
@@ -346,7 +341,7 @@ public class ImageManagerController {
         ArchiveOutputStream archOut = new ArchiveStreamFactory().createArchiveOutputStream(ArchiveStreamFactory.ZIP, zipFos);
         if (archOut instanceof ZipArchiveOutputStream) {
             ZipArchiveOutputStream zos = (ZipArchiveOutputStream) archOut;
-            for(File imageFile : fileList){
+            for (File imageFile : fileList) {
                 ZipArchiveEntry zipEntry = new ZipArchiveEntry(imageFile, imageFile.getName());
                 zos.putArchiveEntry(zipEntry);
                 zos.write(FileUtils.readFileToByteArray(imageFile));
@@ -373,60 +368,173 @@ public class ImageManagerController {
     }
 
     /**
-     * @Method      : exportOneImage
+     * @Method : exportOneImage
      * @Description : 导出一张图片信息
-     * @Params      : [imageId]
-     * @Return      : byte[]
-     *
-     * @Author      : ZhuoNing
-     * @Date        : 2021/1/23
-     * @Time        : 19:53
+     * @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 byte[] exportOneImage(@RequestParam(value = "imageId",required = true) String imageId) throws IOException {
+    @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");
+        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();
+                    }
+                }
+            }
         }
-        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;
     }
 
+//    @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
+     * @Method : exportOneImage2
      * @Description : 程序出错,不可用
-     * @Params      : [imageId]
-     * @Return      : java.awt.image.BufferedImage
-     * 
-     * @Author      : ZhuoNing
-     * @Date        : 2021/1/23       
-     * @Time        : 19:54
+     * @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)
+    @RequestMapping(value = "/exportOneImage2", method = RequestMethod.GET, produces = MediaType.IMAGE_JPEG_VALUE)
     @ResponseBody
-    public BufferedImage exportOneImage2(@RequestParam(value = "imageId",required = true) String imageId) throws IOException {
+    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");
+        if (imagePathEntity == null) {
+            log.error("imagePathEntity[" + imagePathEntity.toString() + "]==null");
         }
         return ImageIO.read(new FileInputStream(new File(imagePathEntity.getImgPath())));
     }
 
     @Deprecated
-    @RequestMapping(value = "/downloadPicture", method = RequestMethod.GET)
-    public void downloadPicture(@RequestParam(value = "imageId",required = true) String imageId) {
+    @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");
+        if (imagePathEntity == null) {
+            log.error("imagePathEntity[" + imagePathEntity.toString() + "]==null");
         }
 
         // 拼接完整图片路径。这里填写图片链接
@@ -447,14 +555,14 @@ public class ImageManagerController {
 //            courseFile = directory.getCanonicalPath();
 //            String fileName = "\\" + StringUtil.getUUID() + postfix;
             String fileName = imagePathEntity.getFileName();
-            System.out.println("fileName>>"+fileName);
+            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);
+            System.out.println("srcPath>>" + srcPath);
             fileInputStream = new FileInputStream(srcPath);
             fis = new BufferedInputStream(fileInputStream);
             byte[] buffer = new byte[fis.available()];