package com.newfiber.api.pc.controller.zhz; import com.newfiber.api.core.annotation.SysLog; import com.newfiber.api.core.commons.*; import com.newfiber.api.pc.dto.MeetPlanFileDTO; import com.newfiber.api.pc.model.meet.MeetPlan; import com.newfiber.api.pc.model.meet.MeetPlanFile; import com.newfiber.api.pc.service.MeetPlanFileService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.util.StringUtils; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletResponse; import java.io.*; import java.net.URLEncoder; import java.util.Calendar; import java.util.Date; import java.util.List; /** * @Author:zzh * @CreateDate:2020/11/26 16:42 * @Description: */ @RestController @RequestMapping("/meet/file/") @Api(value = "MeetPlanFileController",tags = "预案文件管理") public class MeetPlanFileController { @Autowired private MeetPlanFileService meetPlanFileService; /** 节点文件保存路径 */ @Value("${xnFile.path}") private String xnFilePath; @Value("${nginx.file}") private String nginxfile; @PostMapping("/queryPage") @ApiOperation("分页查询某个预案下的文件") @SysLog(actionType = "4",value = "分页查询某个预案下的文件") public ResultObj<PageResultObject<MeetPlanFile>> queryPage(@RequestBody PageRequestObject<MeetPlanFileDTO> meetPlanFileDTO){ if(StringUtils.isEmpty(meetPlanFileDTO.getObject().getPlanId())){ throw new CustomException(ResultCode.PARAM_NULL); } return new ResultObj<>(ResultCode.OK,meetPlanFileService.queryPage(meetPlanFileDTO)); } @PostMapping("/delete") @ApiOperation("批量删除文件") @SysLog(actionType = "2",value = "批量删除文件") public ResultObj deleteFiles(@RequestParam("ids")List<Integer> ids){ if(StringUtils.isEmpty(ids)){ throw new CustomException(ResultCode.PARAM_NULL); } meetPlanFileService.deleteFiles(ids); return ResultObj.ok(); } @PostMapping(value = "/addMeetFile") @ApiOperation("新增预案文件") @SysLog(actionType = "1",value = "新增预案文件") public ResultObj addFileInfo(@RequestParam("file") MultipartFile multipartFile, @RequestParam("planId")Integer planId){ String filename = multipartFile.getOriginalFilename(); String suffix = filename.substring(filename.lastIndexOf(".")); if(!suffix.equalsIgnoreCase(".pdf")){ return new ResultObj(500,"必须上传pdf文件!"); } Calendar instance = Calendar.getInstance(); String filePath = instance.get(Calendar.YEAR) + File.separator +(instance.get(Calendar.MARCH) + 1) + File.separator + filename; File file = new File(xnFilePath , filePath); if(!file.getParentFile().exists()){ file.mkdirs(); } try{ multipartFile.transferTo(file); }catch (IOException e){ e.printStackTrace(); } MeetPlanFile meetPlan = new MeetPlanFile(); meetPlan.setPlanId(planId); meetPlan.setFileName(filename); meetPlan.setFilePath(nginxfile + filePath); meetPlan.setCreateTime(new Date()); boolean insert = meetPlanFileService.insert(meetPlan); if(!insert){ throw new CustomException(999,"新增预案文件失败!"); } return ResultObj.ok(); } @PostMapping("/downloadFile") @ApiOperation("文件下载") @SysLog(actionType = "4",value = "文件下载") public void downloadFile(@RequestParam("mId")Integer mId, HttpServletResponse response) throws UnsupportedEncodingException { if(StringUtils.isEmpty(mId)){ throw new CustomException(ResultCode.PARAM_NULL); } MeetPlanFile meetPlan = meetPlanFileService.selectById(mId); File file = new File(xnFilePath , meetPlan.getFilePath()); if(file.exists()){ // 配置文件下载 response.setHeader("content-type", "application/octet-stream"); response.setContentType("application/octet-stream"); // 下载文件能正常显示中文 response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(meetPlan.getFileName(),"UTF-8") ); // 实现文件下载 byte[] buffer = new byte[1024]; FileInputStream fis = null; BufferedInputStream bis = null; try { fis = new FileInputStream(file); bis = new BufferedInputStream(fis); OutputStream os = response.getOutputStream(); int i = bis.read(buffer); while (i != -1) { os.write(buffer, 0, i); i = bis.read(buffer); } } catch (Exception e) { } finally { if (bis != null) { try { bis.close(); } catch (IOException e) { e.printStackTrace(); } } if (fis != null) { try { fis.close(); } catch (IOException e) { e.printStackTrace(); } } } } } public String getXnFilePath() { return xnFilePath; } public void setXnFilePath(String xnFilePath) { this.xnFilePath = xnFilePath; } public String getNginxfile() { return nginxfile; } public void setNginxfile(String nginxfile) { this.nginxfile = nginxfile; } }