<!--hutool 常用工具--> <dependency> <groupId>cn.hutool</groupId> <artifactId>hutool-all</artifactId> <version>5.4.1</version> </dependency> <!--start poi--> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi</artifactId> <version>4.1.2</version> </dependency> <!-- 导入导出Excel时用到 --> <dependency> <groupId>org.apache.poi</groupId> <artifactId>poi-ooxml</artifactId> <version>4.1.2</version> </dependency> <!--end poi-->
@Resource private HttpServletRequest request;
@ApiOperationSupport(author = "wensm") @ApiOperation(value = "上传logo") @PostMapping(value = "/uploadLogo") public ObjectRestResponse<String> uploadLogo(@RequestParam("file") MultipartFile file) throws IOException { log.info("上传logo"); if (file.isEmpty()) { log.warn("上传失败,请选择文件"); throw new ErpException(ErrorMessagesEnum.ERR_UPLOAD_FAILED); } DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMddHHmmssSSS");//17位 String dateTime_str = LocalDateTime.now().format(formatter); String uri = request.getRequestURI(); String rootPath = System.getProperty("user.dir");//E:IdeaProjectsyq String fileName = file.getOriginalFilename(); //获取最后一个.的位置 int lastIndexOf = fileName.lastIndexOf("."); //获取文件的后缀名 .jpg String suffix = fileName.substring(lastIndexOf); // String filePath = "/Users/itinypocket/workspace/temp/"; String filePath = rootPath+Constants.UPLOAD_DIR+uri+"/"+dateTime_str+suffix; File dest = new File(filePath); if (!dest.exists()) { dest.mkdirs(); } try { file.transferTo(dest); log.info("上传成功"); return new ObjectRestResponse<String>().rel(true).data(filePath.replace("\","/")); } catch (IOException e) { log.error(e.toString(), e); } return new ObjectRestResponse<String>().rel(true); }
@RequestMapping("xxx") public String fileImgSave(@RequestParam("filename") MultipartFile[] files,HttpServletRequest request){ //保存文件的路径 String realPath = request.getSession().getServletContext().getRealPath("/imgssss"); File path = new File(realPath); if(!path.exists()){ path.mkdirs(); } //判断file数组不能为空并且长度大于0 if(files != null && files.length > 0){ //循环获取file数组中得文件 for(int i = 0;i < files.length;i++){ MultipartFile file = files[i]; //保存文件 if (!file.isEmpty()){ try { //转存文件 file.getOriginalFilename();文件原名称包括后缀名 file.transferTo(new File(realPath+"/img"+i+".png")); } catch (IOException e) { e.printStackTrace(); } } } } return "ok"; }