package com.glxp.api.upload; import com.glxp.api.common.enums.ResultEnum; import com.glxp.api.common.res.BaseResponse; import com.glxp.api.common.util.ResultVOUtils; import com.glxp.api.util.MinioUtil; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.multipart.MultipartFile; import java.io.File; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.HashMap; import java.util.Map; import java.util.UUID; /** * 上传文件控制器 */ @RestController public class uploadController { @Value("${file_path}") private String filePath; @Value("${file_url}") private String fileUrl; @Value("${file_lpath}") private String fileLpath; @Value("${minio_path}") private String minioPath; @Value("${minio_url}") private String minioUrl; /** * 上传PDF模板 */ @PostMapping("/udiwms/upload/pdf/template/jasper") public BaseResponse uploadPDFTemplate(@RequestParam("file") MultipartFile file) throws UnsupportedEncodingException { if (file.isEmpty()) { return ResultVOUtils.error(ResultEnum.DATA_ERROR, "上传文件不能为空"); } // 保存文件 --------------------- String fileName = file.getOriginalFilename(); String fileType = fileName.substring(fileName.lastIndexOf(".")); // 文件类型判断 if (StringUtils.isBlank(fileType) || !fileType.equals(".jasper")) { return ResultVOUtils.error(ResultEnum.DATA_ERROR, "上传文件只能是 jasper 格式"); } String newName = UUID.randomUUID() + fileType;//生成新文件名 String savePath = filePath + "/pdf/template"; File file1 = new File(savePath); if (!file1.exists()) {// 判断目录是否存在 file1.mkdirs();// 创建多层目录 } file1 = new File(savePath + "/" + newName); try { file.transferTo(file1); Map rMap = new HashMap<>(); rMap.put("msg", "上传成功"); rMap.put("path", newName); return ResultVOUtils.success(rMap); } catch (IOException e) { e.printStackTrace(); } return ResultVOUtils.error(ResultEnum.DATA_ERROR, "上传失败"); } @PostMapping("/udiwms/upload/pdf/template/jrxml") public BaseResponse uploadPDFJrxmlTemplate(@RequestParam("file") MultipartFile file) throws UnsupportedEncodingException { if (file.isEmpty()) { return ResultVOUtils.error(ResultEnum.DATA_ERROR, "上传文件不能为空"); } // 保存文件 --------------------- String fileName = file.getOriginalFilename(); String fileType = fileName.substring(fileName.lastIndexOf(".")); // 文件类型判断 if (StringUtils.isBlank(fileType) || !fileType.equals(".jrxml")) { return ResultVOUtils.error(ResultEnum.DATA_ERROR, "上传文件只能是 jrxml 格式"); } String newName = UUID.randomUUID() + fileType;//生成新文件名 String savePath = filePath + "/pdf/template"; File file1 = new File(savePath); if (!file1.exists()) {// 判断目录是否存在 file1.mkdirs();// 创建多层目录 } file1 = new File(savePath + "/" + newName); try { file.transferTo(file1); Map rMap = new HashMap<>(); rMap.put("msg", "上传成功"); rMap.put("path", savePath + "/" + newName); return ResultVOUtils.success(rMap); } catch (IOException e) { e.printStackTrace(); } return ResultVOUtils.error(ResultEnum.DATA_ERROR, "上传失败"); } /** * 注册页面上传文件 */ @PostMapping("/udiwms/upload/register/file") public BaseResponse uploadRegisterFile(@RequestParam("file") MultipartFile file, @RequestParam("type") String type) throws UnsupportedEncodingException { if (file.isEmpty()) { return ResultVOUtils.error(ResultEnum.DATA_ERROR, "上传文件不能为空"); } if (StringUtils.isBlank(type)) { return ResultVOUtils.error(ResultEnum.DATA_ERROR, "上传文件类型不能为空"); } // 保存文件 --------------------- String fileName = file.getOriginalFilename(); String fileType = fileName.substring(fileName.lastIndexOf(".")); // 文件类型判断 if (StringUtils.isBlank(fileType) || (!fileType.equals(".jpg") && !fileType.equals(".png") && !fileType.equals(".doc") && !fileType.equals(".pdf"))) { return ResultVOUtils.error(ResultEnum.DATA_ERROR, "上传文件只能是 jpg,png,doc,pdf 格式"); } String newName = UUID.randomUUID() + fileType;//生成新文件名 String savePath = filePath + "/register/file/" + type; String savePath1 = fileLpath + "?type=" + type; String fileFullName = savePath + "/" + newName; File file1 = new File(savePath); if (!file1.exists()) {// 判断目录是否存在 file1.mkdirs();// 创建多层目录 } file1 = new File(savePath + "/" + newName); try { file.transferTo(file1); // MinioUtil.uploadFile(fileFullName, file); Map rMap = new HashMap<>(); rMap.put("msg", "上传成功"); rMap.put("name", fileUrl + savePath1 + "&name=" + newName); rMap.put("type", type); return ResultVOUtils.success(rMap); } catch (Exception e) { e.printStackTrace(); } return ResultVOUtils.error(ResultEnum.DATA_ERROR, "上传失败"); } }