I'm using below code inside a servlet for reading and writing PDF in application, but the read() method is getting blocked for some PDFs after reading some bytes:
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
InputStream is = null;
OutputStream oos = null;
try {
String pdfPath = (String) request.getSession().getAttribute("viewPdfPath");
File file=new File(pdfPath);
oos = response.getOutputStream();
response.setContentType("application/pdf");
byte[] buf = new byte[8192];
is= new FileInputStream(file);
int c = 0;
while ((c = is.read(buf, 0, buf.length)) > 0) { **//blocking after reading some bytes**
oos.write(buf, 0, c);
oos.flush();
}
oos.flush();
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch(Exception e){
e.printStackTrace();
} finally {
if(oos != null)
oos.close();
if(is != null)
is.close();
}
}
The above code when executed from terminal as part of the standalone java class was successfully reading all bytes of the same PDF on the same Linux server where the application is currently hosted.
Why the InputStream read() method is getting blocked as part of application, but same code when executed from the same Linux server as part of the standalone java class was successfully reading without blocking?