From c2976b80e8850be2d28dc2132c6659856b871f3c Mon Sep 17 00:00:00 2001 From: zhubaomin <zhubaomin> Date: 星期四, 08 五月 2025 21:10:42 +0800 Subject: [PATCH] Merge branch 'master' of http://8.140.179.55:20000/r/pipIrr-SV --- pipIrr-platform/pipIrr-web/pipIrr-web-file/src/main/java/com/dy/pipIrrWebFile/files/FileCtrl.java | 254 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 254 insertions(+), 0 deletions(-) diff --git a/pipIrr-platform/pipIrr-web/pipIrr-web-file/src/main/java/com/dy/pipIrrWebFile/files/FileCtrl.java b/pipIrr-platform/pipIrr-web/pipIrr-web-file/src/main/java/com/dy/pipIrrWebFile/files/FileCtrl.java new file mode 100644 index 0000000..02626f9 --- /dev/null +++ b/pipIrr-platform/pipIrr-web/pipIrr-web-file/src/main/java/com/dy/pipIrrWebFile/files/FileCtrl.java @@ -0,0 +1,254 @@ +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鐗堟湰瓒婇珮锛屽娉涘瀷绾︽潫瓒婁弗锛屾墍浠ラ厤缃甋uppressWarnings("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鍒嗗竷寮忔枃浠剁郴缁熶繚瀛樻枃妗f枃浠� + * @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) ; + } + } + +} -- Gitblit v1.8.0