소스 검색

新建指定一张图片下载

zhuoning 4 년 전
부모
커밋
05d3520d79

+ 50 - 6
huimv-video/huimv-video-manager/src/main/java/com/huimv/manager/controller/ImageManagerController.java

@@ -16,14 +16,14 @@ 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.http.HttpServletRequest;
 import javax.servlet.http.HttpServletResponse;
-import java.io.File;
-import java.io.FileOutputStream;
-import java.io.IOException;
-import java.io.OutputStream;
+import java.awt.image.BufferedImage;
+import java.io.*;
 //import java.sql.Timestamp;
 import java.util.Date;
 import java.util.List;
@@ -299,6 +299,7 @@ public class ImageManagerController {
             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()){
@@ -370,8 +371,51 @@ public class ImageManagerController {
         }
     }
 
-    @RequestMapping(value = "/exportOneImage",method = RequestMethod.GET)
-    public void exportOneImage(@RequestParam(value = "type",required = true) String type){
+    /**
+     * @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 byte[] exportOneImage(@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;
+    }
 
+    /**
+     * @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())));
     }
+
 }

+ 13 - 0
huimv-video/huimv-video-manager/src/main/java/com/huimv/manager/dao/repo/ImagePathRepo.java

@@ -95,4 +95,17 @@ public interface ImagePathRepo extends JpaRepository<ImagePathEntity, Integer>,
      */
     @Query(nativeQuery = true, value = "SELECT * FROM image_path WHERE id<:imageId ORDER BY id DESC LIMIT 1")
     ImagePathEntity getPreviousImageById(int imageId);
+
+    /**
+     * @Method      : getOneImageById
+     * @Description : 
+     * @Params      : [imageId]
+     * @Return      : com.huimv.manager.dao.entity.ImagePathEntity
+     * 
+     * @Author      : ZhuoNing
+     * @Date        : 2021/1/23       
+     * @Time        : 19:45
+     */
+    @Query(nativeQuery = true, value = "SELECT * FROM image_path WHERE id=:imageId")
+    ImagePathEntity getOneImageById(String imageId);
 }

+ 12 - 0
huimv-video/huimv-video-manager/src/main/java/com/huimv/manager/service/IImageService.java

@@ -77,4 +77,16 @@ public interface IImageService {
      * @Time        : 19:25
      */
     JSONObject getOneImage(String type, int imageId);
+
+    /**
+     * @Method      : getOneImageDetail
+     * @Description : 读取一张图片详情
+     * @Params      : [imageId]
+     * @Return      : void
+     * 
+     * @Author      : ZhuoNing
+     * @Date        : 2021/1/23       
+     * @Time        : 19:42
+     */
+    ImagePathEntity getOneImageDetail(String imageId);
 }

+ 6 - 0
huimv-video/huimv-video-manager/src/main/java/com/huimv/manager/service/impl/ImageServiceImpl.java

@@ -128,4 +128,10 @@ public class ImageServiceImpl implements IImageService {
             return  (JSONObject)JSONObject.toJSON(imagePathRepo.getPreviousImageById(imageId));
         }
     }
+
+    @Override
+    public ImagePathEntity getOneImageDetail(String imageId) {
+        //
+        return imagePathRepo.getOneImageById(imageId);
+    }
 }

+ 93 - 0
huimv-video/huimv-video-manager/src/main/java/com/huimv/manager/utils/ImageUploadUtils.java

@@ -0,0 +1,93 @@
+package com.huimv.manager.utils;
+
+import org.apache.commons.io.FileUtils;
+import org.springframework.web.multipart.MultipartFile;
+
+import javax.servlet.http.HttpServletRequest;
+import java.io.BufferedOutputStream;
+import java.io.File;
+import java.io.IOException;
+import java.util.UUID;
+
+/**
+ * @Project : huimv.shiwan
+ * @Package : com.huimv.biosafety.uface.controller
+ * @Description : TODO
+ * @Version : 1.0
+ * @Author : ZhuoNing
+ * @Create : 2020-12-25
+ **/
+public class ImageUploadUtils {
+    public static String Upload(MultipartFile files[], HttpServletRequest request) {
+        //ogger.info("图片批量上传,files[]=",files);
+        String uploadPath = request.getSession().getServletContext().getRealPath("/") + "/upload";
+        StringBuilder filefiles = new StringBuilder();
+        // String uploadPath = "C:\\test\\upload";
+        File uploadDirectory = new File(uploadPath);
+        if (uploadDirectory.exists()) {
+            if (!uploadDirectory.isDirectory()) {
+                uploadDirectory.delete();
+            }
+        } else {
+            uploadDirectory.mkdir();
+        }
+        //这里可以支持多文件上传
+        if (files != null && files.length >= 1) {
+            BufferedOutputStream bw = null;
+            try {
+                for (MultipartFile file : files) {
+                    String fileName = file.getOriginalFilename();
+                    //判断是否有文件且是否为图片文件
+                    if(fileName!=null && !"".equalsIgnoreCase(fileName.trim()) && isImageFile(fileName)) {
+                        filefiles.append(uploadPath + "/" + UUID.randomUUID().toString()+ getFileType(fileName));
+                        filefiles.append(":");
+                        //创建输出文件对象
+                        File outFile = new File(uploadPath + "/" + UUID.randomUUID().toString()+ getFileType(fileName));
+                        //拷贝文件到输出文件对象
+                        FileUtils.copyInputStreamToFile(file.getInputStream(), outFile);
+                    }
+                }
+            } catch (Exception e) {
+                e.printStackTrace();
+            } finally {
+                try {
+                    if (bw != null) {
+                        bw.close();
+                    }
+                } catch (IOException e) {
+                    e.printStackTrace();
+                }
+            }
+        }
+//        return "upload successful";
+        return filefiles.toString();
+    }
+
+
+    private static Boolean isImageFile(String fileName) {
+        String[] img_type = new String[]{".jpg", ".jpeg", ".png", ".gif", ".bmp"};
+        if (fileName == null) {
+            return false;
+        }
+        fileName = fileName.toLowerCase();
+        for (String type : img_type) {
+            if (fileName.endsWith(type)) {
+                return true;
+            }
+        }
+        return false;
+    }
+
+    /**
+     * 获取文件后缀名
+     *
+     * @param fileName
+     * @return
+     */
+    private static String getFileType(String fileName) {
+        if (fileName != null && fileName.indexOf(".") >= 0) {
+            return fileName.substring(fileName.lastIndexOf("."), fileName.length());
+        }
+        return "";
+    }
+}