SpringBoot上传文件

来源:互联网 发布:地图数据可视化 编辑:程序博客网 时间:2024/05/01 11:26

后台代码

 private String saveFile(MultipartFile file) {        try {            if (file.isEmpty()) {                log.info("file is empty");                return "";            }            String fileName = StringUtils.cleanPath(file.getOriginalFilename());            if (fileName.contains("..")) {                // This is a security check                log.error("Cannot store file with relative path outside current directory {}", fileName);                return "";            }            log.info("ready to save {}, {}", file.getOriginalFilename(), fileName);            String path = "data/upload/";            if (!Paths.get(path).toFile().exists()) {                Paths.get(path).toFile().mkdirs();            }            Files.copy(file.getInputStream(), Paths.get(path).resolve(fileName), StandardCopyOption.REPLACE_EXISTING);            return file.getOriginalFilename();        } catch (Exception e) {            log.error("saveFile Exception {}", e);        }        return "";    }

前台代码

uploadFile(@RequestParam("file") MultipartFile file)

https://github.com/spring-guides/gs-uploading-files