目录
一、单文件上传
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="/upload" method="post" enctype="multipart/form-data">
<input type="file" name="flie">
<input type="submit" value="上传">
</form>
</body>
</html>
@RestController
public class uploadController {
SimpleDateFormat sdf = new SimpleDateFormat("/yyyy/MM/dd/");
@PostMapping("/upload")
public String upload(MultipartFile flie, HttpServletRequest request) {
String realPath = request.getServletContext().getRealPath("/");
String format = sdf.format(new Date());
String path = realPath + format;
File folds = new File(path);
if(!folds.exists()){
folds.mkdirs();
}
String oldName = flie.getOriginalFilename();
String newName = UUID.randomUUID().toString()+oldName.substring(oldName.lastIndexOf("."));
try {
flie.transferTo(new File(folds,newName));
String s = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + format + newName;
return s;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
二、多文件上传
2.1 多文件选择方式
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<form action="/uploads1" method="post" enctype="multipart/form-data">
<input type="file" name="flies" multiple>
<input type="submit" value="上传">
</form>
</body>
</html>
@PostMapping("/uploads1")
public List<String> uploads1(MultipartFile flies[], HttpServletRequest request) {
String realPath = request.getServletContext().getRealPath("/");
String format = sdf.format(new Date());
String path = realPath + format;
File folds = new File(path);
if(!folds.exists()){
folds.mkdirs();
}
List<String> list = new ArrayList<>();
try {
for (MultipartFile flie : flies) {
String oldName = flie.getOriginalFilename();
String newName = UUID.randomUUID().toString()+oldName.substring(oldName.lastIndexOf("."));
flie.transferTo(new File(folds,newName));
String s = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + format + newName;
list.add(s);
}
} catch (IOException e) {
e.printStackTrace();
}
return list;
}
2.2 多个参数方式
<!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="flie1">
<input type="file" name="flie2">
<input type="submit" value="上传">
</form>
</body>
</html>
@PostMapping("/uploads2")
public List<String> uploads2(MultipartFile flie1,MultipartFile flie2, HttpServletRequest request) {
List<MultipartFile> flies = new ArrayList<>();
flies.add(flie1);
flies.add(flie2);
String realPath = request.getServletContext().getRealPath("/");
String format = sdf.format(new Date());
String path = realPath + format;
File folds = new File(path);
if(!folds.exists()){
folds.mkdirs();
}
List<String> list = new ArrayList<>();
try {
for (MultipartFile flie : flies) {
String oldName = flie.getOriginalFilename();
String newName = UUID.randomUUID().toString()+oldName.substring(oldName.lastIndexOf("."));
flie.transferTo(new File(folds,newName));
String s = request.getScheme() + "://" + request.getServerName() + ":" + request.getServerPort() + format + newName;
list.add(s);
}
} catch (IOException e) {
e.printStackTrace();
}
return list;
}
其实我这里写demo遇到个问题,当我把静态文件放到static下,访问上传的图片是正常的,但是若是我自定义一个文件目录,如实现WebMvcConfigurer接口并定义
registry.addResourceHandler("/**").addResourceLocations("classpath:/dist/");时,出现上传资源访问时被拦截问题,没研究明白,有哪位道友可以说说其中门道呢...