常用的springboot+scala实现的webservice上传下载,
maven配置文件采用常规配置,见springboot+scala配置
后端代码
FileController.scala
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69package org.tashaxing.controller import java.io._ import org.springframework.web.bind.annotation._ import org.springframework.core.io.{InputStreamResource, Resource} import org.springframework.http.ResponseEntity import org.springframework.http.HttpHeaders import org.springframework.http.HttpStatus import org.springframework.http.MediaType import org.springframework.web.multipart.MultipartFile @RestController @RequestMapping(Array("/api")) class FileController { @PostMapping(Array("/file/upload/{myparam}")) def uploadFile(@PathVariable(value = "myparam") myparam: String, // extra param submitted @RequestBody file: MultipartFile): Boolean = { if (file.isEmpty) return false val filename = file.getOriginalFilename val suffix = filename.substring(filename.lastIndexOf('.')) // save to directory // here do sth to handle the file record, maybe insert record to database val save_dir = "D:/file" // absolute and relative path are both OK val save_file = new File(save_dir + '/' + filename) // we can customize the filename, make sure the suffix is right file.transferTo(save_file) // save to file system return true } @PostMapping(Array("/file/delete/{filename}")) def deleteReceipt(@PathVariable(value = "filename") filename: String): Boolean = { // first maybe delete record from database // delete from file system val file_dir = "D:/file" // absolute and relative path are both OK val file = new File(file_dir + '/' + filename) if (file.exists()) file.delete() return true } @GetMapping(Array("/file/download/{filename}")) def downloadReceipt(@PathVariable(value = "filename") filename: String): ResponseEntity[Resource] = { // just support download pdf for this demo val headers = new HttpHeaders headers.setContentType(MediaType.parseMediaType("application/pdf")) // maybe other format: application/png val file = new File(FbConfig.file_root + '/' + filename) val resource = new InputStreamResource(new FileInputStream(file)) return ResponseEntity.ok() .headers(headers) .contentLength(file.length()) .contentType(MediaType.parseMediaType("application/pdf")) .body(resource) } }
前端代码
index.html
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14<!DOCTYPE html> <html lang="en"> <head> <title>Getting Started: Serving Web Content</title> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> </head> <body> <p>Get your greeting <a href="/greeting">here</a></p> <form action="/upload" method="POST" enctype="multipart/form-data"> 文件:<input type="file" name="file"/> <input type="submit" /> </form> <a href="/download">下载</a> </html>
这样就简单实现了一个上传下载功能,需要注意的是
- 前端的input空间里面name要和后端的upload函数里面的参数对应,如:“file”
- 前端可以用react之类的框架包装,但是写法基本一致,上传用表单,下载用href链接
- 如果要支持多种文件格式,后端基本不用改,只要在下载的函数里面改一下文件格式
- 需要在springboot的配置文件里面指定上传下载的文件大小上限
最后
以上就是痴情超短裙最近收集整理的关于sprintboot+scala文件上传下载的全部内容,更多相关sprintboot+scala文件上传下载内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复