我正在为课程作业做一个 java web 应用程序。在此应用程序中有两种类型的用户:卖方和买方。买家可以买一套东西,当他买的时候,我必须创建一个pdf格式的收据;但是当我尝试购买tomcat的东西时给我这个错误:
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);
}
我确定代码是正确的,而且该文件夹存在,为什么我不能保存我的文件?