SpringBoot-fileUpload

一  新建文件   fileUpload.html  用于页面上传




    
    Title




注意建路径

SpringBoot-fileUpload_第1张图片

 2  新建 Controller 

 

package com.example.demo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;

@RestController
public class fileUploadController {

    private static final Logger LOGGER = LoggerFactory.getLogger(fileUploadController.class);



   @RequestMapping(value="/fileUploadController", method = RequestMethod.POST)
    public String fileUpload(@RequestParam("file") MultipartFile file) {
        System.out.println("开始");
        if (file.isEmpty()) {
            return "上传失败,请选择文件";
        }

        String fileName = file.getOriginalFilename();
        String filePath = "E:\\java\\SpringBoot\\SpringBootdom2\\src\\main\\resources\\static\\";
        File dest = new File(filePath + fileName);
        try {
            file.transferTo(dest);
            LOGGER.info("上传成功");
            return "上传成功";
        } catch (IOException e) {
            LOGGER.error(e.toString(), e);
        }
        return "上传失败!";
    }

}

注意文件上传只能是 POST

三  配至文件上传大小

SpringBoot-fileUpload_第2张图片

你可能感兴趣的:(SpringBoot)