JSP View :
<a href="download">Download String </a>
Controller Method :
@RequestMapping(value = "/download", method = RequestMethod.GET)
public @ResponseBody
void downloadFile(HttpServletResponse resp) {
String downloadFileName= "download.txt";
String downloadStringContent= getStringToWrite(); // implement this
try {
OutputStream out = resp.getOutputStream();
resp.setContentType("text/plain; charset=utf-8");
resp.addHeader("Content-Disposition","attachment; filename=\"" + downloadFileName + "\"");
out.write(downloadStringContent.getBytes(Charset.forName("UTF-8")));
out.flush();
out.close();
} catch (IOException e) {
}
}
Check this as well : spring mvc download a file from server