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)文章
詳解Java中多線程異常捕獲Runnable的實現(xiàn)
這篇文章主要介紹了詳解Java中多線程異常捕獲Runnable的實現(xiàn)的相關(guān)資料,希望通過本文能幫助到大家,讓大家理解掌握這樣的知識,需要的朋友可以參考下2017-10-10
JavaWeb 入門篇:創(chuàng)建Web項目,Idea配置tomcat
這篇文章主要介紹了IDEA創(chuàng)建web項目配置Tomcat的詳細(xì)教程,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-07-07
java文件操作工具類實現(xiàn)復(fù)制文件和文件合并
這篇文章主要介紹了java文件操作工具類,類實現(xiàn)了復(fù)制文件和文件合并的功能,需要的朋友可以參考下2014-03-03
java 學(xué)習(xí)筆記(入門篇)_java的基礎(chǔ)語法
從基礎(chǔ)語法開始,這個語法你也可以理解為英語或是漢語里面的語法,只不過大家各有各的特點和區(qū)別;那么在學(xué)習(xí)的過程中我們就要不斷的積累重要的類和方法,這樣寫程序就會方便快捷了,下面就開始學(xué)習(xí)java的基礎(chǔ)語法2013-01-01
Springboot @Configuration @bean注解作用解析
這篇文章主要介紹了springboot @Configuration @bean注解作用解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-02-02

