| New file | 
 |  |  | 
 |  |  | package com.dy.pipIrrWebFile.files; | 
 |  |  |  | 
 |  |  | import com.dy.common.util.NumUtil; | 
 |  |  | import com.dy.pipIrrWebFile.util.*; | 
 |  |  | import lombok.extern.slf4j.Slf4j; | 
 |  |  | import org.jcodec.scale.AWTUtil; | 
 |  |  | import org.springframework.beans.factory.annotation.Value; | 
 |  |  | import org.springframework.web.bind.annotation.PostMapping; | 
 |  |  | import org.springframework.web.bind.annotation.RequestMapping; | 
 |  |  | import org.springframework.web.bind.annotation.RestController; | 
 |  |  | import org.springframework.web.multipart.MultipartFile; | 
 |  |  |  | 
 |  |  | import javax.imageio.ImageIO; | 
 |  |  | import java.awt.image.BufferedImage; | 
 |  |  | import java.io.*; | 
 |  |  |  | 
 |  |  | /** | 
 |  |  |  * web文件上传, 内部调用,即由其他子模块调用, | 
 |  |  |  * 一般不由前端系统调用 | 
 |  |  |  */ | 
 |  |  | @Slf4j | 
 |  |  | @RestController | 
 |  |  | @RequestMapping(path="file") | 
 |  |  | @SuppressWarnings("unchecked")//java版本越高,对泛型约束越严,所以配置SuppressWarnings("unchecked") | 
 |  |  | public class FileCtrl { | 
 |  |  |  | 
 |  |  |     @Value("${dy.photoZipWidth}") | 
 |  |  |     private String photoZipWidthStr ; | 
 |  |  |  | 
 |  |  |     private static final String VideoZipPicFileType = "jpg"; | 
 |  |  |     private static final Integer VideoZipPicFromFrame = 5 ; | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * web分布式文件系统保存照片文件 | 
 |  |  |      * @param file | 
 |  |  |      * @param regionNum | 
 |  |  |      * @param json 水印信息 | 
 |  |  |      * @param absolutePath | 
 |  |  |      * @param relativePath | 
 |  |  |      * @param fileName | 
 |  |  |      * @return | 
 |  |  |      */ | 
 |  |  |     @PostMapping("/savePhoto") | 
 |  |  |     public String savePhoto(MultipartFile file, | 
 |  |  |                           String regionNum, | 
 |  |  |                           String json, | 
 |  |  |                           String absolutePath, | 
 |  |  |                           String relativePath, | 
 |  |  |                           String fileName) { | 
 |  |  |         Integer photoZipWidth = 400 ; | 
 |  |  |         if(photoZipWidthStr != null && NumUtil.isPlusIntNumber(photoZipWidthStr)){ | 
 |  |  |             photoZipWidth = Integer.parseInt(photoZipWidthStr) ; | 
 |  |  |         } | 
 |  |  |         String fileRelativePath = null ; | 
 |  |  |         try { | 
 |  |  |             if (file != null) { | 
 |  |  |                 if(absolutePath != null && relativePath != null && fileName != null){ | 
 |  |  |                     InputStream input = file.getInputStream() ; | 
 |  |  |                     //生成新文件相对路径 | 
 |  |  |                     FilePhotoUtil fUtil = new FilePhotoUtil() ; | 
 |  |  |                     fileRelativePath = fUtil.newFileRelativityPath(absolutePath, relativePath, regionNum) + fileName ; | 
 |  |  |                     String filePath = absolutePath + fileRelativePath ; | 
 |  |  |                     if(!fUtil.saveFile(filePath, input)){ | 
 |  |  |                         fileRelativePath = null ; | 
 |  |  |                     }else { | 
 |  |  |                         //存储成功 | 
 |  |  |                         File fPic = new File(filePath) ; | 
 |  |  |                         //生成缩略图 | 
 |  |  |                         int index = filePath.lastIndexOf(".") ; | 
 |  |  |                         String zipFilePath1 = filePath.substring(0, index) ; | 
 |  |  |                         String zipFilePath2 = filePath.substring(index) ; | 
 |  |  |                         String zipFilePath = zipFilePath1 + "_" + zipFilePath2 ; | 
 |  |  |                         InputStream zipFileInput ; | 
 |  |  |                         if(zipFilePath2.equalsIgnoreCase(".png")){ | 
 |  |  |                             zipFileInput = ZipImg.zipToPng(fPic, photoZipWidth, photoZipWidth) ; | 
 |  |  |                         }else{ | 
 |  |  |                             zipFileInput = ZipImg.zipToJpg(fPic, photoZipWidth, photoZipWidth) ; | 
 |  |  |                         } | 
 |  |  |                         if(zipFileInput.available() > 0){ | 
 |  |  |                             new FileUtil().saveFile(zipFilePath, zipFileInput) ; | 
 |  |  |                         }else{ | 
 |  |  |                             //如果压缩文件不存在或生成失败,则复制源文件 | 
 |  |  |                             new FileUtil().saveFile(zipFilePath, file.getInputStream()) ; | 
 |  |  |                         } | 
 |  |  |                     } | 
 |  |  |                 } | 
 |  |  |             } | 
 |  |  |         } catch (Exception e) { | 
 |  |  |             log.error("保存照片文件异常", e); | 
 |  |  |         } | 
 |  |  |         return fileRelativePath ; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * web分布式文件系统保存录音音频文件 | 
 |  |  |      * @param file | 
 |  |  |      * @param regionNum | 
 |  |  |      * @param json 水印信息 | 
 |  |  |      * @param absolutePath | 
 |  |  |      * @param relativePath | 
 |  |  |      * @param fileName | 
 |  |  |      * @return | 
 |  |  |      */ | 
 |  |  |     @PostMapping("/savePhone") | 
 |  |  |     public String savePhone(MultipartFile file, | 
 |  |  |                             String regionNum, | 
 |  |  |                             String json, | 
 |  |  |                             String absolutePath, | 
 |  |  |                             String relativePath, | 
 |  |  |                             String fileName) { | 
 |  |  |         String fileRelativePath = null ; | 
 |  |  |         try { | 
 |  |  |             if (file != null) { | 
 |  |  |                 if(absolutePath != null && relativePath != null && fileName != null){ | 
 |  |  |                     InputStream input = file.getInputStream() ; | 
 |  |  |                     //生成新文件相对路径 | 
 |  |  |                     FilePhoneUtil fUtil = new FilePhoneUtil() ; | 
 |  |  |                     fileRelativePath = fUtil.newFileRelativityPath(absolutePath, relativePath, regionNum) + fileName ; | 
 |  |  |                     String filePath = absolutePath + fileRelativePath ; | 
 |  |  |                     if(!fUtil.saveFile(filePath, input)){ | 
 |  |  |                         fileRelativePath = null ; | 
 |  |  |                     } | 
 |  |  |                 } | 
 |  |  |             } | 
 |  |  |         } catch (Exception e) { | 
 |  |  |             log.error("保存录音音频文件异常", e); | 
 |  |  |         } | 
 |  |  |         return fileRelativePath ; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * web分布式文件系统保存录像视频文件 | 
 |  |  |      * @param file | 
 |  |  |      * @param regionNum | 
 |  |  |      * @param json 水印信息 | 
 |  |  |      * @param absolutePath | 
 |  |  |      * @param relativePath | 
 |  |  |      * @param fileName | 
 |  |  |      * @return | 
 |  |  |      */ | 
 |  |  |     @PostMapping("/saveVideo") | 
 |  |  |     public String saveVideo(MultipartFile file, | 
 |  |  |                             String regionNum, | 
 |  |  |                             String json, | 
 |  |  |                             String absolutePath, | 
 |  |  |                             String relativePath, | 
 |  |  |                             String fileName) { | 
 |  |  |         String fileRelativePath = null ; | 
 |  |  |         try { | 
 |  |  |             if (file != null) { | 
 |  |  |                 if(absolutePath != null && relativePath != null && fileName != null){ | 
 |  |  |                     InputStream input = file.getInputStream() ; | 
 |  |  |                     //生成新文件相对路径 | 
 |  |  |                     FileVideoUtil fUtil = new FileVideoUtil() ; | 
 |  |  |                     fileRelativePath = fUtil.newFileRelativityPath(absolutePath, relativePath, regionNum) + fileName ; | 
 |  |  |                     String filePath = absolutePath + fileRelativePath ; | 
 |  |  |                     if(!fUtil.saveFile(filePath, input)){ | 
 |  |  |                         fileRelativePath = null ; | 
 |  |  |                     }else { | 
 |  |  |                         //存储成功, 生成缩略图 | 
 |  |  |                         BufferedImage bufImg = new VideoUtils(VideoZipPicFileType, VideoZipPicFromFrame).fetchFrame(file) ; | 
 |  |  |                         // 向右旋转90度 | 
 |  |  |                         // bufImg = AWTUtil.rotate90ToRight(bufImg) ; | 
 |  |  |                         String zipFilePath = filePath.substring(0, filePath.lastIndexOf(".")) + "_." + VideoZipPicFileType ; | 
 |  |  |                         Integer photoZipWidth = 400 ; | 
 |  |  |                         if(photoZipWidthStr != null && NumUtil.isPlusIntNumber(photoZipWidthStr)){ | 
 |  |  |                             photoZipWidth = Integer.parseInt(photoZipWidthStr) ; | 
 |  |  |                         } | 
 |  |  |                         InputStream zipFileInput = ZipImg.zipToJpg(bufImg, photoZipWidth, photoZipWidth) ; | 
 |  |  |                         if(zipFileInput.available() > 0){ | 
 |  |  |                             new FileUtil().saveFile(zipFilePath, zipFileInput) ; | 
 |  |  |                         }else{ | 
 |  |  |                             //如果压缩文件不存在或生成失败,则复制源文件 | 
 |  |  |                             ByteArrayOutputStream os = new ByteArrayOutputStream(); | 
 |  |  |                             ImageIO.write(bufImg, VideoZipPicFileType, os); | 
 |  |  |                             InputStream in = new ByteArrayInputStream(os.toByteArray()); | 
 |  |  |                             new FileUtil().saveFile(zipFilePath, in) ; | 
 |  |  |                         } | 
 |  |  |                     } | 
 |  |  |                 } | 
 |  |  |             } | 
 |  |  |         } catch (Exception e) { | 
 |  |  |             log.error("保存录像视频文件异常", e); | 
 |  |  |         } | 
 |  |  |         return fileRelativePath ; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |  | 
 |  |  |     /** | 
 |  |  |      * web分布式文件系统保存文档文件 | 
 |  |  |      * @param file | 
 |  |  |      * @param regionNum | 
 |  |  |      * @param json 水印信息 | 
 |  |  |      * @param absolutePath | 
 |  |  |      * @param relativePath | 
 |  |  |      * @param fileName | 
 |  |  |      * @return | 
 |  |  |      */ | 
 |  |  |     @PostMapping("/saveDocument") | 
 |  |  |     public String saveDocument(MultipartFile file, | 
 |  |  |                             String regionNum, | 
 |  |  |                             String json, | 
 |  |  |                             String absolutePath, | 
 |  |  |                             String relativePath, | 
 |  |  |                             String fileName) { | 
 |  |  |         String fileRelativePath = null ; | 
 |  |  |         try { | 
 |  |  |             if (file != null) { | 
 |  |  |                 if(absolutePath != null && relativePath != null && fileName != null){ | 
 |  |  |                     InputStream input = file.getInputStream() ; | 
 |  |  |                     //生成新文件相对路径 | 
 |  |  |                     FileDocumentUtil fUtil = new FileDocumentUtil() ; | 
 |  |  |                     fileRelativePath = fUtil.newFileRelativityPath(absolutePath, relativePath, regionNum) + fileName ; | 
 |  |  |                     String filePath = absolutePath + fileRelativePath ; | 
 |  |  |                     if(!fUtil.saveFile(filePath, input)){ | 
 |  |  |                         fileRelativePath = null ; | 
 |  |  |                     } | 
 |  |  |                 } | 
 |  |  |             } | 
 |  |  |         } catch (Exception e) { | 
 |  |  |             log.error("保存文档文件异常", e); | 
 |  |  |         } | 
 |  |  |         return fileRelativePath ; | 
 |  |  |     } | 
 |  |  |  | 
 |  |  |  | 
 |  |  |     public static void main(String[] args) throws Exception{ | 
 |  |  |         String photoZipWidthStr = "400" ; | 
 |  |  |         String VideoZipPicFileType = "jpg"; | 
 |  |  |         //存储成功, 生成缩略图 | 
 |  |  |         String filePath = "D:\\mp4\\test.mp4" ; | 
 |  |  |         BufferedImage bufImg = new VideoUtils("jpg", 0).fetchFrame(filePath) ; | 
 |  |  |         int index = filePath.lastIndexOf(".") ; | 
 |  |  |         String basePath = filePath.substring(0, index) ; | 
 |  |  |         Integer photoZipWidth = 400 ; | 
 |  |  |         if(photoZipWidthStr != null && NumUtil.isPlusIntNumber(photoZipWidthStr)){ | 
 |  |  |             photoZipWidth = Integer.parseInt(photoZipWidthStr) ; | 
 |  |  |         } | 
 |  |  |         String zipFilePath = basePath + "_." + VideoZipPicFileType ; | 
 |  |  |         InputStream zipFileInput = null ; | 
 |  |  |         zipFileInput = ZipImg.zipToJpg(bufImg, photoZipWidth, photoZipWidth) ; | 
 |  |  |         if(zipFileInput.available() > 0){ | 
 |  |  |             new FileUtil().saveFile(zipFilePath, zipFileInput) ; | 
 |  |  |         }else{ | 
 |  |  |             //如果压缩文件不存在或生成失败,则复制源文件 | 
 |  |  |             ByteArrayOutputStream os = new ByteArrayOutputStream(); | 
 |  |  |             ImageIO.write(bufImg, VideoZipPicFileType, os); | 
 |  |  |             InputStream in = new ByteArrayInputStream(os.toByteArray()); | 
 |  |  |             new FileUtil().saveFile(zipFilePath, in) ; | 
 |  |  |         } | 
 |  |  |     } | 
 |  |  |  | 
 |  |  | } |