Java實(shí)現(xiàn)批量下載選中文件功能
1.在action中定義變量
private List<String> downLoadPaths = new ArrayList<String>();//存儲(chǔ)選中文件的下載地址 private OutputStream res; private ZipOutputStream zos; private String outPath; private String lessionIdStr;// 選中文件ID拼接的字符串 private String fileName; //瀏覽器下載彈出框中顯示的文件名
分別給出get和set方法
2. 主方法
/**
* 下載多個(gè)文件:壓縮成zip
*
* @return
* @throws Exception
*/
public String downLoadLessionsZip() {
downLoadPaths.clear();
String firstFileName = "";// 第一個(gè)文件的文件名
List<DownLoadFileVo> fileVos = new LinkedList<DownLoadFileVo>();
if (StringUtils.isNotEmpty(lessionIdStr)) {
int end = lessionIdStr.lastIndexOf(",");
if (end > 0) {
if (end == lessionIdStr.length() - 1) {
lessionIdStr = lessionIdStr.substring(0, end);
}
String[] ids = lessionIdStr.split(",");
for (int i = 0; i < ids.length; i++) {
if (StringUtils.isNumeric(ids[i])) {
BkPersonLession lession = bkPersonLessionService.downLoadLession(Integer.parseInt(ids[i]));
if (lession != null) {
fileVos.add(new DownLoadFileVo(lession
.getLessionName(), getContextRealPath()
+ lession.getLessionSavePath()));
downLoadPaths.add(getContextRealPath()
+ lession.getLessionSavePath());
}
if (i == 0) {
firstFileName = lession.getLessionName();
}
}
}
}
}
// 有數(shù)據(jù)可以下載
if (downLoadPaths.size() != 0) {
// 進(jìn)行預(yù)處理
preProcess(firstFileName);
} else {
// 沒(méi)有文件可以下載,返回nodata
return "nodata";
}
// 處理
writeZip(fileVos);
// 后處理關(guān)閉流
afterProcess();
return null;
}
// 壓縮處理
public void writeZip(List<DownLoadFileVo> fileVos) {
byte[] buf = new byte[8192];
int len;
for (DownLoadFileVo fileVo : fileVos) {
File file = new File(fileVo.getFileSavePath());
if (!file.isFile())
continue;
ZipEntry ze = new ZipEntry(fileVo.getFileName()
+ fileVo.getFileSavePath().substring(
fileVo.getFileSavePath().lastIndexOf(".")));
try {
zos.putNextEntry(ze);
BufferedInputStream bis = new BufferedInputStream(
new FileInputStream(file));
while ((len = bis.read(buf)) > 0) {
zos.write(buf, 0, len);
}
bis.close();
zos.closeEntry();
} catch (IOException e) {
e.printStackTrace();
}
}
}
// 預(yù)處理
public void preProcess(String firseFileName) {
String zipName = "【批量下載】" + firseFileName + "等.zip";
String filename = "";
try {
filename = new String(zipName.getBytes("GBK"), "8859_1");
} catch (UnsupportedEncodingException e1) {
e1.printStackTrace();
}
this.fileName = filename;
HttpServletResponse response = ServletActionContext.getResponse();
try {
res = response.getOutputStream();
// 清空輸出流(在迅雷下載不會(huì)出現(xiàn)一長(zhǎng)竄)
response.reset();
// 設(shè)定輸出文件頭
response.setHeader("Content-Disposition", "attachment;fileName="
+ filename);
response.setContentType("application/zip");
zos = new ZipOutputStream(res);
} catch (IOException e) {
e.printStackTrace();
}
}
// 后處理
public void afterProcess() {
try {
if (zos != null) {
zos.close();
}
if (res != null) {
res.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
3. 在struts.xml中配置
<action name="downLoadBkPersonLessionsZip" class="bkPersonLessionAction"
method="downLoadLessionsZip">//class值為bean.xml中配置的bean
<result name="nodata" type="httpheader">
<param name="status">204</param>//表示響應(yīng)執(zhí)行成功,但沒(méi)有數(shù)據(jù)返回,瀏覽器不用刷新,不用導(dǎo)向新頁(yè)面
</result>
</action>
總結(jié)
以上所述是小編給大家介紹的Java實(shí)現(xiàn)批量下載選中文件功能,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- Java實(shí)現(xiàn)批量向mysql寫入數(shù)據(jù)的方法
- Java OSS批量下載并壓縮為ZIP代碼實(shí)例
- java線程池實(shí)現(xiàn)批量下載文件
- java實(shí)現(xiàn)批量下載 多文件打包成zip格式下載
- java后臺(tái)批量下載文件并壓縮成zip下載的方法
- Java 批量文件壓縮導(dǎo)出并下載到本地示例代碼
- JAVA SFTP文件上傳、下載及批量下載實(shí)例
- Java實(shí)現(xiàn)FTP批量大文件上傳下載篇2
- Java實(shí)現(xiàn)FTP批量大文件上傳下載篇1
- javaweb文件打包批量下載代碼
- Java批量寫入文件和下載圖片的示例代碼
相關(guān)文章
Spring Boot3.x自動(dòng)配置不生效的排查與解決方法(IDEA 文件夾命名導(dǎo)致的問(wèn)題)
在SpringBoot多模塊項(xiàng)目中,自動(dòng)配置類未生效的問(wèn)題通常源于文件路徑錯(cuò)誤,通過(guò)檢查和修正AutoConfiguration.imports文件的實(shí)際路徑,可以解決自動(dòng)配置不生效的問(wèn)題,感興趣的朋友跟隨小編一起看看吧2024-11-11
SpringBoot詳細(xì)探究講解默認(rèn)組件掃描
在項(xiàng)目中我們創(chuàng)建了Controller,這個(gè)Controller是如何被spring自動(dòng)加載的呢?為什么Controller必須放在啟動(dòng)類的同級(jí)目錄下呢2022-06-06
Java并發(fā)編程中的ConcurrentLinkedQueue詳解
這篇文章主要介紹了Java并發(fā)編程中的ConcurrentLinkedQueue詳解,GetThread線程不會(huì)因?yàn)镃oncurrentLinkedQueue隊(duì)列為空而等待,而是直接返回null,所以當(dāng)實(shí)現(xiàn)隊(duì)列不空時(shí),等待時(shí),則需要用戶自己實(shí)現(xiàn)等待邏輯,需要的朋友可以參考下2023-12-12
MyBatis自定義映射resultMap的實(shí)現(xiàn)
本文主要介紹了MyBatis自定義映射resultMap的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03
詳解基于MVC的數(shù)據(jù)查詢模塊進(jìn)行模糊查詢
這篇文章主要介紹了Java基于MVC的數(shù)據(jù)查詢模塊進(jìn)行模糊查詢,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01
springboot serverEndpoint導(dǎo)致@resource注解不生效
在SpringBoot中,@Resource注解用于注入依賴,本文主要介紹了springboot serverEndpoint導(dǎo)致@resource注解不生效,具有一定的參考價(jià)值,感興趣的可以了解一下2023-12-12
Java加載properties文件實(shí)現(xiàn)方式詳解
這篇文章主要介紹了Java加載properties文件實(shí)現(xiàn)方式詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-07-07

