Streaming Data in Java EE

we have this design:

enter image description here

our installed service on tomcat has the getDatabaseData method (String request) (RMI client), which receives data from the database using (the RMI Server implementation) existing on the kernel.

we want to do something so that our installed service inside tomcat will make an xml file and send it to the client now:

we thought of a solution that:

  • first make the whole xml file on our tomcat using getDatabaseData (String request), and then our user can get the link using the link and start downloading the data.

but this solution is not informative for us, because the size of this file is so large, and if we want to do it, then our tomcat server storage will fill up and go down so quickly, so we are looking for a solution for real-time streaming data to the user instead of creating everything file. I mean a way to send data to a user in an xml template when data is received from CORE.

Do you have any ideas?

(by the way, the tomcat web service was written using Spring MVC and Spring Security)

+6
source share
2 answers

I can give you the code from my current project, where we return the file to the struts action:

response.setContentType("application/octet-stream"); response.setHeader("Content-Disposition", "attachment;filename=\"" + fileName + "\""); ServletOutputStream out = response.getOutputStream(); out.write(file); 

Is this what you are looking for?

+1
source

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!"); } 
0
source

All Articles