Sunday, 13 November 2011

how do you download a file from your web application? - From book java/j2ee interview

Files can be downloaded from a web application by using the right combination of
//set the header to a non-standard value for attachments to be saved by the browser with the
//Save-As dialog so that it is unrecognized by the browsers because often browsers try to do
//something special when they recognize the content-type.
//use Content-Disposition “attachment” to invoke “Save As” dialog and “inline” for displaying
//the file content on the browser without invoking the “Save As” dialog.
response.setHeader(“Content-disposition”, “attachment;filename=” + fileName);

@RequestMapping(value = "get_zip_archive")
public ResponseEntity<byte[]> getZippedLogFilesArchive(@RequestParam("tempFileName") String tempFileName) throws IOException {
    final File zipFile = new File(TEMP_DIR, tempFileName);
    try {
        final HttpHeaders headers = new HttpHeaders();
        headers.set("Content-Disposition", "attachment; filename=" + tempFileName);
        return new ResponseEntity<byte[]>(Files.toByteArray(zipFile), headers, HttpStatus.OK);
    } finally {

--jsp page--
function downloadReport(id) {
 window.location = "downloadReportData?id=" + id;

No comments:

Post a Comment