Commit f74dedff by 高晓磊

文件上传并压缩

parent ecc8ad25
package com.xyst.dinas.transport.web; package com.xyst.dinas.transport.web;
import com.beecode.bap.attachment.AttachmentConstants;
import com.beecode.bap.attachment.AttachmentInfo;
import com.beecode.bap.attachment.common.Utils;
import com.beecode.bap.attachment.exception.AttachmentDataUploadException;
import com.beecode.bap.attachment.service.AttachmentService;
import com.beecode.bcp.type.KObject; import com.beecode.bcp.type.KObject;
import com.beecode.inz.basis.team.pojo.ResponseObj; import com.beecode.inz.basis.team.pojo.ResponseObj;
import com.xyst.dinas.biz.service.StationService; import com.xyst.dinas.biz.service.StationService;
...@@ -9,18 +14,21 @@ import com.xyst.dinas.sales.service.SalesPlanService; ...@@ -9,18 +14,21 @@ import com.xyst.dinas.sales.service.SalesPlanService;
import com.xyst.dinas.sales.service.SalesRecordService; import com.xyst.dinas.sales.service.SalesRecordService;
import com.xyst.dinas.sales.web.info.SalesRecordInfo; import com.xyst.dinas.sales.web.info.SalesRecordInfo;
import com.xyst.dinas.transport.service.VehicleDispatchService; import com.xyst.dinas.transport.service.VehicleDispatchService;
import net.coobird.thumbnailator.Thumbnails;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.http.HttpHeaders;
import org.springframework.web.bind.annotation.RequestBody; import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestParam; import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import java.io.*;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.util.Collection; import java.util.*;
import java.util.List;
import java.util.Map;
import java.util.UUID;
@RestController @RestController
/** /**
...@@ -30,116 +38,277 @@ import java.util.UUID; ...@@ -30,116 +38,277 @@ import java.util.UUID;
public class WeighbridgeSyncController { public class WeighbridgeSyncController {
@Autowired @Autowired
private StationService stationService; private StationService stationService;
@Autowired @Autowired
private SalesPlanService salesPlanService; private SalesPlanService salesPlanService;
@Autowired @Autowired
private SalesRecordService salesRecordService; private SalesRecordService salesRecordService;
@Autowired @Autowired
private VehicleDispatchService vehicleDispatchService; private VehicleDispatchService vehicleDispatchService;
@Autowired @Autowired
private InventoryService inventoryService; private InventoryService inventoryService;
@Autowired
private AttachmentService attachmentService;
/**
* 通过List传递 销售台账数据,进行存储 /**
* * 通过List传递 销售台账数据,进行存储
* @param salesRecordList 销售台账队列 *
* @return * @param salesRecordList 销售台账队列
*/ * @return
@PostMapping(value = "/weighbridgeSync/saveSalesRecord", consumes = "application/json") */
public Object saveSalesRecord(@RequestParam String weighbridgeCode,@RequestBody SalesRecordInfo salesRecordList) { @PostMapping(value = "/weighbridgeSync/saveSalesRecord", consumes = "application/json")
//根据地磅id查询场站 public Object saveSalesRecord(@RequestParam String weighbridgeCode, @RequestBody SalesRecordInfo salesRecordList) {
if(StringUtils.isBlank(weighbridgeCode)){ //根据地磅id查询场站
return ResponseObj.error(500,"地磅唯一编码不能为空"); if (StringUtils.isBlank(weighbridgeCode)) {
} return ResponseObj.error(500, "地磅唯一编码不能为空");
KObject station = stationService.getStationByWeighbridge(weighbridgeCode); }
if(station==null){ KObject station = stationService.getStationByWeighbridge(weighbridgeCode);
return ResponseObj.error(500,"没有查询到地磅匹配的场站"); if (station == null) {
} return ResponseObj.error(500, "没有查询到地磅匹配的场站");
if (salesRecordList==null) { }
return ResponseObj.error("传递的销售台账数据条数为0!请确认!"); if (salesRecordList == null) {
} else { return ResponseObj.error("传递的销售台账数据条数为0!请确认!");
return ResponseObj.success("新增成功",salesRecordService.saveSalesRecordService(salesRecordList,station)); } else {
} return ResponseObj.success("新增成功", salesRecordService.saveSalesRecordService(salesRecordList, station));
} }
}
/**
* 根据车牌号和地磅唯一编码获取本场站下本车辆今日销售计划和临时销售计划 /**
* @param * 根据车牌号和地磅唯一编码获取本场站下本车辆今日销售计划和临时销售计划
* @return *
*/ * @param
@PostMapping("/weighbridgeSync/getSalePlanDetail") * @return
public Object approveSalesPlan(@RequestParam String weighbridgeCode,@RequestParam String carNum) { */
//根据地磅id查询场站 @PostMapping("/weighbridgeSync/getSalePlanDetail")
public Object approveSalesPlan(@RequestParam String weighbridgeCode, @RequestParam String carNum) {
if(StringUtils.isBlank(weighbridgeCode)){ //根据地磅id查询场站
return ResponseObj.error(500,"地磅唯一编码不能为空");
} if (StringUtils.isBlank(weighbridgeCode)) {
KObject station = stationService.getStationByWeighbridge(weighbridgeCode); return ResponseObj.error(500, "地磅唯一编码不能为空");
if(station==null){ }
return ResponseObj.error(500,"没有查询到地磅匹配的场站"); KObject station = stationService.getStationByWeighbridge(weighbridgeCode);
} if (station == null) {
if(StringUtils.isBlank(carNum)){ return ResponseObj.error(500, "没有查询到地磅匹配的场站");
return ResponseObj.error(500,"车牌号不能为空"); }
} if (StringUtils.isBlank(carNum)) {
//根据车牌查询所属合同和销售计划 return ResponseObj.error(500, "车牌号不能为空");
List<Map<String,Object>> salesPlan = salesPlanService.querySalesPlanByCarNum(station,carNum); }
Collection<? extends Map<String, Object>> maps = vehicleDispatchService.vehicleDispatchService(station, carNum); //根据车牌查询所属合同和销售计划
if(maps!=null){ List<Map<String, Object>> salesPlan = salesPlanService.querySalesPlanByCarNum(station, carNum);
salesPlan.addAll(maps); Collection<? extends Map<String, Object>> maps = vehicleDispatchService.vehicleDispatchService(station, carNum);
} if (maps != null) {
return ResponseObj.success("查询成功",salesPlan); salesPlan.addAll(maps);
} }
return ResponseObj.success("查询成功", salesPlan);
/** }
* 根据车牌号和地磅唯一编码获取本场站下本车辆今日销售计划和临时销售计划
* @param /**
* @return * 根据车牌号和地磅唯一编码获取本场站下本车辆今日销售计划和临时销售计划
*/ *
@PostMapping("/weighbridgeSync/getDinasTypePrice") * @param
public Object getDinasTypePrice(@RequestParam String weighbridgeCode,@RequestParam String carNum,@RequestParam String dinasTypeId) { * @return
//根据地磅id查询场站 */
if(StringUtils.isBlank(weighbridgeCode)){ @PostMapping("/weighbridgeSync/uploadFile")
return ResponseObj.error(500,"地磅唯一编码不能为空"); public Object getDinasTypePrice(@RequestParam String weighbridgeCode,
} @RequestParam String recordId,
KObject station = stationService.getStationByWeighbridge(weighbridgeCode); @RequestParam("files")
if(station==null){ MultipartFile[] file) {
return ResponseObj.error(500,"没有查询到地磅匹配的场站"); if (null == file || file.length < 1) {
} return ResponseObj.error(500, "文件不能为空");
if(StringUtils.isBlank(carNum)){ }
return ResponseObj.error(500,"车牌号不能为空"); //根据地磅id查询场站
} if (StringUtils.isBlank(weighbridgeCode)) {
if(StringUtils.isBlank(dinasTypeId)){ return ResponseObj.error(500, "地磅唯一编码不能为空");
return ResponseObj.error(500,"砂石类型不能为空"); }
} KObject station = stationService.getStationByWeighbridge(weighbridgeCode);
KObject itemByStationIdAndDinasTypeId = inventoryService.getItemByStationIdAndDinasTypeId(station.getUuid("id"), UUID.fromString("dinasTypeId")); if (station == null) {
BigDecimal price = itemByStationIdAndDinasTypeId.getBigDecimal("price"); return ResponseObj.error(500, "没有查询到地磅匹配的场站");
return ResponseObj.success("查询成功",price); }
}
//根据查询地磅id
/** if (StringUtils.isBlank(recordId)) {
* 查询全部售卖的砂石类型和价格 return ResponseObj.error(500, "地磅记录不能为空");
* @param }
* @return KObject itemByStationIdAndDinasTypeId = inventoryService.getItemByStationIdAndDinasTypeId(station.getUuid("id"), UUID.fromString("dinasTypeId"));
*/ BigDecimal price = itemByStationIdAndDinasTypeId.getBigDecimal("price");
@PostMapping("/weighbridgeSync/getAllDinasType") return ResponseObj.success("查询成功", price);
public Object getAllDinasType(@RequestParam String weighbridgeCode) { }
//根据地磅id查询场站
if(StringUtils.isBlank(weighbridgeCode)){
return ResponseObj.error(500,"地磅唯一编码不能为空"); @RequestMapping(value = "files", method = RequestMethod.POST)
} public ResponseObj<UUID> upload(@RequestParam(name = "belongId", required = false) UUID belongId,
KObject station = stationService.getStationByWeighbridge(weighbridgeCode); @RequestParam("files") MultipartFile[] files,
if(station==null){ @RequestHeader(name = HttpHeaders.USER_AGENT, required = false) String userAgent) {
return ResponseObj.error(500,"没有查询到地磅匹配的场站"); if (belongId == null) {
} belongId = UUID.randomUUID();
List<StationDinasTypeRelation> allDinasTypes = inventoryService.getItemListByStationId(station.getUuid("id")); }
return ResponseObj.success("查询成功",allDinasTypes); checkFileUpload(files);
} try {
for (MultipartFile file : files) {
File file1 = new File("");
Thumbnails.of(file.getInputStream())
.scale(0.9f)
.toFile(file1);
FileInputStream
fileInputStream = new FileInputStream(file1);
AttachmentInfo info = generateInfo(belongId, "saleRecord", true, userAgent, file.getOriginalFilename(), file1.length());
return ResponseObj.response(200,"保存成功", attachmentService.insert(info, fileInputStream));
}
} catch (IOException e) {
throw new AttachmentDataUploadException("文件上传失败," + e.getMessage(), e);
}
return ResponseObj.response(500,"保存失败", null);
}
/**
* FileItem类对象创建
*
* @param inputStream inputStream
* @param fileName fileName
* @return FileItem
*/
private FileItem createFileItem(InputStream inputStream, String fileName) {
FileItemFactory factory = new DiskFileItemFactory(16, null);
String textFieldName = "file";
FileItem item = factory.createItem(textFieldName, MediaType.MULTIPART_FORM_DATA_VALUE, true, fileName);
int bytesRead = 0;
byte[] buffer = new byte[8192];
//使用输出流输出输入流的字节
try (OutputStream os = item.getOutputStream()) {
while ((bytesRead = inputStream.read(buffer, 0, 8192)) != -1) {
os.write(buffer, 0, bytesRead);
}
inputStream.close();
} catch (IOException e) {
throw new IllegalArgumentException("文件上传失败");
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (IOException ignored) {
}
}
}
return item;
}
public static File byte2File(byte[] buf, String filePath, String fileName) {
BufferedOutputStream bos = null;
FileOutputStream fos = null;
File file = null;
try {
File dir = new File(filePath);
if (!dir.exists() && dir.isDirectory()) {
dir.mkdirs();
}
file = new File(filePath + File.separator + fileName);
fos = new FileOutputStream(file);
bos = new BufferedOutputStream(fos);
bos.write(buf);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (bos != null) {
try {
bos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return file;
}
// outputStream转inputStream
public ByteArrayInputStream parse(OutputStream out) throws Exception {
ByteArrayOutputStream baos;
baos = (ByteArrayOutputStream) out;
return new ByteArrayInputStream(baos.toByteArray());
}
private String getFileNameByIE(String fileName) {
String[] fileNameArray = fileName.split("\\\\");
return fileNameArray[fileNameArray.length - 1];
}
private boolean isIE(String userAgent) {
if (userAgent != null) {
userAgent = userAgent.toLowerCase();
return userAgent.contains("msie") || userAgent.contains("trident") || userAgent.contains("edge");
}
return false;
}
private AttachmentInfo generateInfo(UUID belongId, String category, boolean anonymous, String userAgent, String fileName, Long fileSize) {
AttachmentInfo attachmentInfo = new AttachmentInfo();
attachmentInfo.setBelongId(belongId);
if (category != null && !"".equals(category)) {
attachmentInfo.setCategory(category);
}
if (anonymous) {
attachmentInfo.setFileName(UUID.randomUUID() + "." + Utils.getFileType(Objects.requireNonNull(fileName)));
} else if (isIE(userAgent)) {// macOS系统存在含‘\’文件名的文件所以分开写
attachmentInfo.setFileName(getFileNameByIE(Objects.requireNonNull(fileName)));
} else {
attachmentInfo.setFileName(fileName);
}
attachmentInfo.setFileType(Utils.getFileType(Objects.requireNonNull(fileName)));
attachmentInfo.setSize(fileSize);
return attachmentInfo;
}
private void checkFileUpload(MultipartFile[] files) {
Assert.isTrue(files.length < AttachmentConstants.FileCheck.MAXTOTAL, "文件数量不能超过30!");
for (MultipartFile file : files) {
checkFileUpload(file);
}
}
private void checkFileUpload(MultipartFile file) {
Assert.isTrue(!AttachmentConstants.FileCheck.NOTSUPPORTFILETYPE.contains(Utils.getFileType(file.getOriginalFilename())),
"不支持的文件类型");
Assert.isTrue(file.getSize() < AttachmentConstants.FileCheck.MAXLENGTH, "单个文件大小不能超过30m!");
}
/**
* 查询全部售卖的砂石类型和价格
*
* @param
* @return
*/
@PostMapping("/weighbridgeSync/getAllDinasType")
public Object getAllDinasType(@RequestParam String weighbridgeCode) {
//根据地磅id查询场站
if (StringUtils.isBlank(weighbridgeCode)) {
return ResponseObj.error(500, "地磅唯一编码不能为空");
}
KObject station = stationService.getStationByWeighbridge(weighbridgeCode);
if (station == null) {
return ResponseObj.error(500, "没有查询到地磅匹配的场站");
}
List<StationDinasTypeRelation> allDinasTypes = inventoryService.getItemListByStationId(station.getUuid("id"));
return ResponseObj.success("查询成功", allDinasTypes);
}
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment