简体   繁体   中英

Generate HTML file from database content in spring boot application

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

Finally I am able to generate and download the HTML files. below is my complete source code, it might be help some one.

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));
    }

I have used the IOUtils.copy() instead of writing the file.

    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));
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM