0

我正在用 java 编写,但想为用户创建一个动态 HTML 页面。我正在使用 Lowagie 创建带有 HTML 的文档。我确实设法展示了 html,但我的图片是空的。它只包含图片边框。谁能帮我这个?或者告诉我另一种创建 HTML 页面的方法(最好使用 ByteArrauOutputstream 或其他输出流来显示内容)。

代码如下:

 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    String orderId = request.getParameter("id1");
    String telephone = request.getParameter("id2");
    response.setHeader("Expires", EXPIRES);

    response.setContentType(CONTENT_TYPE);

    ServletOutputStream out = null;
    ByteArrayOutputStream baos = null;
    try {
        baos = getHtmlTicket(orderId, telephone);

        response.setContentLength(baos.size());
        out = response.getOutputStream();
        baos.writeTo(out);

    }
    catch (Exception e) {
        log.error(e.getMessage(), e);

    }
    finally {
        if (out != null) {
            try {
                out.flush();
            }
            catch (Exception e) {
                log.debug(e.getMessage(), e);
            }
        }
        if (baos != null) {
            try {
                baos.flush();
            }
            catch (Exception e) {
                log.debug(e.getMessage(), e);
            }
        }
        if (out != null) {
            try {
                out.close();
            }
            catch (Exception e) {
                log.warn(e.getMessage(), e);
            }
        }
        if (baos != null) {
            try {
                baos.close();
            }
            catch (Exception e) {
                log.warn(e.getMessage(), e);
            }
        }
    }



  public ByteArrayOutputStream getHtmlTicket(String orderId, String   telephoneTest) {
  ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
    Document document = new Document();
    Order order = orderService.getOrder(Integer.parseInt(orderId));

    String fileType = "png";
    String filePath = "html/picture.png";


    File myFile = new File(filePath);
    try {
        HtmlWriter.getInstance(document, baos);
        document.open();
        document.add(new Paragraph("Hello World"));

        Image fileImage = Image.getInstance(filePath);

        document.add(fileImage);
        document.add(new Paragraph("osv"));
    }
    catch (DocumentException e) {
        e.printStackTrace();
    }
    catch (IOException e) {
        e.printStackTrace();
    }


    document.close();

    return baos;
 }
4

0 回答 0