从 spring 引导应用程序中的数据库内容生成 HTML 文件
Generate HTML file from database content in spring boot application
我不知道如何使用 REST api
生成包含帮助数据库内容的 HTML 文件
目前我们直接将 HTML 源代码存储在 user table[= 下的 LinkAction 列中14=]
Title
LinkAction
FileName
<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));
}
我使用了 IOUtils.copy() 而不是写入文件。
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");
ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(zipPath.toFile()));
zipOutputStream.putNextEntry(new ZipEntry(cardsExcelPath.getFileName().toString().substring(0,5).concat(".xlsx")));
ByteArrayInputStream byteArrayInputStream = contentManageService.listAll();
IOUtils.copy(byteArrayInputStream, zipOutputStream);
cardsXlsWriter.flush();
cardsXlsWriter.close();
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").replaceAll("\s", "")));
Path cardsHtmlPath = Files.createTempFile(filenameHtml.get(i), ".html");
log.info("Cards html: " + cardsHtmlPath.toAbsolutePath());
FileOutputStream cardsHtmlWriter = new FileOutputStream(cardsHtmlPath.toFile());
List<String> htmlContentList = contentManageService.getHtmlContentFilesExport();
String initialString = htmlContentList.get(i);
InputStream targetStream = new ByteArrayInputStream(initialString.getBytes());
IOUtils.copy(targetStream, zipOutputStream);
cardsHtmlWriter.flush();
cardsHtmlWriter.close();
zipOutputStream.closeEntry();
}
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));
}
DateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd_HHmmss");
String currentDateTime = dateFormatter.format(new Date());
String filename = "Fingertips_";
//Creating Excel file and adding Fingertips content to it
Path cardsExcelPath = Files.createTempFile(filename, ".xlsx");
FileOutputStream cardsXlsWriter = new FileOutputStream(cardsExcelPath.toFile());
//Creating zip file and adding above Fingertips Excel file
Path zipPath = Files.createTempFile(filename, ".zip");
ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(zipPath.toFile()));
zipOutputStream.putNextEntry(new ZipEntry(cardsExcelPath.getFileName().toString().substring(0,10).concat(".xlsx")));
ByteArrayInputStream byteArrayInputStream = fingerTipsService.exportAllContentManageToExcel();
IOUtils.copy(byteArrayInputStream, zipOutputStream);
cardsXlsWriter.flush();
cardsXlsWriter.close();
zipOutputStream.closeEntry();
List<UserContentDTO> htmlContentList = fingerTipsService.getHtmlContentFilesExport();
htmlContentList.stream()
.forEach(userContentDTO -> {
try {
zipOutputStream.putNextEntry(new ZipEntry(userContentDTO.getTitle().replaceAll("\s", "").concat(".html")));
Path cardsHtmlPath = Files.createTempFile(userContentDTO.getTitle(), ".html");
FileOutputStream cardsHtmlWriter = new FileOutputStream(cardsHtmlPath.toFile());
InputStream targetStream = new ByteArrayInputStream(userContentDTO.getLinkactions().getBytes());
IOUtils.copy(targetStream, zipOutputStream);
cardsHtmlWriter.flush();
cardsHtmlWriter.close();
zipOutputStream.closeEntry();
}
catch(Exception e) {
System.out.println("came to exception: "+e);
}
});
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));
}
服务class
public ByteArrayInputStream exportAllContentManageToExcel() {
List<ContentManage> data = userContentManageRepository.findAll(Sort.by("id").ascending());
ByteArrayInputStream in = ContentManageExcelExporter.exportAllContentManageToExcelFile(data);
return in;
}
//To ge the HTML source code from Database
public List<UserContentDTO> getHtmlContentFilesExport()
{
List<UserContentDTO> content = userContentManageRepository.getHtmlContentFiles();
return content;
}
回购 class
@Transactional(rollbackFor = Exception.class)
@Query("select new UserContentDTO( A.id,A.title, A.linkactions) from ContentManage as A order by A.id")
List<UserContentDTO> getHtmlContentFiles();
我不知道如何使用 REST api
生成包含帮助数据库内容的 HTML 文件目前我们直接将 HTML 源代码存储在 user table[= 下的 LinkAction 列中14=]
Title | LinkAction |
---|---|
FileName | <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));
}
我使用了 IOUtils.copy() 而不是写入文件。
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");
ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(zipPath.toFile()));
zipOutputStream.putNextEntry(new ZipEntry(cardsExcelPath.getFileName().toString().substring(0,5).concat(".xlsx")));
ByteArrayInputStream byteArrayInputStream = contentManageService.listAll();
IOUtils.copy(byteArrayInputStream, zipOutputStream);
cardsXlsWriter.flush();
cardsXlsWriter.close();
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").replaceAll("\s", "")));
Path cardsHtmlPath = Files.createTempFile(filenameHtml.get(i), ".html");
log.info("Cards html: " + cardsHtmlPath.toAbsolutePath());
FileOutputStream cardsHtmlWriter = new FileOutputStream(cardsHtmlPath.toFile());
List<String> htmlContentList = contentManageService.getHtmlContentFilesExport();
String initialString = htmlContentList.get(i);
InputStream targetStream = new ByteArrayInputStream(initialString.getBytes());
IOUtils.copy(targetStream, zipOutputStream);
cardsHtmlWriter.flush();
cardsHtmlWriter.close();
zipOutputStream.closeEntry();
}
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));
}
DateFormat dateFormatter = new SimpleDateFormat("yyyy-MM-dd_HHmmss");
String currentDateTime = dateFormatter.format(new Date());
String filename = "Fingertips_";
//Creating Excel file and adding Fingertips content to it
Path cardsExcelPath = Files.createTempFile(filename, ".xlsx");
FileOutputStream cardsXlsWriter = new FileOutputStream(cardsExcelPath.toFile());
//Creating zip file and adding above Fingertips Excel file
Path zipPath = Files.createTempFile(filename, ".zip");
ZipOutputStream zipOutputStream = new ZipOutputStream(new FileOutputStream(zipPath.toFile()));
zipOutputStream.putNextEntry(new ZipEntry(cardsExcelPath.getFileName().toString().substring(0,10).concat(".xlsx")));
ByteArrayInputStream byteArrayInputStream = fingerTipsService.exportAllContentManageToExcel();
IOUtils.copy(byteArrayInputStream, zipOutputStream);
cardsXlsWriter.flush();
cardsXlsWriter.close();
zipOutputStream.closeEntry();
List<UserContentDTO> htmlContentList = fingerTipsService.getHtmlContentFilesExport();
htmlContentList.stream()
.forEach(userContentDTO -> {
try {
zipOutputStream.putNextEntry(new ZipEntry(userContentDTO.getTitle().replaceAll("\s", "").concat(".html")));
Path cardsHtmlPath = Files.createTempFile(userContentDTO.getTitle(), ".html");
FileOutputStream cardsHtmlWriter = new FileOutputStream(cardsHtmlPath.toFile());
InputStream targetStream = new ByteArrayInputStream(userContentDTO.getLinkactions().getBytes());
IOUtils.copy(targetStream, zipOutputStream);
cardsHtmlWriter.flush();
cardsHtmlWriter.close();
zipOutputStream.closeEntry();
}
catch(Exception e) {
System.out.println("came to exception: "+e);
}
});
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));
}
服务class
public ByteArrayInputStream exportAllContentManageToExcel() {
List<ContentManage> data = userContentManageRepository.findAll(Sort.by("id").ascending());
ByteArrayInputStream in = ContentManageExcelExporter.exportAllContentManageToExcelFile(data);
return in;
}
//To ge the HTML source code from Database
public List<UserContentDTO> getHtmlContentFilesExport()
{
List<UserContentDTO> content = userContentManageRepository.getHtmlContentFiles();
return content;
}
回购 class
@Transactional(rollbackFor = Exception.class)
@Query("select new UserContentDTO( A.id,A.title, A.linkactions) from ContentManage as A order by A.id")
List<UserContentDTO> getHtmlContentFiles();