You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
udi-spms-java/src/main/java/com/glxp/api/upload/uploadController.java

150 lines
5.9 KiB
Java

2 years ago
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;
2 years ago
import com.glxp.api.util.MinioUtil;
2 years ago
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;
2 years ago
@Value("${minio_path}")
private String minioPath;
@Value("${minio_url}")
private String minioUrl;
2 years ago
/**
* 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<String, String> rMap = new HashMap<>();
rMap.put("msg", "上传成功");
rMap.put("path", newName);
2 years ago
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<String, String> 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 格式");
2 years ago
}
String newName = UUID.randomUUID() + fileType;//生成新文件名
String savePath = filePath + "/register/" + type;
String savePath1 = fileLpath + "?type=" + type;
2 years ago
File file1 = new File(savePath);
if (!file1.exists()) {// 判断目录是否存在
file1.mkdirs();// 创建多层目录
}
file1 = new File(savePath + "/" + newName);
2 years ago
try {
file.transferTo(file1);
// MinioUtil.uploadFile(fileFullName, file);
2 years ago
Map<String, String> rMap = new HashMap<>();
rMap.put("msg", "上传成功");
rMap.put("name", fileUrl + savePath1 + "&name=" + newName);
2 years ago
rMap.put("type", type);
return ResultVOUtils.success(rMap);
} catch (IOException e) {
2 years ago
e.printStackTrace();
}
return ResultVOUtils.error(ResultEnum.DATA_ERROR, "上传失败");
}
}