package com.dy.pipIrrWebFile.files; 
 | 
  
 | 
import com.dy.common.util.NumUtil; 
 | 
import com.dy.pipIrrWebFile.util.*; 
 | 
import lombok.extern.slf4j.Slf4j; 
 | 
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 java.io.File; 
 | 
import java.io.InputStream; 
 | 
  
 | 
/** 
 | 
 * web文件上传, 内部调用,即由其他子模块调用, 
 | 
 * 一般不由前端系统调用 
 | 
 */ 
 | 
@Slf4j 
 | 
@RestController 
 | 
@RequestMapping(path="file") 
 | 
@SuppressWarnings("unchecked")//java版本越高,对泛型约束越严,所以配置SuppressWarnings("unchecked") 
 | 
public class FileCtrl { 
 | 
  
 | 
    @Value("${dy.photoZipWidth}") 
 | 
    private String photoZipWidthStr ; 
 | 
  
 | 
    /** 
 | 
     * 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 = null ; 
 | 
                        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 ; 
 | 
                    } 
 | 
                } 
 | 
            } 
 | 
        } 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 ; 
 | 
    } 
 | 
  
 | 
} 
 |