Spring Boot MVC 单张图片和多张图片上传 和通用文件下载

@Autowiredprivate ServerConfig serverConfig;/** * 通用下载请求 * * @param fileName 文件名称 * @param delete 是否删除 */@ApiOperation("通用下载请求")@GetMapping("/download")publicvoid fileDownload(String fileName, Boolean delete, HttpServletResponse response, HttpServletRequest request){ try{if(!FileUtils.checkAllowDownload(fileName)) { thrownew Exception(StringUtils.format("文件名称({})非法,不允许下载。", fileName)); } String realFileName = System.currentTimeMillis() + fileName.substring(fileName.indexOf("_") + 1); String filePath = RuoYiConfig.getDownloadPath() + fileName; response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE); FileUtils.setAttachmentResponseHeader(response, realFileName); FileUtils.writeBytes(filePath, response.getOutputStream()); if (delete) { FileUtils.deleteFile(filePath); } } catch (Exception e) { log.error("下载文件失败", e); }}/** * 通用上传请求 */@ApiOperation("通用上传请求")@PostMapping("/upload")public AjaxResult uploadFile(MultipartFile file) throwsException{try{//上传文件路径 String filePath =RuoYiConfig.getUploadPath();//上传并返回新文件名称 String fileName = FileUploadUtils.upload(filePath, file); String url = serverConfig.getUrl() + fileName; AjaxResult ajax = AjaxResult.success(); ajax.put("fileName", fileName); ajax.put("url", url); return ajax; } catch (Exception e) { return AjaxResult.error(e.getMessage()); }}@RequestMapping("xxx")public String fileImgSave(@RequestParam("filename") MultipartFile[] files, HttpServletRequest request){ //保存文件的路径 String realPath =RuoYiConfig.getUploadPath(); //request.getSession().getServletContext().getRealPath("/imgssss"); File path = newFile(realPath);if(!path.exists()){ path.mkdirs(); } //判断file数组不能为空并且长度大于0if(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(newFile(realPath+"/img"+i+".png")); } catch (IOException e) { e.printStackTrace(); } } } } return"ok";}/** * 通用上传请求 */@ApiOperation("通用上传请求")@PostMapping("/uploadList")public AjaxResult uploadFileList(MultipartFile [] file,HttpServletRequest request) throws Exception{ List<String> fileNameList=newArrayList<>(); List<String> urllList=newArrayList<>();try{//上传文件路径qinm String filePath =RuoYiConfig.getUploadPath();//判断file数组不能为空并且长度大于0if (file != null && file.length > 0) { for(int i = 0; i < file.length; i++) { { MultipartFile files =file[i];//上传并返回新文件名称 String fileName = FileUploadUtils.upload(filePath, files); String url = serverConfig.getUrl() + fileName; fileNameList.add(fileName); urllList.add(url); } } AjaxResult ajax = AjaxResult.success(); ajax.put("fileName", fileNameList); ajax.put("url", urllList); return ajax; } else{returnAjaxResult.error("请选择上传的图片"); } } catch (Exception e) { return AjaxResult.error(e.getMessage()); }}/** * 本地资源通用下载 */@ApiOperation("本地资源通用下载")@GetMapping("/download/resource")publicvoid resourceDownload(String resource, HttpServletRequest request, HttpServletResponse response) throwsException{try{if(!FileUtils.checkAllowDownload(resource)) { thrownew Exception(StringUtils.format("资源文件({})非法,不允许下载。", resource)); } //本地资源路径 String localPath =RuoYiConfig.getProfile();//数据库资源地址 String downloadPath = localPath + StringUtils.substringAfter(resource, Constants.RESOURCE_PREFIX); //下载名称 String downloadName = StringUtils.substringAfterLast(downloadPath,"http://www.likecs.com/"); response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE); FileUtils.setAttachmentResponseHeader(response, downloadName); FileUtils.writeBytes(downloadPath, response.getOutputStream()); } catch (Exception e) { log.error("下载文件失败", e); }}

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zwdzwg.html