1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| protected void _doPost(HttpServletRequest request, HttpServletResponse response) throws Exception, IOException {
// Your data can be stored in a database, generated at runtime, or whatever you choose.
// We getSomeData() from an arbitrary location. This depends on your application.
byte[] data = request.getParameter("xml").getBytes();
// In the response, we will specify a file name to present to the user.
String filename = request.getParameter("test.xml");
// Now we have the data to send, and the response object to send it with.
// Next we give the browser some hints on what to do with the file.
// Note that different browsers will behave differently and you have no control over this.
response.setContentType("application/x-unknown");
// We tell the browser how much data to expect.
response.setContentLength(data.length);
// Cross-browser hack for Firefox 1.0.7 and IE 6 compatibility.
// IE 6 ignores the MIME type and decides based on the "attachment" or "inline"
response.setHeader( "Content-disposition", "attachment; filename=\"" + filename + "\"");
// Now we start sending data with the response object.
// You might consider using a buffer if your data comes from a large file
// or a database.
try {
ServletOutputStream out;
out = response.getOutputStream();
out.write(data);
out.close();
} catch (IOException e) {
e.printStackTrace();
}
} |
Partager