2012-11-05 32 views
0

我有java web應用程序。我正在使用ANT來構建應用程序。我在資源文件夾中有一個文件。我想用pdfViewer打開該文件。 PDF查看器的框架正在啓動。但我無法訪問資源文件夾中的文件。這是我的代碼。Java使用pdfviewer來顯示資源文件夾中存在的文件

final String fileName="Installation.pdf"; 

int pagenum = 0; 
RandomAccessFile raf = new RandomAccessFile (new File(fileName), "r"); 
FileChannel fc = raf.getChannel(); 
ByteBuffer buf = fc.map (FileChannel.MapMode.READ_ONLY, 0, fc.size()); 
PDFFile pdfFile = new PDFFile (buf); 

enter image description here 我得到一個FileNotFoundException。主要問題是我無法在資源文件夾中找到該文件。什麼請幫忙。

上述工作正常,當我作爲一個獨立的Java應用程序運行它。

+0

你能提供你的目錄結構? – CAMOBAP

+0

通過「pdfViewer」DYM是指一個特定的API,或者你只是指PDF文檔的通用查看器? –

+0

您可以簡單地*打開*文件而不是啓動pdf查看器。人們通常擁有正確的pdf文件系統關聯。這裏是相關的答案:[使用Ant,我如何在瀏覽器中打開文件?](http://stackoverflow.com/a/6597772/772981) – Jarekczek

回答

0

試試這個:

ClassLoader classLoader = Thread.currentThread().getContextClassLoader(); 
URL url = classLoader.getResource("/resources/Installation.pdf"); 

if (url != null) { 

    try { 
     RandomAccessFile raf = new RandomAccessFile (new File(url), "r"); 

     ... 

    } finally { 
     input.close(); 
    } 
} 
+0

你能幫我解決這個問題嗎? – Goofy