2012-11-13 113 views
1

我正在爲一門課程做一個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); 
     } 

我敢肯定,代碼是正確的,也即該文件夾存在,爲什麼我不能保存我的文件?

回答

1

這不是一個iText問題。如果刪除所有iText代碼並嘗試將純文本寫入FileOutputStream,則會出現相同的異常。

Perneel提交的問題「是否有必要從網上加載PDF」是非常相關的。爲什麼不在內存中創建PDF而不是將其寫入文件?請參閱「iText in Action」中第9章的示例以獲取靈感:http://itextpdf.com/book/chapter.php?id=9(閱讀本章也有幫助)。

在任何情況下:madth3都是正確的:假設(1)存在\WebApplication\pdf\的路徑並且(2)Web應用程序有權訪問此路徑。只需使用路徑"."創建一個File對象,並使用getAbsolutePath()將該路徑寫入您的servlet的輸出。我敢打賭它不會給你預期的結果。

0

是否有必要從網上加載pdf?你就不能generete PDF文件是這樣的:

Document document = new Document(PageSize.A4,50,50,50,50); 
File file = File.createTempFile("dossier_" + selectedDossier.getDossierID() + "_", ".pdf"); 
PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream(file.getPath())); 
+0

如果沒有文件存在,FileOutputStream創建它。關鍵是在獨立的Java應用程序中,一切都很順利。 – DamianFox

0

你得到它的上下文路徑是在URL中的,因此它可能沒有指向您認爲地方使用相對路徑。我想你應該使用類似getRealPath()

+0

現在它使用getRealPath()。謝謝! – DamianFox