package com.tianbo.warehouse.controller; import com.tianbo.warehouse.controller.response.ResultJson; import com.tianbo.warehouse.model.Attachment; import com.tianbo.warehouse.service.AttachmentService; import com.tianbo.warehouse.util.Date.DateUtil; import com.tianbo.warehouse.util.Helper; import org.apache.commons.io.FileUtils; import org.springframework.beans.factory.annotation.Autowired; 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 static com.tianbo.warehouse.util.IO.FileTool.readProperties; @RestController public class UploadController { @Autowired private AttachmentService attachmentService; @Value("${web.upload-path}") private String uploadPath; // =./upload/ /** * 单个文件上传 * @param file * @return * @throws Exception */ @PostMapping("/upload") public ResultJson upload(@RequestParam MultipartFile file) throws Exception { ResultJson resultJson = new ResultJson(); if (null!=file && !file.isEmpty() && file.getSize()>0){ byte[] filebyte = file.getBytes(); String fileLitleName = file.getName(); String fileName = file.getOriginalFilename(); String fileType = fileName.substring((fileName.lastIndexOf("."))); StringBuffer stringBuffer = new StringBuffer(); String saveFileName = stringBuffer.append(uploadPath).append(DateUtil.getToday()).append("/").append(Helper.getUUID()).append(fileType).toString(); File saveFile = new File(saveFileName); if(!saveFile.getParentFile().exists()){ saveFile.mkdirs(); } try{ FileUtils.writeByteArrayToFile(saveFile,filebyte); Attachment attachment = new Attachment(saveFileName); int i =attachmentService.insertSelective(attachment); resultJson = (i==1 ? new ResultJson("200","附件入库成功",saveFileName) :new ResultJson("500","附件入库失败")); }catch (IOException e){ resultJson = new ResultJson("400","附件存储失败",e); e.printStackTrace(); }catch (IllegalStateException e){ resultJson = new ResultJson("400","附件存储失败",e); e.printStackTrace(); } } return resultJson; } /** * 批量上传 * @param files * @return * @throws Exception */ @PostMapping("/uploads") public ResultJson upload(MultipartFile[] files) throws Exception { if (files!=null && files.length>0){ // ... 处理文件储存逻辑 for (MultipartFile file : files) { if (!file.isEmpty() && file.getSize()>0){ String fileName = file.getName(); String fileType = file.getContentType(); String saveDir = readProperties("uploadDirectory"); StringBuffer stringBuffer = new StringBuffer(); stringBuffer.append(saveDir).append("/").append(DateUtil.getToday()).append("/").append(fileType).append(Helper.getUUID()).append(fileName); File saveFile = new File(stringBuffer.toString()); try{ file.transferTo(saveFile); }catch (IOException e){ e.printStackTrace(); }catch (IllegalStateException e){ e.printStackTrace(); } } } } return new ResultJson(); } }