新增附件上传接口
parent
592c85184d
commit
ff7325b117
@ -0,0 +1,38 @@
|
|||||||
|
package com.nmgs.config;
|
||||||
|
|
||||||
|
import com.nmgs.util.PropertiesUtil;
|
||||||
|
import org.springframework.beans.factory.annotation.Value;
|
||||||
|
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||||
|
import org.springframework.boot.autoconfigure.web.servlet.MultipartAutoConfiguration;
|
||||||
|
import org.springframework.boot.web.servlet.MultipartConfigFactory;
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.core.io.ClassPathResource;
|
||||||
|
import org.springframework.util.unit.DataSize;
|
||||||
|
import org.springframework.web.multipart.MultipartResolver;
|
||||||
|
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
|
||||||
|
|
||||||
|
import javax.servlet.MultipartConfigElement;
|
||||||
|
import java.io.File;
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
//@Configuration
|
||||||
|
public class MultipartConfig{
|
||||||
|
private String tempDir= PropertiesUtil.getValue("fileTemp");
|
||||||
|
|
||||||
|
// @Bean
|
||||||
|
MultipartConfigElement multipartConfigElement() {
|
||||||
|
MultipartConfigFactory factory = new MultipartConfigFactory();
|
||||||
|
factory.setMaxFileSize(DataSize.ofMegabytes(30));
|
||||||
|
factory.setMaxRequestSize(DataSize.ofMegabytes(50));
|
||||||
|
File tmpDirFile = new File(tempDir);
|
||||||
|
// 判断文件夹是否存在
|
||||||
|
if (!tmpDirFile.exists()) {
|
||||||
|
//创建文件夹
|
||||||
|
tmpDirFile.mkdirs();
|
||||||
|
}
|
||||||
|
factory.setLocation(tempDir);
|
||||||
|
return factory.createMultipartConfig();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@ -0,0 +1,107 @@
|
|||||||
|
package com.nmgs.controller;
|
||||||
|
|
||||||
|
import com.alibaba.fastjson.JSONArray;
|
||||||
|
import com.alibaba.fastjson.JSONObject;
|
||||||
|
import com.nmgs.config.ResultData;
|
||||||
|
import com.nmgs.util.*;
|
||||||
|
import io.swagger.annotations.Api;
|
||||||
|
import io.swagger.annotations.ApiOperation;
|
||||||
|
import org.apache.commons.io.FileUtils;
|
||||||
|
import org.springframework.scheduling.annotation.Async;
|
||||||
|
import org.springframework.stereotype.Controller;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMethod;
|
||||||
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
|
import org.springframework.web.bind.annotation.ResponseBody;
|
||||||
|
import org.springframework.web.multipart.MultipartFile;
|
||||||
|
|
||||||
|
import java.io.File;
|
||||||
|
import java.util.Base64;
|
||||||
|
import java.util.Properties;
|
||||||
|
|
||||||
|
@Controller
|
||||||
|
@RequestMapping(value = "/fileUpload")
|
||||||
|
@Api(tags = "附件上传")
|
||||||
|
public class FileUploadController {
|
||||||
|
@RequestMapping(value = "/uploadAllFile", method = {RequestMethod.POST})
|
||||||
|
@ApiOperation(value = "上传附件")
|
||||||
|
@ResponseBody
|
||||||
|
public ResultData<String> uploadAllFile(@RequestParam("filedata") MultipartFile filedata, @RequestParam("fileType") String fileType, @RequestParam("uuid") String uuid) {
|
||||||
|
String fileName = "";
|
||||||
|
try {
|
||||||
|
LogUtil.WriteLog_Info("附件上传接口执行开始===========" + PubTools.getCurrentDate(), "FileController");
|
||||||
|
Properties props = System.getProperties();
|
||||||
|
String os = props.getProperty("os.name").toLowerCase();
|
||||||
|
String fileAddressLocal = PathUtil.uploadPath;
|
||||||
|
if (os.startsWith("win")) {
|
||||||
|
// fileAddressLocal = PropertiesUtil.getValue("fileAddressWin");
|
||||||
|
fileAddressLocal = fileAddressLocal.replace("/", "\\");
|
||||||
|
} else {
|
||||||
|
// fileAddressLocal = PropertiesUtil.getValue("fileAddressLinux");
|
||||||
|
}
|
||||||
|
LogUtil.WriteLog_Info("附件上传接口====fileAddressLocal====" + fileAddressLocal, "FileController");
|
||||||
|
if (!PubTools.isNull(fileAddressLocal)) {
|
||||||
|
File filePath = new File(fileAddressLocal);
|
||||||
|
if (!filePath.exists()) {
|
||||||
|
filePath.mkdir();
|
||||||
|
}
|
||||||
|
if ("NUMBERPIC".equals(fileType)) {//人员身份证
|
||||||
|
fileName = uuid + "NUMPIC" + ".jpg";
|
||||||
|
} else if ("LICENSEPIC".equals(fileType)) {//车辆驾驶证
|
||||||
|
fileName = uuid + "-LICPIC" + ".jpg";
|
||||||
|
} else if ("PERMITPIC".equals(fileType)) {//车辆行驶证
|
||||||
|
fileName = uuid + "-PERPIC" + ".jpg";
|
||||||
|
} else if ("VEHICLEPIC".equals(fileType)) {//车辆照片
|
||||||
|
fileName = uuid + "-VEHPIC" + ".jpg";
|
||||||
|
} else {
|
||||||
|
return ResultData.fail("上传附件失败:附件类型不匹配");
|
||||||
|
}
|
||||||
|
|
||||||
|
/* File dest = new File(PropertiesUtil.getValue("fileTemp"), fileName);
|
||||||
|
System.out.println(filedata.getOriginalFilename());
|
||||||
|
// 使用下面的jar包
|
||||||
|
FileUtils.copyInputStreamToFile(filedata.getInputStream(), dest);
|
||||||
|
File fileTemp = new File(PropertiesUtil.getValue("fileTemp") + "/" + fileName);
|
||||||
|
if (!fileTemp.exists()) {
|
||||||
|
return ResultData.success(2, "附件上传失败");
|
||||||
|
}
|
||||||
|
String imageBase64 = ImageUtils.getImgFileToBase64(PropertiesUtil.getValue("fileTemp") + "/" + fileName);
|
||||||
|
if (fileTemp.exists()) {
|
||||||
|
fileTemp.delete();
|
||||||
|
}*/
|
||||||
|
byte[] bytes = filedata.getBytes();
|
||||||
|
String imageBase64 = ImageUtils.byteToBase64(bytes);
|
||||||
|
if (PubTools.isNull(imageBase64)) {
|
||||||
|
return ResultData.success(2, "附件解析失败");
|
||||||
|
}
|
||||||
|
String addWaterImage = ImageUtils.addImageWaterMark(imageBase64, "仅用于内蒙古交通货运车辆积分会员认证", fileName);
|
||||||
|
if (!PubTools.isNull(addWaterImage)) {
|
||||||
|
// ImageUtils.delFile(fileName, false);
|
||||||
|
byte[] imageBytes = Base64.getDecoder().decode(addWaterImage);
|
||||||
|
// 创建文件
|
||||||
|
File file = new File(fileAddressLocal, fileName); // 指定上传位置
|
||||||
|
if (file.exists()) {
|
||||||
|
file.delete();
|
||||||
|
}
|
||||||
|
// 保存文件
|
||||||
|
LogUtil.WriteLog_Info("附件开始上传到服务器===========" + PubTools.getCurrentDate(), "FileController");
|
||||||
|
FileUtils.writeByteArrayToFile(file, imageBytes);
|
||||||
|
LogUtil.WriteLog_Info("附件上传到服务器结束===========" + PubTools.getCurrentDate(), "FileController");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
LogUtil.WriteLog_Info("附件上传接口执行结束===========" + PubTools.getCurrentDate(), "FileController");
|
||||||
|
} catch (Exception e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
return ResultData.fail("上传附件失败:" + e.getMessage());
|
||||||
|
}
|
||||||
|
return ResultData.success("上传成功", fileName, 1);
|
||||||
|
}
|
||||||
|
|
||||||
|
@RequestMapping(value = "/uploadAllFileTest", method = {RequestMethod.POST})
|
||||||
|
@ApiOperation(value = "上传附件测试")
|
||||||
|
@ResponseBody
|
||||||
|
public ResultData<String> uploadAllFileTest() {
|
||||||
|
String fileName = "测试返回结果";
|
||||||
|
return ResultData.success("上传成功", fileName, 1);
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue