If you want to convert String to a file and wants to get a downloadable dialog in browser to download file then below code will help you. This example code will work for servlets, struts, Spring MVC etc. You just need to take care of response headers. This code is helpful for any type of file download if you change response.setContentType(“application/xml”); to any other file format and change file name too.
@RequestMapping(value = “/urlhere”, method = RequestMethod.POST)
public ModelAndView urlhere(HttpServletRequest request, HttpServletResponse response) throws Exception {
ModelAndView mav = new ModelAndView();
String str= “string to convert into xml file and then download that file through browser dialog.”;
//set response headers
response.setHeader(“Pragma”, “public”);
response.setHeader(“Expires”, “0″);
response.setHeader(“Cache-Control”, “must-revalidate, post-check=0, pre-check=0″);
response.setHeader(“Content-type”, “application-download”);
response.setHeader(“Content-Disposition”, “attachment; filename=” + fileName);
response.setHeader(“Content-Transfer-Encoding”, “binary”);
response.setContentType(“application/xml”);
Writer writer = response.getWriter();
writer.append(str);
writer.close();
mav.setViewName(“viewname”);
return mav;
}