public void downLoadFile(HttpServletResponse response, List<XbptSfProjectMaterialApp> list,XbptSfProjectApp xbptSfProjectApp) { List<File> files = new ArrayList<>(); for (XbptSfProjectMaterialApp xbptSfProjectMaterialApp : list) { String [] fileIds = xbptSfProjectMaterialApp.getMaterialId().split(","); for (String id : fileIds){ UpmsFile upmsFile = upmsFileService.queryById(id); File file = new File(filePath + upmsFile.getSrc()); files.add(file); } } downFile(files, response,xbptSfProjectApp); } public HttpServletResponse downFile(List<File> list, HttpServletResponse response,XbptSfProjectApp xbptSfProjectApp) { try { String zipFiles = ""; SimpleDateFormat sdf = new SimpleDateFormat("yyyyHHmm"); String date = sdf.format(new Date()); if (StringUtils.isBlank(xbptSfProjectApp.getProjectDeclareNum())){ zipFiles = xbptSfProjectApp.getEntName()+date+".zip"; }else { zipFiles = xbptSfProjectApp.getProjectDeclareNum()+date+".zip"; } File file = new File(zipFiles); if (!file.exists()) { file.createNewFile(); //创建一个zip文件 } response.reset();//清空response FileOutputStream fileOutputStream = new FileOutputStream(file); ZipOutputStream zipOutputStream = new ZipOutputStream(fileOutputStream); zipFiles(list, zipOutputStream); zipOutputStream.close(); fileOutputStream.close(); return downloadZip(file, response); } catch (Exception e) { e.printStackTrace(); } return response; } public void zipFiles(List<File> files, ZipOutputStream outputStream) { int size = files.size(); for (int i = 0; i < size; i++) { File file = files.get(i); zipFile(file, outputStream); } } /** * @param inputFile //文档路径 * @param ouputStream */ public void zipFile(File inputFile, ZipOutputStream ouputStream) { try { //File路径 FileInputStream IN = new FileInputStream(inputFile); BufferedInputStream bins = new BufferedInputStream(IN, 1024); ZipEntry entry = new ZipEntry(inputFile.getName()); ouputStream.putNextEntry(entry); // 向压缩文件中输出数据 int nNumber; byte[] buffer = new byte[1024]; while ((nNumber = bins.read(buffer)) != -1) { ouputStream.write(buffer, 0, nNumber); } // 关闭创建的流对象 bins.close(); IN.close(); } catch (Exception e) { e.printStackTrace(); } } public HttpServletResponse downloadZip(File file, HttpServletResponse response) { try { // 以流的形式下载文件。 InputStream fis = new BufferedInputStream(new FileInputStream(file.getPath())); byte[] buffer = new byte[fis.available()]; fis.read(buffer); fis.close(); // 清空response response.reset(); OutputStream toClient = new BufferedOutputStream(response.getOutputStream()); response.setContentType("application/octet-stream"); // 如果输出的是中文名的文件,在此处就要用URLEncoder.encode方法进行处理 // response.setHeader("Content-Disposition", "attachment;filename=" + new String(file.getName().getBytes("GB2312"), "ISO8859-1")); response.setHeader("content-disposition", "attachment;filename*=" + URLEncoder.encode(file.getName(), "UTF-8")); toClient.write(buffer); toClient.flush(); toClient.close(); } catch (Exception ex) { ex.printStackTrace(); } finally { try { File f = new File(file.getPath()); f.delete(); } catch (Exception e) { e.printStackTrace(); } } return response; }
Vue前端接收流写法
let config = {
headers: {
'Content-Type': 'application/json'
},
responseType:'blob',//必须要写
};
this.$http.post("/upms/api/v1/itim_doc/downloadFile", this.tableSelection, config).then(res => {//tableSelection参数,我自己的是对象,后台用List接,可以看一downloadFile接口
let data = res.data;
let contentDisposition = res.headers['content-disposition'];
let fileName = contentDisposition.substring(contentDisposition.indexOf('=') + 1);
let url = window.URL.createObjectURL(new Blob([data]));
let edik = document.createElement('a');
edik.style.display = 'none';
edik.href = url;
edik.setAttribute('download', fileName);
document.body.appendChild(edik);
//点击下载
edik.click();
// 释放掉blob对象
window.URL.revokeObjectURL(edik);
// 下载完成移除元素
document.body.removeChild(edik);
});