新建工程,只需要引入 web 和 thymeleaf 两个jar就行。
参考:https://blog.csdn.net/nece001/article/details/106301721
只需要建4个文件(用的IDE是Netbeans)
先建三个HTML文件,用作模板:
失败页面:
<!DOCTYPE html>
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<div>上传失败</div>
</body>
</html>
成功页面:
<!DOCTYPE html>
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<div>上传成功</div>
文件标题:[[${title}]]
</body>
</html>
文件上传页:
<!DOCTYPE html>
<html>
<head>
<title>Upload Form</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<form method="post" enctype="multipart/form-data" action="/save">
单个上传
<div>标题:<input type="text" name="title"></div>
<div>选择:<input type="file" name="upfile"></div>
<input type="submit" value="上传">
</form>
<form method="post" enctype="multipart/form-data" action="/save-all">
上传多个文件
<div>标题:<input type="text" name="title"></div>
<div>选择1:<input type="file" name="upfiles"></div>
<div>选择2:<input type="file" name="upfiles"></div>
<div>选择3:<input type="file" name="upfiles"></div>
<input type="submit" value="上传">
</form>
</body>
</html>
控制器类:
package com.example.controller;
import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.UUID;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.multipart.MultipartFile;
@Controller
public class UploadController {
String base = "D:\\test\\java\\1\\upload\\"; // 不要使用项目内的目录
@GetMapping("/upload")
public String upload() {
return "upload";
}
@PostMapping("/save")
public String save(MultipartFile upfile, String title, Map<String, Object> map) throws IOException {
map.put("title", title);
if (upfile.getSize() > 0) {
String origName = upfile.getOriginalFilename();
String saveName = UUID.randomUUID() + origName;
File file = new File(base + saveName);
upfile.transferTo(file);
return "success";
}
return "failure";
}
@PostMapping("/save-all")
public String saveAll(MultipartFile[] upfiles, String title, Map<String, Object> map) throws IOException {
map.put("title", title);
int saveCount = 0;
if (upfiles.length > 0) {
MultipartFile upfile;
for (int i = 0; i < upfiles.length; i++) {
upfile = upfiles[i];
if (upfile.getSize() > 0) {
saveCount++;
String origName = upfile.getOriginalFilename();
String saveName = UUID.randomUUID() + origName;
File file = new File(base + saveName);
upfile.transferTo(file);
}
}
}
return saveCount > 0 ? "success" : "failure";
}
}
增加图片缩略功能:
引入jar包
<dependency>
<groupId>net.coobird</groupId>
<artifactId>thumbnailator</artifactId>
<version>0.4.11</version>
</dependency>
控制器添加代码:
package com.example.controller;
import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.UUID;
import net.coobird.thumbnailator.Thumbnails;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.multipart.MultipartFile;
@Controller
public class UploadController {
String base = "D:\\test\\java\\1\\upload\\";
@GetMapping("/upload")
public String upload() {
return "upload";
}
@PostMapping("/save")
public String save(MultipartFile upfile, String title, Map<String, Object> map) throws IOException {
map.put("title", title);
if (upfile.getSize() > 0) {
String origName = upfile.getOriginalFilename();
String saveName = UUID.randomUUID() + origName;
File file = new File(base + saveName);
upfile.transferTo(file);
File thumb = new File(base+"thumb_"+origName);
Thumbnails.of(file)
.size(200,200)
.outputQuality(0.8f)
.toFile(thumb);
return "success";
}
return "failure";
}
@PostMapping("/save-all")
public String saveAll(MultipartFile[] upfiles, String title, Map<String, Object> map) throws IOException {
map.put("title", title);
int saveCount = 0;
if (upfiles.length > 0) {
MultipartFile upfile;
for (int i = 0; i < upfiles.length; i++) {
upfile = upfiles[i];
if (upfile.getSize() > 0) {
saveCount++;
String origName = upfile.getOriginalFilename();
String saveName = UUID.randomUUID() + origName;
File file = new File(base + saveName);
upfile.transferTo(file);
}
}
}
return saveCount > 0 ? "success" : "failure";
}
}