我正在爲一門課程做一個java web應用程序。在這個應用程序中有兩種類型的用戶:賣家和買家。買家可以購買一套東西,當他這樣做時,我必須創建一個pdf格式的收據;但是當我嘗試買東西的tomcat給我這個錯誤:如何用java itext保存PDF
HTTP Status 500 - \WebApplication\pdf\test.pdf (Impossibile trovare il percorso specificato)
type Exception report
message \WebApplication\pdf\test.pdf (Impossibile trovare il percorso specificato)
description The server encountered an internal error that prevented it from fulfilling this request.
exception
java.io.FileNotFoundException: \WebApplication\pdf\test.pdf (Impossibile trovare il percorso specificato)
java.io.FileOutputStream.open(Native Method)
java.io.FileOutputStream.<init>(FileOutputStream.java:212)
java.io.FileOutputStream.<init>(FileOutputStream.java:104)
viewer.PdfCreator.createPdf(PdfCreator.java:30)
servlet.BuyerConfirmationPage.doGet(BuyerConfirmationPage.java:115)
servlet.BuyerConfirmationPage.doPost(BuyerConfirmationPage.java:61)
javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
,這裏是我寫的代碼:
try {
Document document = new Document(PageSize.A4,50,50,50,50);
PdfWriter.getInstance(document,new FileOutputStream(request.getContextPath() + "/pdf/test.pdf"));
document.open();
PdfPTable table = new PdfPTable(5);
PdfPCell seller_cell = new PdfPCell(new Paragraph("Seller"));
PdfPCell name_cell = new PdfPCell(new Paragraph("Name"));
PdfPCell price_cell = new PdfPCell(new Paragraph("Price"));
PdfPCell UM_cell = new PdfPCell(new Paragraph("UM"));
PdfPCell quantity_cell = new PdfPCell(new Paragraph("Quantity"));
table.addCell(seller_cell);
table.addCell(name_cell);
table.addCell(price_cell);
table.addCell(UM_cell);
table.addCell(quantity_cell);
PdfPCell seller_cell_value = new PdfPCell(new Paragraph(seller));
PdfPCell name_cell_value = new PdfPCell(new Paragraph(name));
PdfPCell price_cell_value = new PdfPCell(new Paragraph(total_price));
PdfPCell UM_cell_value = new PdfPCell(new Paragraph(UM));
PdfPCell quantity_cell_value = new PdfPCell(new Paragraph(quantity));
table.addCell(seller_cell_value);
table.addCell(name_cell_value);
table.addCell(price_cell_value);
table.addCell(UM_cell_value);
table.addCell(quantity_cell_value);
document.add(table);
document.close();
} catch (DocumentException ex) {
Logger.getLogger(PdfCreator.class.getName()).log(Level.SEVERE, null, ex);
}
我敢肯定,代碼是正確的,也即該文件夾存在,爲什麼我不能保存我的文件?
如果沒有文件存在,FileOutputStream創建它。關鍵是在獨立的Java應用程序中,一切都很順利。 – DamianFox