2017-01-13 6 views
1

私はintellij Ideaを使用しています。私はpdfファイルをresourcesフォルダに保存しました。私はブラウザにそのpdfファイルを表示したい。サーブレットを使用してブラウザにPDFファイルを表示

public class GetDocumentation extends HttpServlet { 
    private static final Logger log = Logger.getLogger(GetDocumentation.class); 
@Override 
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 
    InputStream pdf_path = this.getClass().getResourceAsStream(ApplicationProperties.getProperty("PDF_PATH")); 

    resp.setContentType("application/pdf"); 
    resp.addHeader("Content-Disposition", "attachment; filename=Documentation.pdf"); 
    OutputStream responseOutputStream = resp.getOutputStream(); 

    byte[] buf = new byte[4096]; 
    int len = -1; 

    while ((len = pdf_path.read(buf)) != -1) { 
     responseOutputStream.write(buf, 0, len); 
    } 
    responseOutputStream.flush(); 
    responseOutputStream.close(); 
    } 
} 


<a href="/documentation">Documentation</a> 

私はJspサーブレットを使用しており、 "/ documentation"を呼び出しています。私のファイルはレンダリングされていますが、空白です。私は間違って何かしていますか?

+0

あなたはコンテンツDespositionヘッダーを設定すると、ファイルはブラウザ内に表示すべきではありません。ブラウザには、開いている/保存しているダイアログが表示されます。サーバー側のコードがファイルを見つけてそれを応答して送信していますか?サーバー側で例外はありませんか? – zaerymoghaddam

答えて

0

inlineドキュメントを表示するには、Content-Dispositionを使用する必要があります。 "inline""attachment"を置き換えます

resp.addHeader("Content-Disposition", "inline; filename=Documentation.pdf"); 
関連する問題