关于java调用浏览器下载sqllite db文件的后续及zip压缩代码
说明 : 刚写一篇关于导出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");
}
}
拜拜了,最后祝看文章的小伙伴大富大贵 多财多亿 人早生贵子新婚快乐......


