我在用java编写,但想为用户创建一个动态的HTML页面。我正在使用Lowagie用HTML创建文档。我设法提供html,但我的图片是空的。它只包含图片边框。 谁能帮我这个?或者告诉我另一种创建HTML页面的方法(最好使用ByteArrauOutputstream或其他outpustream来显示内容)。
代码如下:
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;
}