兩種方式java
1.使用臨時文件下載的方式apache
//文件打包下載
public static HttpServletResponse downLoadFiles(List<File> files,
HttpServletRequest request, HttpServletResponse response)
throws Exception {
try {
/**這個集合就是你想要打包的全部文件,
* 這裏假設已經準備好了所要打包的文件*/
//List<File> files = new ArrayList<File>();
/**建立一個臨時壓縮文件,
* 咱們會把文件流所有注入到這個文件中
* 這裏的文件你能夠自定義是.rar仍是.zip*/
File file = new File("c:/certpics.rar");
if (!file.exists()){
file.createNewFile();
}
response.reset();
//response.getWriter()
//建立文件輸出流
FileOutputStream fous = new FileOutputStream(file);
/**打包的方法咱們會用到ZipOutputStream這樣一個輸出流,
* 因此這裏咱們把輸出流轉換一下*/
// org.apache.tools.zip.ZipOutputStream zipOut
// = new org.apache.tools.zip.ZipOutputStream(fous);
ZipOutputStream zipOut
= new ZipOutputStream(fous);
/**這個方法接受的就是一個所要打包文件的集合,
* 還有一個ZipOutputStream*/
zipFile(files, zipOut);
zipOut.close();
fous.close();
return downloadZip(file,response);
}catch (Exception e) {
e.printStackTrace();
}
/**直到文件的打包已經成功了,
* 文件的打包過程被我封裝在FileUtil.zipFile這個靜態方法中,
* 稍後會呈現出來,接下來的就是往客戶端寫數據了*/
// OutputStream out = response.getOutputStream();
return response ;
}app
/**
* 把接受的所有文件打成壓縮包
* @param List<File>;
* @param org.apache.tools.zip.ZipOutputStream
*/
public static void zipFile
(List files,ZipOutputStream outputStream) {
int size = files.size();
for(int i = 0; i < size; i++) {
File file = (File) files.get(i);
zipFile(file, outputStream);
}
}this
public static 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");
response.setHeader("Content-Disposition", "attachment;filename=" + file.getName());
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();
}
}
return response;
}spa
/**
* 根據輸入的文件與輸出流對文件進行打包
* @param File
* @param org.apache.tools.zip.ZipOutputStream
*/
public static void zipFile(File inputFile,
ZipOutputStream ouputStream) {
try {
if(inputFile.exists()) {
/**若是是目錄的話這裏是不採起操做的,
* 至於目錄的打包正在研究中*/
if (inputFile.isFile()) {
FileInputStream IN = new FileInputStream(inputFile);
BufferedInputStream bins = new BufferedInputStream(IN, 512);
//org.apache.tools.zip.ZipEntry
ZipEntry entry = new ZipEntry(inputFile.getName());
ouputStream.putNextEntry(entry);
// 向壓縮文件中輸出數據
int nNumber;
byte[] buffer = new byte[512];
while ((nNumber = bins.read(buffer)) != -1) {
ouputStream.write(buffer, 0, nNumber);
}
// 關閉建立的流對象
bins.close();
IN.close();
} else {
try {
File[] files = inputFile.listFiles();
for (int i = 0; i < files.length; i++) {
zipFile(files[i], ouputStream);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}.net
2.直接存入ZipOutputStream 下載對象