2012-11-14 49 views
1

我想要使用JFileChooser來分割PDF文件,但PdfReader無法讀取文件。 IOException讀取「Publishing Letter.pdf not found as file or resource」。使用文件上傳訪問pdf文件

private void butSelectActionPerformed(java.awt.event.ActionEvent evt) { 
    int returnValue = fileChooserPdf.showOpenDialog(this); 
    if(returnValue == JFileChooser.APPROVE_OPTION) 
    { 
     int n; 
     String theFile = fileChooserPdf.getSelectedFile().getName(); 
     String theFileInLower = theFile.toLowerCase(); 

     JOptionPane.showMessageDialog(null, "Reading the file " + theFileInLower, "Ok", JOptionPane.INFORMATION_MESSAGE); 
     try 
     { 
      PdfReader reader = new PdfReader(theFileInLower); 
      n = reader.getNumberOfPages(); 
      System.out.println("there are " + Integer.toString(n) + " number of pages"); 
     } 
     catch(IOException io) 
     { 
      JOptionPane.showMessageDialog(null, io.toString(), "Ok", JOptionPane.ERROR_MESSAGE); 

     } 

    } 
    else 
    { 
     JOptionPane.showMessageDialog(null, "An error occured", "Ok", JOptionPane.ERROR_MESSAGE); 
    } 


} 

是否有可能通過使用JFileChooser中分裂它的目的來訪問PDF文件?我會怎麼做呢?

回答

2

這是因爲您正在使用具有文件名的構造函數。這樣該文件將在本地目錄中進行搜索。您應該使用InputStream構造函數與FileInputStream。這樣你就可以直接傳遞選中的File對象。