单文件上传
在static中新建upload.html文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="/upload" method="post" enctype="multipart/form-data">
<!-- name这里必须和后台接收名字相同-->
<input type="file" name="file">
<input type="submit" value="上传">
</form>
</body>
</html>
新建FileUploadController类
package org.hx.springboot_uploadfile_demo17;
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;
@RestController
public class FileUploadController {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("/yyyy/MM/dd/");
@PostMapping("/upload")
public String upload(MultipartFile file, HttpServletRequest request){
String realpath = request.getServletContext().getRealPath("/");
String format = simpleDateFormat.format(new Date());
String path = realpath + format;
File folder = new File(path);
if (!folder.exists())
{
folder.mkdirs();
}
String oldName = file.getOriginalFilename();
String newName = UUID.randomUUID().toString()+oldName.substring(oldName.lastIndexOf("."));
try {
file.transferTo(new File(folder,newName));
String s = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+format+newName;
return s;
} catch (IOException e) {
e.printStackTrace();
}
return "";
}
}
多文件上传(一)
新建uploads.html文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="/uploads" method="post" enctype="multipart/form-data">
<input type="file" name="files" multiple>
<input type="submit" value="上传">
</form>
</body>
</html>
新建FileUploadsController类
package org.hx.springboot_uploadfile_demo17;
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;
@RestController
public class FileUploadsController {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("/yyyy/MM/dd/");
@PostMapping("/uploads")
public void upload(MultipartFile[] files, HttpServletRequest request){
String realpath = request.getServletContext().getRealPath("/");
String format = simpleDateFormat.format(new Date());
String path = realpath + format;
File folder = new File(path);
if (!folder.exists())
{
folder.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));
String url = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+format+newName;
System.out.println(url);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
多文件上传(二)
新建uploads2.html文件
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="/uploads2" method="post" enctype="multipart/form-data">
<input type="file" name="file1" >
<input type="file" name="file2" >
<input type="submit" value="上传">
</form>
</body>
</html>
新建FileUploadsController类
package org.hx.springboot_uploadfile_demo17;
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;
@RestController
public class FileUploads2Controller {
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("/yyyy/MM/dd/");
@PostMapping("/uploads2")
public void upload(MultipartFile file1,MultipartFile file2, HttpServletRequest request){
String realpath = request.getServletContext().getRealPath("/");
String format = simpleDateFormat.format(new Date());
String path = realpath + format;
File folder = new File(path);
if (!folder.exists())
{
folder.mkdirs();
}
try {
String oldName1 = file1.getOriginalFilename();
String newName1 = UUID.randomUUID().toString()+oldName1.substring(oldName1.lastIndexOf("."));
file1.transferTo(new File(folder,newName1));
String url1 = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+format+newName1;
System.out.println(url1);
String oldName2 = file2.getOriginalFilename();
String newName2 = UUID.randomUUID().toString()+oldName2.substring(oldName2.lastIndexOf("."));
file2.transferTo(new File(folder,newName2));
String url2 = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+format+newName2;
System.out.println(url2);
} catch (IOException e) {
e.printStackTrace();
}
}
}