This util method is used for increasing performance by reducing the time and output xml size. The concept is just compress the xml content and send it as a gzip file. So the file size will reduce much better and responce will be faster
public static HttpServletResponse GZipFileGenerator( HttpServletResponse response, String xmlString, String fileName) throws IOException { try { //create a temp file with gzip file extension (.gz) File temp = File.createTempFile(fileName, ".gz"); //Delete the temp file on exit of the program temp.deleteOnExit(); //Creating Buffered output stream contains gzip (temp) file BufferedOutputStream out = new BufferedOutputStream( new GZIPOutputStream((new FileOutputStream(temp)))); //writing the xml to the Buffered output stream out.write(xmlString.getBytes("UTF-8")); out.close(); //Now the gzip file is generated with xml data. //Apend the temp gzip xml file to the response. response.setContentType("application/gzip"); response.setHeader("Content-Disposition", "attachment; filename= \" " + fileName + ".gz \""); FileInputStream fileInput = new FileInputStream(temp); //Reading the file size and set the content to response int size = fileInput.available(); response.setContentLength(size); byte[] filesize = new byte[size]; OutputStream outputStream = response.getOutputStream(); int bytesread; do { bytesread = fileInput.read(filesize, 0, size); if (bytesread > -1) outputStream.write(filesize, 0, bytesread); } while (bytesread > -1); fileInput.close(); outputStream.flush(); outputStream.close(); } catch (Exception e) { } return response; }