Issue
I have no idea how to generate the HTML file with the help database content using REST api
Currently we are directly storing the HTML source code in LinkAction column under user table
Title | LinkAction |
---|---|
FileName | <html><head><title>Title</title></head><body><h1>Heading</h1><p>paragraph</p></body></html> |
My requirement is to generate the html file from the html source code and html file name should be the title value.
Please help me out this
Solution
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));
}
service 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;
}
repo 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();
Answered By - K Kumar