springboot2.4 接收文件上传功能
2021/4/22 14:44:0 | 阅1505 | 来源:好空间网络 [打印] [关闭] |
public boolean FileUpload(MultipartFile file, String key, String userid) throws IOException { //生成文件名称 String newFileName = RandomStringGenerator.getRandomStringToNumberByLength2(15); System.out.println("文件名称:"+newFileName); // 获取日期并且格式化 String time = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMdd")); // 处理要上传的文件夹 String uploadPath = myProperties.getPath() +File.separator+ time; System.out.println("保存路径:" + uploadPath); // 如果目录不存在则创建 File uploadDir = new File(uploadPath); if (!uploadDir.exists()) { uploadDir.mkdirs(); } String filename = file.getOriginalFilename();//获取文件名称 String suffixname = filename.substring(filename.lastIndexOf("."));//取后缀 File dest = new File(uploadDir,newFileName+suffixname);// 把路径和新的文件名生成file对象 file.transferTo(dest); // 保存到硬盘 }
点吗比较简单,注释也比较详细,就不在特别说明了