JAVA导出样例文件功能的实现
public ResponseEntity<InputStreamResource> downloadSampleFile(String filePath) {
File sampleFile= new File(sampleFilePath + "/" + filePath);
if (!sampleFile.exists()) {
#throw some excption;
}
String fileName = filePath.substring(filePath.lastIndexOf("/") + 1, filePath.length());
ResponseEntity<InputStreamResource> response = null;
HttpHeaders headers = new HttpHeaders();
try {
headers.add("Cache-Control", "no-cache, no-store, must-revalidate");
headers.add("Content-Disposition", "attachment; filename=" + URLEncoder.encode(fileName, "UTF-8"));
headers.add("Pragma", "no-cache");
headers.add("Expires", "0");
response = ResponseEntity.ok().headers(headers).contentType(MediaType.parseMediaType("application/xlsx"))
.body(new InputStreamResource(new BufferedInputStream(new FileInputStream(sampleFile))));
} catch (IOException e) {
#throw some excption;
}
return response;
}