-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileController.java
More file actions
87 lines (79 loc) · 4.01 KB
/
FileController.java
File metadata and controls
87 lines (79 loc) · 4.01 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
package com.adam.rest_demo_project;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.util.StreamUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.Map;
@Controller
@RequestMapping("/file")
@Slf4j
public class FileController {
private final String fileSeparator = System.getProperty("file.separator");
private final String rootPath = File.listRoots()[0].getPath() + fileSeparator + "api_doc_center";
@ResponseBody
@RequestMapping("/upload")
public Response<Map<String,String>> upload(@RequestParam Map<String,String> paramMap, MultipartFile file_data) {
MultipartFile file = file_data;
long userId = 9999L;
log.debug("upload file {} {} {} user id={}", paramMap, file.getName(), file.getOriginalFilename(), userId);
try {
String now = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyyMMddHHmmss"));
File file1 = new File(rootPath + fileSeparator + userId + fileSeparator + now + file.getOriginalFilename());
if(file1.mkdirs()) {
file.transferTo(file1);
String downloadUrl = "/file/download?userId=" + userId + "&fileName=" + now + file.getOriginalFilename();
Map<String, String> dataMap = new HashMap<>();
dataMap.put("filename", file.getOriginalFilename());
dataMap.put("downloadUrl", downloadUrl);
dataMap.putAll(paramMap);
return Response.success(dataMap);
} else {
return Response.fail("上传失败");
}
} catch (IOException e) {
log.error("upload file error userId={} file={}", userId, file.getOriginalFilename(), e);
return Response.fail("上传失败");
}
}
@RequestMapping("/download")
public void downloadLocal(@RequestParam long userId, @RequestParam String fileName, HttpServletResponse response) throws IOException {
String path = rootPath + fileSeparator + userId + fileSeparator + fileName;
File file = new File(path);
if(!file.exists()) {
throw new FileNotFoundException("未找到文件");
}
// 读到流中
InputStream inputStream = new FileInputStream(path);// 文件的存放路径
response.reset();
response.setContentType("application/octet-stream");
String filename = new File(path).getName();
response.addHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(filename, "UTF-8"));
ServletOutputStream outputStream = response.getOutputStream();
byte[] b = new byte[1024];
int len;
//从输入流中读取一定数量的字节,并将其存储在缓冲区字节数组中,读到末尾返回-1
while ((len = inputStream.read(b)) > 0) {
outputStream.write(b, 0, len);
}
inputStream.close();
}
@RequestMapping("/paramTest2")
public void paramTest2(@RequestParam Map<String,Object> paramMap, @RequestParam Map<String, MultipartFile> fileMap, HttpServletResponse response) throws IOException {
log.debug("paramTest paramMap={} fileMap={}", paramMap, fileMap);
response.setContentType("application/octet-stream");
MultipartFile file0 = fileMap.values().toArray(new MultipartFile[fileMap.size()])[0];
String filename = file0.getOriginalFilename();
response.addHeader("Content-Disposition", "attachment; filename=" + URLEncoder.encode(filename, "UTF-8"));
StreamUtils.copy(file0.getInputStream(), response.getOutputStream());
}
}