String helloworld = "hello world";
response.setContentType("application/x-download");
// must be set the Header before any data has wrote into it.
response.addHeader("Content-Disposition","attachment;filename=helloworld.txt");
byte[] ins = helloworld.getBytes();
response.getOutputStream().write(ins);
The things that u should only concerned is that, if u want to set Header or ContentType that basic impacts on the format of the Response, u should set them before u added any data in the response.getOutputStream().