SpringBoot单文件和多文件上传,通用的文件上传方法
2019-09-25 07:08
1161 查看
//通用的文件上传方法 public String saveFileByIO(MultipartFile upload, String fileSeparator, String uploadPath, int flag) { if (upload == null) { return ""; } InputStream in = null; try { in = upload.getInputStream(); } catch (IOException e2) { log.debug(e2); } File dir = new File(uploadPath); if (!dir.exists()) { dir.mkdirs(); } String fileName = upload.getOriginalFilename(); if (flag == 1) { String uuidString = UUID.randomUUID().toString(); String suffix = fileName.substring(fileName.indexOf(".")); fileName = uuidString + (suffix != null ? suffix : ""); } String relaPth = uploadPath + fileSeparator + fileName; File checkName = new File(relaPth); String reName = fileName; if (checkName.exists()) { String suffix = fileName.substring(fileName.indexOf(".")); reName = fileName.substring(0, fileName.indexOf(".")) + "_" + DateUtil.getTodayTimeString().replace(" ", "_").replace(":", "-") + (suffix != null ? suffix : ""); relaPth = uploadPath + fileSeparator + reName; } try { upload.transferTo(new File(relaPth)); } catch (IllegalStateException e1) { log.debug(e1); } catch (IOException e1) { log.debug(e1); } return reName; }
单文件上传
public String upload(@RequestParam("file") MultipartFile file) { try { if (file == null || file.isEmpty()) { return "文件为空"; } // 获取文件名 String fileName = file.getOriginalFilename(); log.info("上传的文件名为:" + fileName); // 设置文件存储路径 SimpleDateFormat sdf = new SimpleDateFormat("yyyy" + "/" + "MM" + "/" + "dd"); String subPath = sdf.format(new Date()); String basePath = subPath + "/" + fileName; String rootPath = userFileClient.findConfigByCode("UPLOAD_ROOT_PATH").toString(); String path = rootPath + basePath; File dest = new File(path); // 检测是否存在目录 if (!dest.getParentFile().exists()) { dest.getParentFile().mkdirs();// 新建文件夹 } file.transferTo(dest);// 文件写入 fileinfo = saveFileMessage(file, path); insert(fileinfo); return "上传成功"; } catch (IllegalStateException e) { log.error(e); } catch (IOException e) { log.error(e); } }
多文件上传
public String handleFileUpload(HttpServletRequest request) { List<MultipartFile> files = ((MultipartHttpServletRequest) request).getFiles("file"); MultipartFile file = null; BufferedOutputStream stream = null; for (int i = 0; i < files.size(); ++i) { file = files.get(i); if (!file.isEmpty()) { try { byte[] bytes = file.getBytes(); String fileName = file.getOriginalFilename(); SimpleDateFormat sdf = new SimpleDateFormat("yyyy" + "/" + "MM" + "/" + "dd"); String subPath = sdf.format(new Date()); String basePath = subPath + "/" + fileName; String rootPath = userFileClient.findConfigByCode("UPLOAD_ROOT_PATH").toString(); String path = rootPath + basePath; stream = new BufferedOutputStream(new FileOutputStream( new File(path)));//设置文件路径及名字 stream.write(bytes);// 写入 fileinfo = saveFileMessage(file, path); insert(fileinfo); stream.close(); } catch (Exception e) { stream = null; return "第 " + i + " 个文件上传失败 ==> " + e.getMessage(); } } else { return "第 " + i + " 个文件上传失败因为文件为空"; } } return "上传成功"; }
相关文章推荐
- Spring Boot搭建文件上传服务的方法
- Springboot 文件上传(多个单个一个方法) 文件下载
- spring boot 文件上传接口设计
- 基于spring-boot的web应用,ckeditor上传文件图片文件
- 使用Spring boot + jQuery上传文件(kotlin)
- 解决使用Spring Boot、Multipartfile上传文件路径错误问题
- spring boot文件上传、下载
- 基于SpringBoot上传任意文件功能的实现
- Spring Boot 上传文件(spring boot upload file)
- Spring boot 文件上传(多文件上传)
- spring-boot上传文件MultiPartFile获取不到文件问题解决
- 详细全面的 SpringBoot 文件上传
- springboot实现文件上传和下载功能
- 解决spring boot上传文件MultipartFile找不到,报错的问题
- SpringBoot下文件上传与下载的实现
- SPRINGBOOT之文件上传与下载
- SpringBoot上传单个文件和多个文件
- Asp.net_使用FileUpload控件上传文件通用方法分享
- Springboot 之 多文件上传-知识林
- 29. Spring boot 文件上传(多文件上传)【从零开始学Spring Boot】