SpringBoot 文件上传
SpringBoot文件上传
1.单文件上传
FileUploadController.java
package com.zhuantai.fileupload.controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;
/**
* @author ANTIA1
* @date 2021/7/7 20:26
*/
@RestController
public class FileUploadController {
//注意前后的"/"
// /2020/07/07/
SimpleDateFormat sdf = new SimpleDateFormat("/yyyy/MM/dd/");
/**
* @param file 要和前端表单中文件上传框的name属性保持一致
* @return
*/
@PostMapping("/upload")
public String upload(MultipartFile file, HttpServletRequest req){
String realPath = req.getServletContext().getRealPath("/");
String format = sdf.format(new Date());
String path = realPath + format;
File folder = new File(path);
if (!folder.exists()){
folder.mkdirs();//mkdirs
}
String oldName = file.getOriginalFilename();//原文件名
String newName = UUID.randomUUID().toString() + oldName.substring(oldName.lastIndexOf("."));
try {
file.transferTo(new File(folder,newName));
//req.getScheme 获取请求协议 http || https
String s = req.getScheme() + "://" + req.getServerName() + ":" + req.getServerPort() + format + newName;
return s;
} catch (IOException e) {
e.printStackTrace();
}
return "";
}
}
01.html
<!DOCTYPE html>
<html lang="zh">
<head>
<title>Title</title>
</head>
<body>
<form action="/upload" method="post" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" value="上传">
</form>
</body>
</html>
访问:localhost:8080/01.html
2.多文件上传
多文件上传(一)
02.html
<!DOCTYPE html>
<html lang="en">
<head>
<title>Title</title>
</head>
<body>
<form action="/upload2" method="post" enctype="multipart/form-data">
<input type="file" name="files" multiple> <!--multiple可以选择多个文件-->
<input type="submit" value="上传">
</form>
</body>
</html>
FileUploadController.java
package com.zhuantai.fileupload.controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;
/**
* @author ANTIA1
* @date 2021/7/7 20:51
*/
@RestController
public class FileUploadController2 {
//注意前后的"/"
// /2020/07/07/
SimpleDateFormat sdf = new SimpleDateFormat("/yyyy/MM/dd/");
/**
* 多文件上传
* @param files 要和前端表单中文件上传框的name属性保持一致
* @return
*/
@PostMapping("/upload2")
public void upload(MultipartFile[] files, HttpServletRequest req){
String realPath = req.getServletContext().getRealPath("/");
String format = sdf.format(new Date());
String path = realPath + format;
File folder = new File(path);
if (!folder.exists()){
folder.mkdirs();//mkdirs
}
try {
for (MultipartFile file : files) {
String oldName = file.getOriginalFilename();//原文件名
String newName = UUID.randomUUID().toString() + oldName.substring(oldName.lastIndexOf("."));
file.transferTo(new File(folder,newName));
//req.getScheme 获取请求协议 http || https
String s = req.getScheme() + "://" + req.getServerName() + ":" + req.getServerPort() + format + newName;
System.out.println("s = " + s);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
多文件上传(二)
FileUploadController2.java
package com.zhuantai.fileupload.controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;
/**
* @author ANTIA1
* @date 2021/7/7 20:51
*/
@RestController
public class FileUploadController2 {
//注意前后的"/"
// /2020/07/07/
SimpleDateFormat sdf = new SimpleDateFormat("/yyyy/MM/dd/");
/**
* 多文件上传
* @param files 要和前端表单中文件上传框的name属性保持一致
* @return
*/
@PostMapping("/upload2")
public void upload(MultipartFile[] files, HttpServletRequest req){
String realPath = req.getServletContext().getRealPath("/");
String format = sdf.format(new Date());
String path = realPath + format;
File folder = new File(path);
if (!folder.exists()){
folder.mkdirs();//mkdirs
}
try {
for (MultipartFile file : files) {
String oldName = file.getOriginalFilename();//原文件名
String newName = UUID.randomUUID().toString() + oldName.substring(oldName.lastIndexOf("."));
file.transferTo(new File(folder,newName));
//req.getScheme 获取请求协议 http || https
String s = req.getScheme() + "://" + req.getServerName() + ":" + req.getServerPort() + format + newName;
System.out.println("s = " + s);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
3.Ajax文件上传
ajax.html
<!DOCTYPE html>
<html lang="en">
<head>
<title>Title</title>
<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.js"></script>
</head>
<body>
<div id="result"></div>
<input type="file" id="file">
<input type="button" value="上传" onclick="uploadFile()">
<script>
function uploadFile(){
/* --$("#file")[0]-- jquery对象转javascript对象
* --files[0]-- 第1个文件对象
* */
var file = $("#file")[0].files[0];
var formData = new FormData();
formData.append("file",file);//和后端形参名保持一致
$.ajax({
type:"post",
url:"/upload",
processData:false,
contentType:false,
data:formData,
success:function (msg){
$("#result").html(msg);
}
})
}
</script>
</body>
</html>
后端java同以上Controller.