liurunyu
2025-01-13 bc70680118bfc82574330b2ae8bc10ca3c9453a9
pipIrr-platform/pipIrr-web/pipIrr-web-file/src/main/java/com/dy/pipIrrWebFile/files/FileCtrl.java
@@ -9,8 +9,9 @@
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import java.io.File;
import java.io.InputStream;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.*;
/**
 * web文件上传, 内部调用,即由其他子模块调用,
@@ -24,6 +25,9 @@
    @Value("${dy.photoZipWidth}")
    private String photoZipWidthStr ;
    private static final String VideoZipPicFileType = "jpg";
    private static final Integer VideoZipPicFromFrame = 5 ;
    /**
     * web分布式文件系统保存照片文件
@@ -65,7 +69,7 @@
                        String zipFilePath1 = filePath.substring(0, index) ;
                        String zipFilePath2 = filePath.substring(index) ;
                        String zipFilePath = zipFilePath1 + "_" + zipFilePath2 ;
                        InputStream zipFileInput = null ;
                        InputStream zipFileInput ;
                        if(zipFilePath2.equalsIgnoreCase(".png")){
                            zipFileInput = ZipImg.zipToPng(fPic, photoZipWidth, photoZipWidth) ;
                        }else{
@@ -152,6 +156,24 @@
                    String filePath = absolutePath + fileRelativePath ;
                    if(!fUtil.saveFile(filePath, input)){
                        fileRelativePath = null ;
                    }else {
                        //存储成功, 生成缩略图
                        BufferedImage bufImg = new VideoUtils(VideoZipPicFileType, VideoZipPicFromFrame).fetchFrame(file) ;
                        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) ;
                        }
                    }
                }
            }
@@ -199,4 +221,31 @@
        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) ;
        }
    }
}