Skip to content

Commit 2f7e20a

Browse files
committed
Java:APIJSONBoot 新增文件 上传、浏览、下载 接口
1 parent a416323 commit 2f7e20a

File tree

2 files changed

+132
-1
lines changed

2 files changed

+132
-1
lines changed

APIJSON-Java-Server/APIJSONBoot/src/main/java/apijson/boot/DemoController.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@
8686
import apijson.orm.exception.OutOfRangeException;
8787

8888

89-
/**request controller
89+
/**数据相关的控制器,包括通用增删改查接口等
9090
* <br > 建议全通过HTTP POST来请求:
9191
* <br > 1.减少代码 - 客户端无需写HTTP GET,PUT等各种方式的请求代码
9292
* <br > 2.提高性能 - 无需URL encode和decode
Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
package apijson.boot;
2+
3+
import java.io.File;
4+
import java.io.FileInputStream;
5+
import java.io.FileNotFoundException;
6+
import java.io.FileOutputStream;
7+
import java.util.ArrayList;
8+
import java.util.List;
9+
10+
import javax.annotation.PostConstruct;
11+
12+
import org.springframework.core.io.InputStreamResource;
13+
import org.springframework.http.HttpHeaders;
14+
import org.springframework.http.MediaType;
15+
import org.springframework.http.ResponseEntity;
16+
import org.springframework.stereotype.Controller;
17+
import org.springframework.web.bind.annotation.GetMapping;
18+
import org.springframework.web.bind.annotation.PathVariable;
19+
import org.springframework.web.bind.annotation.PostMapping;
20+
import org.springframework.web.bind.annotation.RequestParam;
21+
import org.springframework.web.bind.annotation.ResponseBody;
22+
import org.springframework.web.multipart.MultipartFile;
23+
24+
import com.alibaba.fastjson.JSONObject;
25+
26+
import apijson.demo.DemoParser;
27+
28+
/**文件相关的控制器,包括上传、下载、浏览等
29+
* @author : ramostear
30+
* @modifier : Lemon
31+
* @date : 2019/3/8 0008-15:35
32+
*/
33+
@Controller
34+
public class FileController {
35+
36+
private static String fileUploadRootDir = null;
37+
38+
// @Value"${file.upload.root.dir.windows}")
39+
String fileUploadRootDirWindows = "C:/work/upload/";
40+
41+
// @Value"${file.upload.root.dir.mac}")
42+
String fileUploadRootDirMac = "/Users/Tommy/upload/";
43+
44+
// @Value"${file.upload.root.dir.linux}")
45+
String fileUploadRootDirLinux = "~/upload/";
46+
47+
private static List<String> fileRepository = new ArrayList<>();
48+
49+
@PostConstruct
50+
public void initFileRepository(){
51+
// 判断文件夹是否存在,不存在就创建
52+
String osName = System.getProperty("os.name");
53+
if (osName.startsWith("Mac OS")) {
54+
// 苹果
55+
fileUploadRootDir = fileUploadRootDirMac;
56+
} else if (osName.startsWith("Windows")) {
57+
// windows
58+
fileUploadRootDir = fileUploadRootDirWindows;
59+
} else {
60+
// unix or linux
61+
fileUploadRootDir = fileUploadRootDirLinux;
62+
}
63+
64+
File directories = new File(fileUploadRootDir);
65+
if (directories.exists()) {
66+
System.out.println("文件上传根目录已存在");
67+
} else { // 如果目录不存在就创建目录
68+
if (directories.mkdirs()) {
69+
System.out.println("创建多级目录成功");
70+
} else {
71+
System.out.println("创建多级目录失败");
72+
}
73+
}
74+
}
75+
76+
@GetMapping("/files")
77+
@ResponseBody
78+
public JSONObject files() {
79+
JSONObject res = new JSONObject();
80+
res.put("data", fileRepository);
81+
return DemoParser.extendSuccessResult(res);
82+
}
83+
84+
@PostMapping(value = "/upload", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
85+
@ResponseBody
86+
public JSONObject upload(@RequestParam("file") MultipartFile file) {
87+
try {
88+
File convertFile = new File(fileUploadRootDir + file.getOriginalFilename());
89+
FileOutputStream fileOutputStream;
90+
fileOutputStream = new FileOutputStream(convertFile);
91+
fileOutputStream.write(file.getBytes());
92+
fileOutputStream.close();
93+
94+
fileRepository.add(file.getOriginalFilename());
95+
96+
JSONObject res = new JSONObject();
97+
res.put("path", file.getOriginalFilename());
98+
res.put("size", file.getBytes().length);
99+
return DemoParser.extendSuccessResult(res);
100+
}
101+
catch (Exception e) {
102+
e.printStackTrace();
103+
return DemoParser.newErrorResult(e);
104+
}
105+
}
106+
107+
@GetMapping("/download/{fileName}")
108+
@ResponseBody
109+
public ResponseEntity<Object> download(@PathVariable(name = "fileName") String fileName) throws FileNotFoundException {
110+
111+
File file = new File(fileUploadRootDir + fileName);
112+
InputStreamResource resource = new InputStreamResource(new FileInputStream(file));
113+
114+
HttpHeaders headers = new HttpHeaders();
115+
headers.add("Content-Disposition", String.format("attachment;filename=\"%s", fileName));
116+
headers.add("Cache-Control", "no-cache,no-store,must-revalidate");
117+
headers.add("Pragma", "no-cache");
118+
headers.add("Expires", "0");
119+
120+
ResponseEntity<Object> responseEntity = ResponseEntity.ok()
121+
.headers(headers)
122+
.contentLength(file.length())
123+
.contentType(MediaType.parseMediaType("application/txt"))
124+
.body(resource);
125+
126+
return responseEntity;
127+
}
128+
129+
130+
131+
}

0 commit comments

Comments
 (0)