Reputation: 967
here the below code trying to write pdf file from local machine to browser, but here doesn't write file to browser
String pdfFileName = "hello1.pdf";
String contextPath = "";
contextPath = "/home/admin/Desktop/";
File pdfFile = new File(contextPath + pdfFileName);
FileInputStream fileInputStream = new FileInputStream(pdfFile);
response.setContentType("application/pdf");
response.addHeader("Content-Disposition", "attachment; filename=" + pdfFileName);
response.setContentLength((int) pdfFile.length());
OutputStream responseOutputStream = response.getOutputStream();
System.out.println("fileInputstream length : " + fileInputStream.available());
int length;
byte[] buffer = new byte[4096];
while ((length = fileInputStream.read(buffer)) > 0) {
responseOutputStream.write(buffer, 0, length);
}
System.out.println(" outputstream length : " + responseOutputStream.toString());
fileInputStream.close();
responseOutputStream.flush();
responseOutputStream.close();
Upvotes: 3
Views: 2815
Reputation: 316
in your code statement:
response.addHeader("Content-Disposition", "attachment; filename=" + pdfFileName);
change"attachment" to "inline", like below :
response.addHeader("Content-Disposition", "inline; filename=" + pdfFileName);
Then pdf file will open automatically
Upvotes: 1