package com.dy.dyFile.files; import com.dy.common.util.NumUtil; import com.dy.dyFile.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) ; } boolean zipOk = new FileUtil().saveFile(zipFilePath, zipFileInput) ; } } } } 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 ; } }