上传文件是我们日常使用最为广泛的功能之一,比如App端上传头像。本章演示如何从客户端上传到 Spring Boot 开发的 Api中。
https://github.com/fishpro/spring-boot-study/tree/master/spring-boot-study-upload
1 新建 Spring Boot Maven 示例工程项目
注意:本示例是用 IDEA 开发工具
文件上传不需要引入第三方组件。
2 依赖引入 Pom.xml
为了演示代码,这里引入 thymeleaf
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
3 编写上传示例
本章代码主要演示单文件上传和多文件上传,前端采用 thymeleaf 模板,实际上就是一个html文件。文件清单包括
3.1 控制层代码
主要使用 MultipartFile 来实现,如下代码 /upload 和 /uploads 分别为单文件上传和多文件上传。其中 @Value("${fishpro.uploadPath}") 是配置文件中设置的。
server.port=8086
fishpro.uploadPath=/Users/jiaojunkang/Desktop/upload/
@Controller
public class FileController {
@Value("${fishpro.uploadPath}")
private String uploadPath;
@GetMapping("/uploadfile")
public String uploadfile(){
return "uploadfile";
}
/**
* 单文件
* */
@PostMapping("/upload")
@ResponseBody
Object upload(@RequestParam("file") MultipartFile file, HttpServletRequest request) {
Map<String,Object> map=new HashMap();
map.put("status",0);
String fileName = file.getOriginalFilename();
fileName = UUID.randomUUID().toString(); //对文件名称重命名
try {
FileUtil.uploadFile(file.getBytes(), uploadPath, fileName);
map.put("filename",fileName);
} catch (Exception e) {
map.put("status",-1);
map.put("message",e.getMessage());
}
return map;
}
/**
* 多文件
* */
@PostMapping("/uploads")
@ResponseBody
Object uploads(@RequestParam("files") MultipartFile [] files, HttpServletRequest request) {
Map<String,Object> map=new HashMap();
map.put("status",0);
List<String> filenames=new ArrayList<>();
for (MultipartFile file: files
) {
String ext = file.getOriginalFilename().split("\\.")[1];
String fileName = file.getOriginalFilename();
//fileName = UUID.randomUUID().toString()+"."+ext; //对文件名称重命名
try {
FileUtil.uploadFile(file.getBytes(), uploadPath, fileName);
filenames.add(fileName);
} catch (Exception e) {
map.put("status",-1);
map.put("message",e.getMessage());
return map;
}
}
map.put("filename",filenames);
return map;
}
}
3.2 前端文件
前端文件这里演示的比较简单,可以有多中形态,这里使用 form 来提交。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<hr/>
<div>单位文件</div>
<form enctype="multipart/form-data" method="post" action="/upload">
文件 <input type="file" name="file"/>
<input type="submit" value="上传"/>
</form>
<hr/>
<div>多文件</div>
<form enctype="multipart/form-data" method="post" action="/uploads">
<p>文件1<input type="file" name="files"/></p>
<p>文件2<input type="file" name="files"/></p>
<p>文件3<input type="file" name="files"/></p>
<p><input type="submit" value="上传"/></p>
</form>
</body>
</html>
3.3 文件保存类
public class FileUtil {
public static void uploadFile(byte[] file, String filePath, String fileName) throws Exception {
File targetFile = new File(filePath);
if (!targetFile.exists()) {
targetFile.mkdirs();
}
FileOutputStream out = new FileOutputStream(filePath + fileName);
out.write(file);
out.flush();
out.close();
}
public static boolean deleteFile(String fileName) {
File file = new File(fileName);
// 如果文件路径所对应的文件存在,并且是一个文件,则直接删除
if (file.exists() && file.isFile()) {
if (file.delete()) {
return true;
} else {
return false;
}
} else {
return false;
}
}
public static String renameToUUID(String fileName) {
return UUID.randomUUID() + "." + fileName.substring(fileName.lastIndexOf(".") + 1);
}
}
以上就是本次介绍的全部知识点内容,感谢大家的阅读和对亿速云的支持。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。