准备工作
先建一个springboot项目
添加web依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
页面编写及注意事项
jsp页面
<form action="./upload" method="post" enctype="multipart/form-data">
上传文件:<input type="file" name="file" value="请选择上传的文件">
<input type="submit" value="上传"><br><br>
</form>
注意
form 表单声明为multipart/form-data,如果没有这个声明,Spring MVC会解析文件请求出错
开发控制器类
配置文件配置
#是否启用Spring MVC多分部上传功能
spring.servlet.multipart.enabled=true
# 指定默认上传的文件夹
spring.servlet.multipart.location=E:\work\download
#将文件写入磁盘的阈值。值可以使用后缀MB或KB来表示兆字节或字节大小
spring.servlet.multipart.file-size-threshold=0
#限制单个文件最大大小
spring.servlet.multipart.max-file-size=1MB
#限制所有文件最大大小
spring.servlet.multipart.max-request-size=10MB
1. 入参HttpServletRequest
控制器
package com.example.mvcdemo.controller;/**
* Created by weimingyue on 2021/1/10
*/
import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
/**
* @ClassName: FileController
* @Description TODO
* @Author: weimingyue
* @Date: 2021/1/10 10:08
* @Version: 1.0
*/
@Controller
@RequestMapping("/file")
public class FileController {
@GetMapping("/upload/page")
public String uploadPage(){
return "/file/upload";
}
//
// 使用HttpServletRequest
@PostMapping("/upload/request")
@ResponseBody
public JSONObject uploadReauest(HttpServletRequest request){
JSONObject jsonObject = new JSONObject();
boolean flag=false;
MultipartHttpServletRequest multipartHttpServletRequest = null;
if(request instanceof MultipartHttpServletRequest){
multipartHttpServletRequest = (MultipartHttpServletRequest) request;
}else{
jsonObject.put("msg","传输类型不为file");
return jsonObject;
}
MultipartFile file = multipartHttpServletRequest.getFile("upload");
String fileName = file.getOriginalFilename();
System.out.println(fileName);
File newFile= new File(fileName);
try {
file.transferTo(newFile);
} catch (IOException e) {
e.printStackTrace();
jsonObject.put("msg","保存文件失败");
return jsonObject;
}
return jsonObject;
}
}
2. 直接使用MultipartFile对象
@PostMapping("/upload/multipartFile")
@ResponseBody
public JSONObject uploadMultipartFile(MultipartFile file){
JSONObject jsonObject = new JSONObject();
String name = file.getOriginalFilename();
File dest = new File(name);
try {
file.transferTo(dest);
} catch (IOException e) {
e.printStackTrace();
jsonObject.put("msg","保存文件失败");
return jsonObject;
}
return jsonObject;
}
3. 使用Servlet的API
@PostMapping("/upload/part")
@ResponseBody
public JSONObject uploadPart(Part file){
JSONObject jsonObject = new JSONObject();
String name = file.getSubmittedFileName();
try {
file.write(name);
} catch (IOException e) {
e.printStackTrace();
jsonObject.put("msg","保存文件失败");
return jsonObject;
}
return jsonObject;
}