java實現(xiàn)導(dǎo)出數(shù)據(jù)為zip壓縮文件
1,前端只要將要導(dǎo)出的數(shù)據(jù)的ids傳回后端就行了
比如
handleExportApp(row) { const ids = row ? [row.id] : this.checkedRows.map(v => v.id); //exportApp為后端導(dǎo)出接口 exportApp(ids.join(",")); },
2.后端接口
public void exportApp(String ids, HttpServletResponse response) { if (StringUtils.isBlank(ids)) { throw new BusinessException("參數(shù)不能為空"); } List<String> idsList = Arrays.asList(ids.split(",")); List<App> list = appService.findAppAllListByIds(idsList); //創(chuàng)建HttpServerResponse的輸出流 OutputStream out = null; try { out = response.getOutputStream(); BufferedInputStream bis; File file = new File("應(yīng)用數(shù)據(jù)包.zip"); //通過ZipOutputStream定義要寫入的對象 ZipOutputStream zos = null; zos = new ZipOutputStream(new FileOutputStream(file)); writeZos(list, zos); zos.close(); //定義返回類型 response.setContentType("text/html; charset=UTF-8"); response.setContentType("application/octet-stream"); response.setHeader("Content-Disposition", "attachment;filename=".concat(String.valueOf(URLEncoder.encode("應(yīng)用數(shù)據(jù)包.zip", "UTF-8")))); bis = new BufferedInputStream(new FileInputStream(file)); //定義byte,長度就是要轉(zhuǎn)成zip文件的byte長度,避免浪費資源 byte[] buffer = new byte[bis.available()]; bis.read(buffer); out.flush(); out.write(buffer); file.delete(); } catch (IOException e) { logger.error("應(yīng)用數(shù)據(jù)包流寫入異常{}", e.getMessage()); throw new BusinessException("系統(tǒng)異常"); } finally { if (out != null) { try { out.close(); } catch (IOException e) { e.printStackTrace(); } } } } public void writeZos(List<App> list, ZipOutputStream zos) { list.forEach(a -> { ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream(); try { byteArrayOutputStream.write(JSONUtil.toJsonStr(a).getBytes()); zos.putNextEntry(new ZipEntry(a.getName() + ".json")); byte[] excelStream = byteArrayOutputStream.toByteArray(); zos.write(excelStream); zos.closeEntry(); } catch (IOException e) { logger.error("應(yīng)用數(shù)據(jù)包流寫入異常{}", e.getMessage()); throw new BusinessException("系統(tǒng)異常"); } }); }
拓展
如果只是導(dǎo)出json文件,不需要壓縮包的話
前端
handleExportApp(row) { this.ids = row ? [row.id] : this.checkedRows.map(v => v.id); this.loading = true; this.exportData(this.ids); }, exportData(ids) { if (ids.length > 0) { const currentId = ids.shift(); // 取出數(shù)組中的第一個id simulateClick(exportApp(currentId)); // 導(dǎo)出單個數(shù)據(jù) setTimeout(() => { this.exportData(ids); // 遞歸調(diào)用導(dǎo)出函數(shù),繼續(xù)下一個數(shù)據(jù) }, 10000); // 設(shè)置遞歸的間隔時間,以免處理過多數(shù)據(jù)造成性能問題 } },
后端
if (StringUtils.isBlank(ids)) { throw new BusinessException("參數(shù)不能為空"); } List<String> idsList = Arrays.asList(ids.split(",")); for (String id : idsList) { App app = appService.getById(id); // 忽略未找到的應(yīng)用程序 if (app == null) { continue; } ObjectMapper objectMapper = new ObjectMapper(); try { //把對象轉(zhuǎn)成json字符串 String jsonString = objectMapper.writeValueAsString(app); // 設(shè)置響應(yīng)頭部信息 response.setContentType("application/json"); response.setCharacterEncoding("UTF-8"); response.setHeader("Content-Disposition", "attachment;filename=".concat(String.valueOf(URLEncoder.encode("應(yīng)用_" + app.getName() + ".json", "UTF-8")))); // 獲取輸出流并寫入JSON字符串 PrintWriter writer = response.getWriter(); writer.write(jsonString); writer.flush(); writer.close(); } catch (IOException e) { logger.error("導(dǎo)出應(yīng)用數(shù)據(jù)異常:{}", e.getMessage()); throw new BusinessException("系統(tǒng)異常"); } }
但是這樣有一個不好的地方,就是前端用戶體驗感不是很好,需要等待前端一個個文件導(dǎo)出。
到此這篇關(guān)于java實現(xiàn)導(dǎo)出數(shù)據(jù)為zip壓縮文件的文章就介紹到這了,更多相關(guān)java數(shù)據(jù)導(dǎo)出為zip內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
解決idea報錯 Connot resolve column 的問題
這篇文章主要介紹了解決idea報錯 Connot resolve column 的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-02-02Java String方法獲取字符出現(xiàn)次數(shù)及字符最大相同部分示例
這篇文章主要介紹了Java String方法獲取字符出現(xiàn)次數(shù)及字符最大相同部分,涉及java字符串的遍歷、比較、計算等相關(guān)操作技巧,需要的朋友可以參考下2017-09-09圖解Java經(jīng)典算法冒泡排序的原理與實現(xiàn)
冒泡排序是一種簡單的排序算法,它也是一種穩(wěn)定排序算法。其實現(xiàn)原理是重復(fù)掃描待排序序列,并比較每一對相鄰的元素,當(dāng)該對元素順序不正確時進(jìn)行交換。一直重復(fù)這個過程,直到?jīng)]有任何兩個相鄰元素可以交換,就表明完成了排序2022-09-09關(guān)于HashMap 并發(fā)時會引起死循環(huán)的問題解析
JDK1.8之前采用頭插,即在鏈表結(jié)構(gòu)上每次都把數(shù)據(jù)放在鏈表頭部。JDK1.8采用尾插方法,很多朋友在學(xué)習(xí)Java并發(fā)容器和框架時,看到為什么要使用ConcurrentHashMap時不知道究其原因,今天小編通過本文給大家介紹下HashMap 并發(fā)死循環(huán)問題,一起看看吧2021-05-05