說明 : 剛寫一篇關於導出db文件爲sql腳本的文章 --https://my.oschina.net/u/3774949/blog/4470489 但是如果數據量特別大的話就可能很慢,原來是因爲導出db文件亂碼所以想解析數據庫並生成sql腳本方式, 就在剛剛破案了 ,果不其然確實是前端js部分出了問題
- 先把前端代碼貼出來吧
//按說blob和arraybuffer都是一樣處理二進制的但是看了下介紹說arraybuffer比blob更貼近原型 axios({ url: '/api/backups/backupsDownLoad', method: 'GET', responseType: 'arraybuffer', params: { name: backups.name } }).then((res) => { console.log(res) const content = res.data const blob = new Blob([content], {type: "application/octet-stream"}) let fileName = backups.name.substring(0, backups.name.indexOf(".")) + ".db"; if ('download' in document.createElement('a')) { // 非IE下載 const elink = document.createElement('a') elink.download = fileName elink.style.display = 'none' elink.href = window.URL.createObjectURL(blob) document.body.appendChild(elink) elink.click() window.URL.revokeObjectURL(elink.href) // 釋放URL 對象 document.body.removeChild(elink) } else { // IE10+下載 navigator.msSaveBlob(blob, fileName) } }).catch((err) => { console.log(err); })
猜想:是不是因爲blob的原因
這個是用arraybuffer 打印出來的信息,是有序的二進制數組
但是如果使用 blob打印出來的就是
emm又可以用了 ,這就有意思了
又翻翻剛剛寫的博客 emm ajax 妙哉呀 --- 先看看這個打印信息是啥
emm 怎麼是中文亂碼的,再看一眼代碼
我也有設置這個rsponseType怎麼不一樣呢,難道這個是axios專有屬性??不會怎麼辦,當然問度娘
發現ajax的rsponseType不能直接設置,需要這樣來
xhrFields: { responseType: "blob" },
測試 --
看到這個我就知道可以了 好吧,我承認我是菜雞,我攤牌了(我是誰?我在哪?我在幹啥?)
不要問我爲啥一開始不用axios,是因爲我前端是用的cdn方式的vue,不是vue axios有點不友好,
不能this直接指向,加上我要傳參,怎麼寫都爆紅線 ,煩,就換了
後端代碼就不貼了,頂上連接文章的改吧改吧就能用
如果是大文件還是建議壓縮一下
放個壓縮的工具類 --當然也是搜的了假裝你看不見這句話
package com.tzh.hw.utils; import org.apache.tools.zip.ZipEntry; import org.apache.tools.zip.ZipOutputStream; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.*; import java.util.ArrayList; import java.util.List; /** * 文件壓縮 * @author xing.Li * @date 2020/08/05 */ public class FileZipUtils { /** * 把文件打成壓縮包並保存在本地硬盤 * @param srcFiles 原路徑(裏面是string類型的路徑集合) * @param zipPath zip路徑 */ public static void ZipFiles(List srcFiles, String zipPath) { byte[] buf = new byte[4096]; ZipOutputStream out = null; try { // 創建zip輸出流 out = new ZipOutputStream(new FileOutputStream(zipPath)); // 循環將源文件列表添加到zip文件中 for (int i = 0; i < srcFiles.size(); i++) { File file = new File((String) srcFiles.get(i)); FileInputStream in = new FileInputStream(file); String fileName = file.getName(); // 將文件名作爲zip的Entry存入zip文件中 out.putNextEntry(new ZipEntry(fileName)); int len; while ( (len = in.read(buf)) > 0) { out.write(buf, 0, len); } out.closeEntry(); in.close(); } } catch (IOException e) { e.printStackTrace(); } finally { if (null != out) { try { out.close(); out = null; } catch (IOException e) { e.printStackTrace(); } } } } /** * zip文件 * 把文件列表打成壓縮包並輸出到客戶端瀏覽器中 * * @param request 請求 * @param response 響應 * @param srcFiles 原路徑(裏面是string類型的路徑集合) * @param zipName aip的名字 */ public static void ZipFiles(HttpServletRequest request, HttpServletResponse response, List srcFiles, String zipName) { byte[] buf = new byte[4096]; try { //--設置成這樣可以不用保存在本地,再輸出,通過response流輸出,直接輸出到客戶端瀏覽器中。 ZipOutputStream out = new ZipOutputStream(response.getOutputStream()); // Compress the files if (request.getHeader("User-Agent").toLowerCase().indexOf("firefox") > 0) { zipName = new String(zipName.getBytes("GB2312"),"ISO-8859-1"); } else { // 對文件名進行編碼處理中文問題 zipName = java.net.URLEncoder.encode(zipName, "UTF-8"); zipName = new String(zipName.getBytes("UTF-8"), "GBK"); } // 重點 不同類型的文件對應不同的MIME類型---setContentType response.reset(); response.setCharacterEncoding("UTF-8"); response.setContentType("application/zip; application/octet-stream"); // inline在瀏覽器中直接顯示,不提示用戶下載 // attachment彈出對話框,提示用戶進行下載保存本地 // 默認爲inline方式 response.setHeader("Content-Disposition", "attachment;filename=" + zipName); for (int i = 0; i < srcFiles.size(); i++) { File file = new File((String) srcFiles.get(i)); FileInputStream in = new FileInputStream(file); // Add ZIP entry to output stream. String fileName = file.getName(); out.putNextEntry(new ZipEntry(fileName)); // Transfer bytes from the file to the ZIP file int len; while ( (len = in.read(buf)) > 0) { out.write(buf, 0, len); } // Complete the entry out.closeEntry(); in.close(); } // Complete the ZIP file out.close(); } catch (IOException e) { e.printStackTrace(); } } /** * 把文件目錄打成壓縮包並輸出到客戶端瀏覽器中 * @param request 請求 * @param response 響應 * @param sourcePath 原路徑(裏面是string類型的路徑集合) * @param zipName 下載zip文件名稱 */ public static void createZip(HttpServletRequest request, HttpServletResponse response, String sourcePath, String zipName) { FileOutputStream fos = null; ZipOutputStream out = null; try { //--設置成這樣可以不用保存在本地,再輸出,通過response流輸出,直接輸出到客戶端瀏覽器中。 out = new ZipOutputStream(response.getOutputStream()); //此處修改字節碼方式。 out.setEncoding("gbk"); if (request.getHeader("User-Agent").toLowerCase().indexOf("firefox") > 0) { zipName = new String(zipName.getBytes("GB2312"),"ISO-8859-1"); } else { // 對文件名進行編碼處理中文問題 zipName = java.net.URLEncoder.encode(zipName, "UTF-8"); zipName = new String(zipName.getBytes("UTF-8"), "GBK"); } // 重點 不同類型的文件對應不同的MIME類型---setContentType response.reset(); response.setCharacterEncoding("UTF-8"); response.setContentType("application/x-msdownload"); // 默認爲inline方式 response.setHeader("Content-Disposition", "attachment;filename=" + zipName); writeZip(new File(sourcePath),"",out); out.flush(); out.close(); } catch (IOException e) { e.printStackTrace(); } finally { if(out != null) { try { out.close(); out = null; } catch (IOException e) { e.printStackTrace(); } } if(fos != null) { try { fos.close(); fos = null; } catch (IOException e) { e.printStackTrace(); } } } } /** * 創建zip文件 * @param file 文件或者目錄 * @param parentPath 父路徑(默認爲"") * @param zos ZipOutputStream */ private static void writeZip(File file, String parentPath, ZipOutputStream zos) { if(file.exists()){ //處理文件夾 if(file.isDirectory()){ parentPath += file.getName() + File.separator; File [] files=file.listFiles(); if(files.length != 0) { for(File f:files){ writeZip(f, parentPath, zos); } } else {//空目錄則創建當前目錄 try { zos.putNextEntry(new ZipEntry(parentPath)); } catch (IOException e) { e.printStackTrace(); } } } else { FileInputStream fis=null; try { fis=new FileInputStream(file); ZipEntry ze = new ZipEntry(parentPath + file.getName()); zos.putNextEntry(ze); byte [] content=new byte[1024]; int len; while((len=fis.read(content))!=-1){ zos.write(content,0,len); zos.flush(); } } catch (FileNotFoundException e) { System.out.println("創建ZIP文件失敗"); } catch (IOException e) { System.out.println("創建ZIP文件失敗"); }finally{ try { if(fis!=null){ fis.close(); } }catch(IOException e){ System.out.println("創建ZIP文件失敗"); } } } } } public static void main(String[] args) { //測試文件列表 List srcFiles = new ArrayList(); srcFiles.add("C:\\Users\\hw\\Desktop\\hdvdatasrv.db"); ZipFiles(srcFiles, "C:\\Users\\hw\\Desktop\\cszip.zip"); } } 拜拜了,最後祝看文章的小夥伴大富大貴 多財多億 人早生貴子新婚快樂......