public void showFile(){
try{
HttpServletResponse response = (HttpServletResponse)FacesContext.getCurrentInstance().getExternalContext().getResponse();
File file = new File(filePath+fileName);
BufferedInputStream input =new BufferedInputStream(new FileInputStream(file), 1024);
response.reset();
response.setContentType("application/pdf");
response.setHeader("Content-disposition", "inline; filename=\"" + fileName + "\"");
BufferedOutputStream output = new BufferedOutputStream(response.getOutputStream(), 1024);
byte[] buffer = new byte[1024] //1KB buffer size
int length;
while ((length = input.read(buffer)) > 0) {
output.write(buffer, 0, length);
}
output.flush();
} catch (Exception e) {
e.printStackTrace();
} finally {
try{
output.close();
input.close();
}catch(Exception e){e.printStackTrace();}
}
facesContext.responseComplete();
}
JSP sayfasında yapılması gereken de şöyle bir kod ile çağırmaktır;
<h:commandLink value="Download PDF" action="#{testBean.showFile}" target="_blank" />
Hiç yorum yok:
Yorum Gönder