这是我下载文件的解决方案:
Spring Controller方法:
@RequestMapping(value = "/download", method = RequestMethod.GET)public void retrievedocument(@RequestParam("id") String id, HttpServletResponse response) throws IOException { InputStream in = fileService.getFileStream(); // My service to get the stream. response.setContentType(MediaType.APPLICATION_OCTET_STREAM); response.setHeader("Content-Transfer-Encoding", "binary"); response.setHeader("Content-Disposition", "attachment; filename=" + filename); try { IOUtils.copy(inputStream, response.getOuputStream()); //Apache commons IO. inputStream.close(); response.flushBuffer(); response.setStatus(HttpServletResponse.SC_OK); } catch (Exception e) { //log error. }}
在客户端功能上:
function download(id) { var id = $('#file').attr('id') var xhr = new XMLHttpRequest(); xhr.open('GET', 'url here' + id, true); xhr.responseType = 'arraybuffer'; xhr.onload = function() { if(this.status == '200') {var filename = '';//get the filename from the header.var disposition = xhr.getResponseHeader('Content-Disposition');if (disposition && disposition.indexOf('attachment') !== -1) { var filenameRegex = /filename[^;=n]*=((['"]).*?2|[^;n]*)/; var matches = filenameRegex.exec(disposition); if (matches !== null && matches[1]) filename = matches[1].replace(/['"]/g, '');}var type = xhr.getResponseHeader('Content-Type');var blob = new Blob([this.response], {type: type});//workaround for IEif(typeof window.navigator.msSaveBlob != 'undefined') { window.navigator.msSaveBlob(blob, filename);}else { var URL = window.URL || window.webkitURL; var download_URL = URL.createObjectURL(blob); if(filename) { var a_link = document.createElement('a'); if(typeof a_link.download == 'undefined') { window.location = download_URL; }else { a_link.href = download_URL; a_link.download = filename; document.body.appendChild(a_link); a_link.click(); } }else { window.location = download_URL; } setTimeout(function() { URL.revokeObjectURL(download_URL); }, 10000);} }else { alert('error')';//do something... } }; xhr.setRequestHeader('Content-type', 'application/*'); xhr.send();}


