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测试


猜你喜欢

转载自blog.csdn.net/ran0914/article/details/80701462