using the @Noofiz code, I wrote this code to test this feature and worked for me!
we can read from the file and send it to the user in time and let the user download the file!
1) I printed "The End!" so that the file is sent to the user on time when he buffers and does not finish buffering first, and then sends it to the user!
2) the downloaded file and the main md5 file were checked, and they were similar.
here is my servlet code:
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { InputStream is = new FileInputStream("/home/Mehdi/Desktop/log.log"); response.setContentType("application/octet-stream"); ServletOutputStream out = response.getOutputStream(); int i; byte[] buffer = new byte[10 * 1024]; while ((i = is.read(buffer)) != -1) { if (i != 10240) { for (int j = 0; j < i; j++) { out.write(buffer[j]); } } else { out.write(buffer); } } System.out.println("End!"); }
Mehdi source share