Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit e467151

Browse files
committed
Java:MultiDataSource 通过缓存提升 /download 静态资源的接口性能
1 parent 26191b8 commit e467151

File tree

2 files changed

+59
-17
lines changed

2 files changed

+59
-17
lines changed

APIJSON-Java-Server/APIJSONBoot-MultiDataSource/src/main/java/apijson/ExcelUtil.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ private static List<List<Object>> prepareData(List<DetailItem> detailItems, int
329329
detailRow.add(item.getCorrectCount());
330330
detailRow.add(item.getFalsePositiveCount());
331331
// G-M: 公式计算列 (注意所有列号都已更新)
332-
detailRow.add(createFormulaCell(String.format("D%d-E%d", currentRow, currentRow))); // G: 漏检数
332+
detailRow.add(createFormulaCell(String.format("C%d-D%d", currentRow, currentRow))); // G: 漏检数
333333
// detailRow.add(createFormulaCell(String.format("IFERROR(E%d/D%d,0)", currentRow, currentRow))); // H: 召回率
334334
// detailRow.add(createFormulaCell(String.format("IFERROR(E%d/(E%d+F%d),0)", currentRow, currentRow, currentRow))); // I: 精准率
335335
// detailRow.add(createFormulaCell(String.format("IFERROR(2*I%d*H%d/(I%d+H%d),0)", currentRow, currentRow, currentRow, currentRow))); // J: F1 Score

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

Lines changed: 58 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
package apijson.boot;
22

33
import java.io.*;
4-
import java.util.ArrayList;
5-
import java.util.Arrays;
6-
import java.util.List;
7-
import java.util.Objects;
4+
import java.net.URLEncoder;
5+
import java.text.DateFormat;
6+
import java.util.*;
87

98
//import javax.annotation.PostConstruct;
109

10+
import apijson.ExcelUtil;
11+
import apijson.StringUtil;
1112
import org.apache.commons.io.FileUtils;
1213
import org.springframework.core.io.InputStreamResource;
1314
import org.springframework.http.HttpHeaders;
@@ -25,6 +26,8 @@
2526

2627
import apijson.demo.DemoParser;
2728

29+
import static com.google.common.io.Files.getFileExtension;
30+
2831
/**文件相关的控制器,包括上传、下载、浏览等
2932
* @author : ramostear
3033
* @modifier : Lemon
@@ -104,18 +107,21 @@ public boolean accept(File file) {
104107
@ResponseBody
105108
public JSONObject upload(@RequestParam("file") MultipartFile file) {
106109
try {
107-
File convertFile = new File(fileUploadRootDir + file.getOriginalFilename());
110+
String name = file.getOriginalFilename();
111+
name = (StringUtil.isEmpty(name) ? DateFormat.getDateInstance().format(new Date()) : name)
112+
.replaceAll("[^a-zA-Z0-9._-]", String.valueOf(Math.round(1100*Math.random())));
113+
File convertFile = new File(fileUploadRootDir + name);
108114
FileOutputStream fileOutputStream;
109115
fileOutputStream = new FileOutputStream(convertFile);
110116
fileOutputStream.write(file.getBytes());
111117
fileOutputStream.close();
112118

113119
if (fileNames != null && ! fileNames.isEmpty()) {
114-
fileNames.add(file.getOriginalFilename());
120+
fileNames.add(name);
115121
}
116122

117123
JSONObject res = new JSONObject();
118-
res.put("path", "/download/" + file.getOriginalFilename());
124+
res.put("path", "/download/" + name);
119125
res.put("size", file.getBytes().length);
120126
return new DemoParser().extendSuccessResult(res);
121127
}
@@ -132,40 +138,76 @@ public ResponseEntity<Object> download(@PathVariable(name = "fileName") String f
132138
File file = new File(fileUploadRootDir + fileName);
133139
InputStreamResource resource = new InputStreamResource(new FileInputStream(file));
134140

141+
String encodedFileName = fileName;
142+
try {
143+
encodedFileName = URLEncoder.encode(fileName, StringUtil.UTF_8);
144+
} catch (UnsupportedEncodingException e) {
145+
e.printStackTrace();
146+
}
147+
135148
HttpHeaders headers = new HttpHeaders();
136-
headers.add("Content-Disposition", String.format("attachment;filename=\"%s", fileName));
137-
headers.add("Cache-Control", "no-cache,no-store,must-revalidate");
138-
headers.add("Pragma", "no-cache");
139-
headers.add("Expires", "0");
149+
headers.add("Content-Disposition", String.format("attachment;filename=\"%s;filename*=UTF_8''%s", fileName, encodedFileName));
150+
headers.add("Cache-Control", "public, max-age=86400");
151+
// headers.add("Cache-Control", "no-cache,no-store,must-revalidate");
152+
// headers.add("Pragma", "no-cache");
153+
// headers.add("Expires", "0");
140154

141155
ResponseEntity<Object> responseEntity = ResponseEntity.ok()
142156
.headers(headers)
143157
.contentLength(file.length())
144-
.contentType(MediaType.parseMediaType("application/txt"))
158+
.contentType(determineContentType(fileName))
145159
.body(resource);
146160

147161
return responseEntity;
148162
}
149163

164+
private MediaType determineContentType(String fileName) {
165+
String extension = getFileExtension(fileName).toLowerCase();
166+
switch (extension) {
167+
case "jpg":
168+
case "jpeg":
169+
return MediaType.IMAGE_JPEG;
170+
case "png":
171+
return MediaType.IMAGE_PNG;
172+
case "gif":
173+
return MediaType.IMAGE_GIF;
174+
case "pdf":
175+
return MediaType.APPLICATION_PDF;
176+
case "json":
177+
return MediaType.APPLICATION_JSON;
178+
case "xml":
179+
return MediaType.APPLICATION_XML;
180+
case "txt":
181+
return MediaType.TEXT_PLAIN;
182+
case "css":
183+
return MediaType.valueOf("text/css");
184+
case "js":
185+
return MediaType.valueOf("application/javascript");
186+
default:
187+
return MediaType.APPLICATION_OCTET_STREAM;
188+
}
189+
}
190+
150191
@GetMapping("/download/report/{reportId}/cv")
151192
@ResponseBody
152193
public ResponseEntity<Object> downloadCVReport(@PathVariable(name = "reportId") String reportId) throws FileNotFoundException, IOException {
153194
String name = "CVAuto_report_" + reportId + ".xlsx";
154-
File file = new File(fileUploadRootDir + name);
195+
String path = fileUploadRootDir + name;
196+
File file = new File(path);
155197
long size = file.exists() ? file.length() : 0;
156198
if (size < 10*1024) {
157199
file.delete();
158200
}
159201

160202
if ((file.exists() ? file.length() : 0) < 10*1024) {
161203
String filePath = ExcelUtil.newCVAutoReportWithTemplate(fileUploadRootDir, name);
162-
if (! Objects.equals(filePath, fileUploadRootDir + name)) {
204+
if (! Objects.equals(filePath, path)) {
163205
try {
164206
File sourceFile = new File(filePath);
165-
File destFile = new File(fileUploadRootDir + name);
207+
File destFile = new File(path);
166208
if (! destFile.getAbsolutePath().equals(sourceFile.getAbsolutePath())) {
167209
FileUtils.copyFile(sourceFile, destFile);
168-
System.out.println("文件复制完成 (Commons IO): " + filePath + " -> " + fileUploadRootDir + name);
210+
System.out.println("文件复制完成 (Commons IO): " + filePath + " -> " + path);
169211
}
170212
} catch (IOException e) {
171213
e.printStackTrace();

0 commit comments

Comments
 (0)