java - 在 Spring Boot 应用程序中从数据库内容生成 HTML 文件
问题描述
我不知道如何使用REST api生成带有帮助数据库内容的 HTML 文件
目前我们直接将 HTML 源代码存储在user表下的LinkAction列中
标题 | 链接动作 |
---|---|
文件名 | <html><head><title>Title</title></head><body><h1>Heading</h1><p>paragraph</p></body></html> |
我的要求是从 html 源代码生成html 文件,并且 html 文件名应该是标题值。
请帮我解决这个问题
解决方案
最后,我能够生成和下载 HTML 文件。以下是我的完整源代码,可能对某些人有所帮助。
public ResponseEntity<Resource> exportToExcel() throws IOException {
DateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd_HHmmss");
String currentDateTime = dateFormatter.format(new Date());
String filename = "cards_";
List<String> filenameHtml = contentManageService.getHtmlFileName();
//Creating Execl file and adding Cards content to it
Path cardsExcelPath = Files.createTempFile(filename, ".xlsx");
log.info("Cards excel: " + cardsExcelPath.toAbsolutePath());
FileOutputStream cardsXlsWriter = new FileOutputStream(cardsExcelPath.toFile());
//Creating zip file and adding above Cards Excel file
Path zipPath = Files.createTempFile(filename, ".zip");
log.info("Zipfile path: " + zipPath.toAbsolutePath());
ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(zipPath.toFile()));
zipOutputStream.putNextEntry(new ZipEntry(cardsExcelPath.getFileName().toString()));
final byte[] cardsBuffer = new byte[1024];
int length;
ByteArrayInputStream byteArrayInputStream = contentManageService.listAll();
while ((length = byteArrayInputStream.read(cardsBuffer)) >= 0) {
cardsXlsWriter.write(cardsBuffer);
}
cardsXlsWriter.flush();
cardsXlsWriter.close();
final byte[] buffer = new byte[1024];
FileInputStream cardsXlsFileInputStream = new FileInputStream(cardsExcelPath.toFile());
while ((length = cardsXlsFileInputStream.read(buffer)) > 0) {
zipOutputStream.write(buffer);
}
zipOutputStream.closeEntry();
//Creating HTML file and adding Cards content to it
for (int i = 0; i < filenameHtml.size(); i++) {
zipOutputStream.putNextEntry(new ZipEntry(filenameHtml.get(i).concat(".html")));
Path cardsHtmlPath = Files.createTempFile(filenameHtml.get(i), ".html");
log.info("Cards html: " + cardsHtmlPath.toAbsolutePath());
FileOutputStream cardsHtmlWriter = new FileOutputStream(cardsHtmlPath.toFile());
final byte[] htmlBuffer = new byte[1024];
int htmlLength;
List<String> htmlContentList = contentManageService.getHtmlContentFilesExport();
String initialString = htmlContentList.get(i);
// ByteArrayInputStream byteArrayInputStream = contentManageService.getHtmlContentFilesExport();
InputStream targetStream = new ByteArrayInputStream(initialString.getBytes());
while ((htmlLength = targetStream.read(htmlBuffer)) >= 0) {
cardsHtmlWriter.write(htmlBuffer);
}
cardsHtmlWriter.flush();
cardsHtmlWriter.close();
final byte[] bufferHtml = new byte[1024];
FileInputStream cardsHtmlFileInputStream = new FileInputStream(cardsHtmlPath.toFile());
while ((htmlLength = cardsHtmlFileInputStream.read(bufferHtml)) > 0) {
zipOutputStream.write(bufferHtml);
}
}
zipOutputStream.closeEntry();
zipOutputStream.flush();
zipOutputStream.close();
return ResponseEntity.ok()
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=" + filename + currentDateTime)
.contentType(MediaType.parseMediaType("application/zip"))
.body(new FileSystemResource(zipPath));
}
推荐阅读
- java - ThreadLocalRandom double 2 十进制
- url - 从 URL 访问 Samba 共享文件
- group-by - SELECT:按函数结果分组
- acumatica - 票据窗口未更新
- vba - 无法将字符串从 VBA (Excel) 传递到我的 COM 对象
- swift - 使用 Swift 在 Mac 上显示通知横幅
- multithreading - Jenkins 流水线脚本 - 线程编程
- .net - ZetPDF - 有人知道这个产品的背景吗?
- r - xgb.DMatrix 在 mclapply 中挂起
- llvm - 如何在 LLVM 中标记指令并在编译后识别这些指令?