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

139 lines
5.5 KiB
Java

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 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;
/**
* 上传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", savePath + "/" + 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<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 格式");
}
String newName = UUID.randomUUID() + fileType;//生成新文件名
String savePath = filePath + "/register/" + type;
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("name", newName);
rMap.put("type", type);
return ResultVOUtils.success(rMap);
} catch (IOException e) {
e.printStackTrace();
}
return ResultVOUtils.error(ResultEnum.DATA_ERROR, "上传失败");
}
}