Reputation: 103
I have this example which displays the result in a console
HttpServletResponse presponse
presponse.setContentType(text/xml; charset=UTF-8)
PrintWriter lout = presponse.getWriter();
lout.println(var);
lout.close();
But i want to save the result in downloadable file instead of display it in the console can you help me please
Upvotes: 0
Views: 228
Reputation: 11487
You can do something like, you need to set correct mime-type
and content-length
header.
File downloadFile = new File(filePath);
FileInputStream inStream = new FileInputStream(downloadFile);
// obtains response's output stream
OutputStream outStream = response.getOutputStream();
byte[] buffer = new byte[4096];
int bytesRead = -1;
while ((bytesRead = inStream.read(buffer)) != -1) {
outStream.write(buffer, 0, bytesRead);
}
inStream.close();
outStream.close();
Upvotes: 0
Reputation: 38205
If you do not want to provide the file content as the HTTP response payload, you should not write it into response.getWriter()
. By the way, you're not "displaying it in the console", you're sending it as the HTTP response to the client's request.
To save a file with that content on the local disk, just create a FileOutputStream
and write the file content in there.
As for the HTTP response, just provide the URL to the newly created downloadable file (as a Location
header or whatever).
Upvotes: 1