New springboot project
https://blog.csdn.net/qq_43560721/article/details/104653470
Project structure
Creating Controller
package cn.xxs.springbootfileupload.controller;
import java.io.File;
import java.io.IOException;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
@RestController//当前类下的方法返回值自动做json格式的转换
public class FileUploadController {
/**
* 文件上传
* @throws IOException
* @throws IllegalStateException
*/
@RequestMapping("/fileUpload")
public String fileUpload(MultipartFile file) throws IllegalStateException, IOException{
System.out.println(file.getOriginalFilename());
file.transferTo(new File("E:/"+file.getOriginalFilename()));
return "success";
}
}
Start writing class
package cn.xxs.springbootfileupload;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class SpringbootfileuploadApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootfileuploadApplication.class, args);
}
}
Upload files in application.properties file parameters and permissions access path
spring.servlet.multipart.max-file-size=200MB
spring.servlet.multipart.max-request-size=200MB
spring.mvc.static-path-pattern=/static/**
spring.resources.static-locations=classpath:/static
Below two can not be provided, but some versions may not be setting the following 404, I was like this
Set upload page
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="/fileUpload" method="post" enctype="multipart/form-data">
<input type="file" name="file"/>
<br>
<input type="submit" value="上传">
</form>
</body>
</html>
Access project
Browser and enter http: // localhost: 8080 / static / upload.html
Here I made a mistake
Figure lazy direct access
Jumped into the browser, you will not pay attention to see go down (watch carefully you will find the port number is not 8080)
Will lead