前端ajax請(qǐng)求+后端java實(shí)現(xiàn)的下載zip壓縮包功能示例
ajax請(qǐng)求 下載zip壓縮包
后臺(tái)最主要是 response.setContentType(“application/octet-stream”);
以及 response.addHeader(“Content-Disposition”, “attachment;fileName=” + URLEncoder.encode(“圖片.zip”, “UTF-8”));
一、后臺(tái)代碼
@PostMapping("/downloadZip")
public void downloadCerts(HttpServletRequest request, HttpServletResponse response, @RequestBody List<String> ids) throws UnsupportedEncodingException {
//文件流octet-stream
response.setContentType("application/octet-stream");
response.setCharacterEncoding("utf-8");
response.addHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode("圖片.zip", "UTF-8"));
try {
ZipOutputStream resultStream = new ZipOutputStream(response.getOutputStream());
// 這里是查詢數(shù)據(jù)庫
List<Map> result = service.downloadCerts(ids);
byte[] buffer = new byte[10240];
for (Map map :result) {
//因?yàn)閿?shù)據(jù)庫保存的是圖片的base64 所以需要轉(zhuǎn)換
BASE64Decoder decoder = new BASE64Decoder();
File certFace = new File("temp.png");
OutputStream out = new FileOutputStream(certFace);
byte[] b = decoder.decodeBuffer(((String) map.get("certB64")).split(",")[1]);
for (int i = 0; i <b.length ; i++) {
if (b[i] <0) {
b[i]+=256;
}
}
out.write(b);
out.flush();
out.close();
//到這里 base64 轉(zhuǎn)換成了圖片
//往zip里面壓入第一個(gè)文件 本地文件
resultStream.putNextEntry(new ZipEntry("本地圖片.png" ));
InputStream stream = new FileInputStream(new File("temp.png"));
int len;
// 讀入需要下載的文件的內(nèi)容,打包到zip文件
while ((len = stream.read(buffer)) > 0) {
resultStream.write(buffer, 0, len);
}
resultStream.closeEntry();
stream.close();
resultStream.flush();
//第一個(gè)文件壓入完成 關(guān)閉流 刷新一下緩沖區(qū)
// 往zip里面壓入第二個(gè)文件 網(wǎng)絡(luò)文件 例:https://profile.csdnimg.cn/8/C/E/3_blogdevteam
resultStream.putNextEntry(new ZipEntry("網(wǎng)絡(luò)圖片.png"));
URL url = new URL("https://profile.csdnimg.cn/8/C/E/3_blogdevteam";);
String str = url.toString();
URLConnection connection = url.openConnection();
InputStream backStream = connection.getInputStream();
// 讀入需要下載的文件的內(nèi)容,打包到zip文件
while ((len = backStream.read(buffer)) > 0) {
resultStream.write(buffer, 0, len);
}
resultStream.closeEntry();
backStream.close();
resultStream.flush();
//第二個(gè)文件壓入完成 關(guān)閉流 刷新一下緩沖區(qū)
}
resultStream.close();
//關(guān)閉流
} catch (IOException e) {
e.printStackTrace();
}
}二、前端代碼
前端代碼比較簡單 直接貼出 我使用的是vue的 axios
download(this.ids).then((response) =>{
if (response.status == 200) {
let url = window.URL.createObjectURL(new Blob([response.data]))
let link= document.createElement('a')
link.style.display='none'
link.href=url
link.setAttribute('download', "圖片.zip") // 自定義下載文件名(如exemple.txt)
document.body.appendChild(link)
link.click()
}else{
this.$message.error("下載出錯(cuò)了");
}
});這里的 download(this.ids) 是封裝過的axios 重點(diǎn)是 then里的代碼
問題
如果你發(fā)現(xiàn)下載的文件比源文件大,很可能是前端請(qǐng)求需要加入以下代碼
responseType:'blob',
注意:筆者在測試過程中發(fā)現(xiàn)一些網(wǎng)站帶有防盜鏈功能,需要referer驗(yàn)證。另外還可能會(huì)出現(xiàn)前端blob格式轉(zhuǎn)換、跨域等諸多問題 ,需要讀者酌情處理。
相關(guān)文章
Ajax登陸使用Spring Security緩存跳轉(zhuǎn)到登陸前的鏈接
這篇文章主要介紹了Ajax登陸使用Spring Security緩存跳轉(zhuǎn)到登陸前的鏈接,需要的朋友可以參考下2019-04-04
在(ASP/PHP/JSP/html/js)中禁止ajax緩存的方法集錦
禁止ajax緩存最簡單的辦法就是在js端直接生成一個(gè)隨機(jī)數(shù)了,但是有時(shí)會(huì)發(fā)現(xiàn)此方法不適用于post了,如果我們要禁止post 提交數(shù)據(jù)的ajax緩存需要怎么處理呢,下面我整理了很多關(guān)于禁止ajax緩存的例子2014-08-08
jquery ajax實(shí)現(xiàn)文件上傳功能實(shí)例代碼
這篇文章主要介紹了jquery ajax實(shí)現(xiàn)文件拖拽上傳功能的實(shí)例代碼,代碼簡單易懂,非常不錯(cuò),具有參考借鑒價(jià)值,需要的朋友可以參考下2018-04-04
用iframe設(shè)置代理解決ajax跨域請(qǐng)求問題
面對(duì)ajax跨域請(qǐng)求的問題,想用代理的方式來解決這個(gè)跨域問題。在服務(wù)器端創(chuàng)建一個(gè)靜態(tài)的代理頁面,在客戶端用iframe調(diào)用這個(gè)代理2013-11-11
AJAX的原理—如何做到異步和局部刷新【實(shí)現(xiàn)代碼】
如何做到異步和局部刷新?下面小編就為大家?guī)硪黄狝JAX的原理—如何做到異步和局部刷新【實(shí)現(xiàn)代碼】。小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-05-05

