需求
導出文件后存留在了服務器中,需要提供下載按鈕,點擊后可下載到本地;(因為涉及多個文件,下載前先將文件進行壓縮,提供下載壓縮文件)
效果預覽
代碼
主要方法
/**
* 下載生成的所有在線/離線用戶信息表格
* @param request
* @param response
* @return 壓縮文件
* @throws FTPConnectionClosedException
* @throws IOException
*/ public File downloadExcel (HttpServletRequest request, HttpServletResponse response) throws FTPConnectionClosedException, IOException { //提供下載文件前進行壓縮,即服務端生成壓縮文件 File file = new File(zipPath);
FileOutputStream fos = new FileOutputStream(file);
ZipUtils.toZip(path, fos, true); //1.獲取要下載的文件的絕對路徑 String realPath = zipPath; //2.獲取要下載的文件名 String fileName = realPath.substring(realPath.lastIndexOf(File.separator)+1);
response.reset();
response.setCharacterEncoding("UTF-8");
response.setContentType("application/octet-stream"); //3.設置content-disposition響應頭控制瀏覽器以下載的形式打開文件 response.addHeader("Content-Disposition","attachment;filename=" + new String(fileName.getBytes(),"utf-8")); //獲取文件輸入流 InputStream in = new FileInputStream(realPath); int len = 0; byte[] buffer = new byte[1024];
OutputStream out = response.getOutputStream(); while ((len = in.read(buffer)) > 0) { //將緩沖區的數據輸出到客戶端瀏覽器 out.write(buffer,0,len);
} in.close(); return file;
}
壓縮方法
public static void toZip(String srcDir, OutputStream out, boolean KeepDirStructure) throws RuntimeException{ long start = System.currentTimeMillis();
ZipOutputStream zos = null ; try {
zos = new ZipOutputStream(out);
File sourceFile = new File(srcDir);
compress(sourceFile,zos,sourceFile.getName(),KeepDirStructure); long end = System.currentTimeMillis();
System.out.println("壓縮完成,耗時:" + (end - start) +" ms");
} catch (Exception e) { throw new RuntimeException("zip error from ZipUtils",e);
}finally{ if(zos != null){ try {
zos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
* 遞歸壓縮方法
* @param sourceFile 源文件
* @param zos zip輸出流
* @param name 壓縮后的名稱
* @param KeepDirStructure 是否保留原來的目錄結構,true:保留目錄結構;
* false:所有文件跑到壓縮包根目錄下(注意:不保留目錄結構可能會出現同名文件,會壓縮失敗)
* @throws Exception
*/ private static void compress(File sourceFile, ZipOutputStream zos, String name, boolean KeepDirStructure) throws Exception{ byte[] buf = new byte[BUFFER_SIZE]; if(sourceFile.isFile()){ // 向zip輸出流中添加一個zip實體,構造器中name為zip實體的文件的名字 zos.putNextEntry(new ZipEntry(name)); // copy文件到zip輸出流中 int len;
FileInputStream in = new FileInputStream(sourceFile); while ((len = in.read(buf)) != -1){
zos.write(buf, 0, len);
} // Complete the entry zos.closeEntry(); in.close();
} else {
File[] listFiles = sourceFile.listFiles(); if(listFiles == null || listFiles.length == 0){ // 需要保留原來的文件結構時,需要對空文件夾進行處理 if(KeepDirStructure){ // 空文件夾的處理 zos.putNextEntry(new ZipEntry(name + "/")); // 沒有文件,不需要文件的copy zos.closeEntry();
}
}else { for (File file : listFiles) { // 判斷是否需要保留原來的文件結構 if (KeepDirStructure) { // 注意:file.getName()前面需要帶上父文件夾的名字加一斜杠, // 不然最后壓縮包中就不能保留原來的文件結構,即:所有文件都跑到壓縮包根目錄下了 compress(file, zos, name + "/" + file.getName(),KeepDirStructure);
} else {
compress(file, zos, file.getName(),KeepDirStructure);
}
}
}
}
}
SpringMVC
@Download @Path("downloadExcel") public File DownloadExcel(HttpServletRequest request, HttpServletResponse response) throws FTPConnectionClosedException, IOException {
File file = exportXMPPUserInfo.downloadExcel(request, response); return file;
}
前端
公司內部寫法,理解其中含義即可。
...
<input id="downloadExcel" type="submit" value="下載" />
...
<script>
...
//下載excel
W.$('downloadExcel').on('click',function(e){
W.download('userLoginService/downloadExcel');
/* W.get('userLoginService/downloadExcel').done(function(result){
console.log("下載用戶信息表");
}); */
});
...
</script>
詳細配置信息可以參考我寫的這篇文章:http://blog.ncmem.com/wordpress/2019/06/14/java%e6%89%b9%e9%87%8f%e4%b8%8b%e8%bd%bd%e6%96%87%e4%bb%b6%e5%88%b0%e6%8c%87%e5%ae%9a%e6%96%87%e4%bb%b6%e5%a4%b9/