springMvcZip下載 壓縮流實現批量下載Word文件
阿新 • • 發佈:2019-02-04
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;
/**
* Created by LiuD on 2017/11/7
*/
public class TestZip {
/**
* 批量下載
*/
@ResponseBody
@RequestMapping("/download.ajax")
public class download(HttpServletResponse response) {
List<File> list = new ArrayList<>();
String s1 = "好好學習";
String fileName1 = s1 + ".doc";
File file1 = new File(fileName1);
htmlToDoc(s1, file1);
list.add(file1);
String s2 = "天天向上";
String fileName2 = s2 + ".doc" ;
File file2 = new File(fileName2);
htmlToDoc(s2, file2);
list.add(file2);
String s3 = "好好學習12321AA";
String fileName3 = s3 + ".doc";
File file3 = new File(fileName3);
htmlToDoc(s3, file3);
list.add(file3);
try {
// 要壓縮的檔案路徑
File file = new File("e:\\1.zip");
if (file.exists()) {
System.out.println("該檔名已存在。");
} else {
boolean newFile = file.createNewFile();
if (newFile) {
System.out.println("恭喜你,下載成功");
} else {
System.out.println("下載失敗。請稍後再試···");
}
}
//建立檔案輸出流
FileOutputStream fileOutputStream = new FileOutputStream(file);
// ZipOutputStream輸出流轉換
ZipOutputStream zipOutputStream = new ZipOutputStream(fileOutputStream);
// 接收檔案集合、壓縮流
zipFileAll(list, zipOutputStream);
zipOutputStream.close();
fileOutputStream.close();
downloadZip(file, response);
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 把接收的全部檔案打成壓縮包
*
* @param files (檔案集合)
* @param outputStream (壓縮流、輸出)
*/
private static void zipFileAll(List files, ZipOutputStream outputStream) {
for (Object file1 : files) {
File file = (File) file1;
zipFile(file, outputStream);
}
}
/**
* 根據輸入的檔案與輸出流對檔案進行打包
*
* @param file (單個檔案物件)
* @param outputStream (壓縮流、輸出)
*/
private static void zipFile(File file, ZipOutputStream outputStream) {
try {
if (file.exists()) {
if (file.isFile()) {
FileInputStream IN = new FileInputStream(file);
BufferedInputStream bins = new BufferedInputStream(IN, 1024);
// 將檔名寫入壓縮檔案中
ZipEntry entry = new ZipEntry(file.getName());
outputStream.putNextEntry(entry);
// 向壓縮檔案中輸出資料
int nNumber;
byte[] buffer = new byte[1024];
while ((nNumber = bins.read(buffer)) != -1) {
outputStream.write(buffer, 0, nNumber);
}
outputStream.flush();
bins.close();
IN.close();
} else {
File[] files = file.listFiles();
for (File file1 : files) {
zipFile(file1, outputStream);
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 下載檔案,支援跨域
*
* @param file (要下載的檔案)
* @param response
*/
privtal static void 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.reset();
OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(file.getName(), "UTF-8"));
toClient.write(buffer);
toClient.flush();
toClient.close();
} catch (IOException ex) {
ex.printStackTrace();
} finally {
try {
File f = new File(file.getPath());
f.delete();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}