我想在我的JSF頁面上顯示PDF文件,我檢查了這個how to display a pdf document in jsf page in iFrame,但我不想在iframe上顯示它(因爲它會生成滾動條)。我只想將pdf顯示在一個像圖像這樣的頁面上,並且可以給它一個寬度和高度。如何在JSF頁面上顯示PDF
EDIT Hi BalusC。我仍然無法在聯機中顯示PDF。這是我的代碼。
@WebServlet(name = "pdfHandler", urlPatterns = {"/pdfHandler/*"})
public class pdfHandler extends HttpServlet {
private static final int DEFAULT_BUFFER_SIZE = 10240;
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String requestedFile = request.getPathInfo();
File file = new File("/Users/KingdomHeart/Downloads/Test/pdf/" + requestedFile);
response.reset();
response.setContentType("application/pdf");
response.setBufferSize(DEFAULT_BUFFER_SIZE);
response.setHeader("Content-Length", String.valueOf(file.length()));
response.setHeader("Content-Disposition", "inline; filename=\"" + file.getName() + "\"");
BufferedInputStream input = null;
BufferedOutputStream output = null;
try{
input = new BufferedInputStream(new FileInputStream(file), DEFAULT_BUFFER_SIZE);
output = new BufferedOutputStream(response.getOutputStream(), DEFAULT_BUFFER_SIZE);
byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
int length;
while((length = input.read(buffer)) > 0){
output.write(buffer, 0, length);
}
}finally{
output.close();
input.close();
}
}
...
}
它仍然提示我下載pdf文件。下載到我的電腦的PDF文件是正確的PDF文件btw。你能發現任何錯誤嗎?
首先,檢查您的瀏覽器中是否安裝了Adobe Reader插件**。您可以在瀏覽器中查看PDF文檔**,還是要求下載? – 2011-04-21 17:28:11