springboot 上传文件

1.application.properties文件

#fileUpload
spring.http.multipart.maxFileSize=100Mb
spring.http.multipart.maxRequestSize=100Mb

2.controller

    @PostMapping("/fileUpload")
    @ResponseBody
    public String uploadFile(@RequestParam("file") MultipartFile file) {
        if (!file.isEmpty()) {
            try {
                String filePath = "src/main/resources/upload/";
                File targetFile = new File(filePath);
                if (!targetFile.exists()) {
                    targetFile.mkdirs();
                }

                BufferedOutputStream out = new BufferedOutputStream(
                        new FileOutputStream(new File(filePath + file.getOriginalFilename())));
                out.write(file.getBytes());
                out.flush();
                out.close();
            } catch (Exception e) {
                // TODO: handle exception
            }
        } else {
            return "文件为空";
        }
        return "文件上传成功";
    }

3.postman测试

springboot 上传文件_第1张图片

你可能感兴趣的:(Java,springboot,上传文件,postman上传文件)