2016-08-02 2 views
0

私はpdfファイルを開いて、クライアントのデスクトップに表示しようとしています。 Tomcatサーバーでjavaを使用しています。 ファイルはプロジェクトのresourcesフォルダーにあり、ビルド後はWEB-INF/resources/in.pdfJava:inputStream = null PDFファイルからnull

nullが返されます。どのようにしてファイルの実際のパスを見つけることができますか?ここ

コードです:

try{ 
     if (Desktop.isDesktopSupported()) { 
      File file = new File("out.pdf"); 
      if (!file.exists()) { 
       InputStream inputStream = ClassLoader.getSystemClassLoader() 
         .getResourceAsStream("/WEB-INF/resources/in.pdf"); 
       OutputStream outputStream = new FileOutputStream(file); 
       byte[] buffer = new byte[1024]; 
       int length; 
       while ((length = inputStream.read(buffer)) > 0) { 
        outputStream.write(buffer, 0, length); 
       } 
       outputStream.close(); 
       inputStream.close(); 
      } 
      Desktop.getDesktop().open(file); 
     } 
    }catch(Exception e1) 
    { 
     e1.printStackTrace(); 
    } 
+1

http://stackoverflow.com/questions/1108434/howto-load-a-resource-from-web-inf-directory-of-a-web-archive ... try 'getResourceAsStream(" WEB-INF/resources/in.pdf ")' – Tom

+0

ありがとう、それは動作します:) –

答えて

0

今getResourceAsStreamは、おそらく瓶や戦争に詰めリソース、クラスパス上のファイルを指します。

また、WEB-INFはWebサーバーディレクトリであり、パスはWEB-INF/classesではありません。だから、サーブレットであること、およびWebディレクトリ/ WEB-INF

 File file = new File("out.pdf"); 
     if (!file.exists()) { 
      File source = request.getServletContext() 
        .getRealPath("/WEB-INF/resources/in.pdf"); 
      Files.copy(source.toPath(), file.toPath()); 
     } 

その後、再び現在のデスクトップが(同じコンピュータ上のWebサーバとデスクトップ)PDFを表示する必要がありますへのファイルシステムパスを取得する必要があります。

サーバがリモートマシンの場合、一部のメタ情報ファイルを除いて一般にWEB-INFにはアクセスできません。

関連する問題