1、文件上传
1.后台代码:
定制网站顺便在上个demo的controller层写的。
package com.example.controller;import com.fasterxml.jackson.annotation.JsonIgnoreProperties;import org.springframework.web.bind.annotation.RequestMapping;import org.springframework.web.bind.annotation.RequestParam;import org.springframework.web.bind.annotation.RestController;import org.springframework.web.multipart.MultipartFile;import java.util.UUID;import java.io.File;@RestController@JsonIgnoreProperties({"hibernateLazyInitializer", "handler"})@RequestMapping("/fileUpload")public class FileUploadController { @RequestMapping("/fileUploadTest") public String fileUploadTest(@RequestParam("file") MultipartFile file) throws Exception { //定制网站检测是否为空 if (file.isEmpty()) { return "false"; } String fileName = file.getOriginalFilename(); //定制网站定制网站生成独一无二的文件名 fileName = makeFileName(fileName); //定制网站得到分散后的目录路径 String realPath = makeDirPath(fileName, "D:\\fileSave"); File dest = new File(new File(realPath).getAbsolutePath() + "/" + fileName); if (!dest.getParentFile().exists()) { dest.getParentFile().mkdirs(); } try { file.transferTo(dest); // 保存文件 return "true"; } catch (Exception e) { e.printStackTrace(); return "false"; } } //生成独一无二的文件名 private String makeFileName(String fileName) { //使用下划线把UUID和文件名分割开来,后面可能会解析文件名的。 return UUID.randomUUID().toString() + "_" + fileName; } //使用hashCode算法来打散保存的目录 private String makeDirPath(String fileName, String path) { //通过文件名来算出一级目录和二级目录 int hashCode = fileName.hashCode(); int dir1 = hashCode & 0xf; int dir2 = (hashCode & 0xf0) >> 4; String dir = path + "\\" + dir1 + "\\" + dir2; //如果该目录不存在,就创建目录 File file = new File(dir); if (!file.exists()) { file.mkdirs(); } //返回全路径 return dir; }}
- 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
2.postman测试接口
a.选择post进行测试
request URL: http://localhost:8080/fileUpload/fileUploadTest
路径问题参考@RequestMapping("/fileUpload") 依次下级
(具体是怎么弄也不清楚,自己试出来的,有懂的大佬还望指教。。)
b.填写Headers
Key:Content-Type
Value:multipart/form-data
[{“key”:“Content-Type”,“value”:“multipart/form-data”,“description”:"",“enabled”:true}]
c.填写body
选择form-data
key 最右边选择file ,并且打字输入file
(后台@RequestParam(“file”) )
value 选择文件file 进行操作
点击添加文件,最后发送即可
d.下方表示上传成功
e.查看本地目录,随机前缀和随机二级目录名
2、下载文件
1.后台代码:
只是简单测试,选择本地文件
@RequestMapping("/fileDownLoadTest") public void downloadLocal(HttpServletResponse response) throws FileNotFoundException { // 下载本地文件 // 文件的默认保存名 String fileName = "aabo_01.png".toString(); // 读到流中 // 文件的存放路径 InputStream inStream = new FileInputStream("D:/fileSave/aabo_01.png"); // 设置输出的格式 response.reset(); response.setContentType("bin"); response.addHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\""); // 循环取出流中的数据 byte[] b = new byte[100]; int len; try { while ((len = inStream.read(b)) > 0) { response.getOutputStream().write(b, 0, len); } inStream.close(); } catch (IOException e) { e.printStackTrace(); } }
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
2.postman测试接口
选择 get 请求
request URL: http://localhost:8080/fileDownLoad/fileDownLoadTest
后台代码无参数,Params不填写
务必选择 点击Send and Download 按钮,执行请求的同时会下载文件
直接Send 会返回乱码