1.使用ajax下载失败。
失败原因
ajax的返回值类型是json,text,html,xml类型,或者可以说ajax的接收类型只能是string字符串,不是流类型,所以无法实现文件下载。但用ajax仍然可以获得文件的内容,该文件将被保留在内存中,无法将文件保存到磁盘。这是因为JavaScript无法和磁盘进行交互,否则这会是一个严重的安全问题,js无法调用到浏览器的下载处理机制和程序,会被浏览器阻塞。
解决方式
1.隐藏表单,用提交表单的形式
2.用window.open() 或 window.location.href()
3.创建iframe,iframe的src可以是文件地址url来直接下载文件
使用表单提交
$("#btnDownload").click(function(){
var arr = new Array;
var nodes = tree.getCheckedNodes(true);
if(nodes!=""){
for(var i=0; i<nodes.length; i++) {
arr[i] = nodes[i].path;
}
var url = "${pageContext.request.contextPath}/download/image";
// 封装装form表单
var form = $("<form></form>").attr("action", url).attr("method", "post");
// 封装参数
form.append($("<input></input>").attr("type", "hidden").attr("name", "imageArray").attr("value", arr));
// form.append($("<input></input>").attr("type", "hidden").attr("name", "filePath").attr("value", this.formData.filePath));
// 提交
form.appendTo('body').submit().remove();
}else{
alertx("请选择要下载的文件!");
return ;
}
});
使用windows.open方法提交,但新窗口让它藏着页面底下
var wWidth = document.body.clientWidth ;
var wHeight =document.body.clientHeight +1000;
window.open('AA.action','newwindow','height=5,width=5,top='+wHeight+',left='+wWidth+',toolbar=no,menubar=no,scrollbars=no, resizable=no,location=no, status=no')
使用hidden的iframe提交。
var downloadURL = "AA.action";
var iframe = document.createElement("iframe");
iframe.src = downloadURL;
iframe.style.display = "none";
document.body.appendChild(iframe);
后端直接打包下载
@RequestMapping(value = "image", method=RequestMethod.POST)
public void download(@RequestParam("imageArray") String [] array, HttpServletResponse response){
try {
String downloadFilename = "文件.zip";//文件的名称
downloadFilename = URLEncoder.encode(downloadFilename, "UTF-8");//转换中文否则可能会产生乱码
response.setContentType("application/octet-stream");// 指明response的返回对象是文件流
response.setHeader("Content-Disposition", "attachment;filename=" + downloadFilename);// 设置在下载框默认显示的文件名
ZipOutputStream zos = new ZipOutputStream(response.getOutputStream());
// String[] files =`在这里插入代码片` new String[]{"http://xxxx/xx.jpg","http://xxx/xx.jpg"};
for (int i=0;i<array.length;i++) {
String path = Cryptos.aesDecrypt(array[i]);
//URL url = new URL(path);
File file = new File(path);
zos.putNextEntry(new ZipEntry(file.getName()));
//FileInputStream fis = new FileInputStream(new File(files[i]));
//InputStream fis = url.openConnection().getInputStream();
InputStream fis = new BufferedInputStream(new FileInputStream(file.getPath()));
byte[] buffer = new byte[1024];
int r = 0;
while ((r = fis.read(buffer)) != -1) {
zos.write(buffer, 0, r);
}
fis.close();
}
zos.flush();
zos.close();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
后端打包在本地,然后把本地压缩包输出到客户端下载。
// @RequestMapping(value = "image", method=RequestMethod.POST)
public void downloads(@RequestParam("imageArray") String[] filepath,HttpServletRequest request,HttpServletResponse response) throws IOException, ServletException{
// 要生成的压缩文件地址和文件名称
String str=request.getSession().getServletContext().getRealPath("/");
String path = str.substring(0,3)+File.separator+"文件.zip";
//String path =request.getSession().getServletContext().getRealPath("/") + "static\\lodop\\文件.zip";
File zipFile = new File(path);
ZipOutputStream zipStream = null;
FileInputStream zipSource = null;
BufferedInputStream bufferStream = null;
try {
//构造最终压缩包的输出流
zipStream = new ZipOutputStream(new FileOutputStream(zipFile));
for(int i =0;i<filepath.length;i++){
File file = new File(Cryptos.aesDecrypt(filepath[i]));
//将需要压缩的文件格式化为输入流
zipSource = new FileInputStream(file);
//压缩条目不是具体独立的文件,而是压缩包文件列表中的列表项,称为条目,就像索引一样
ZipEntry zipEntry = new ZipEntry(file.getName());
//定位该压缩条目位置,开始写入文件到压缩包中
zipStream.putNextEntry(zipEntry);
//输入缓冲流
bufferStream = new BufferedInputStream(zipSource, 1024 * 10);
int read = 0;
//创建读写缓冲区
byte[] buf = new byte[1024 * 10];
while((read = bufferStream.read(buf, 0, 1024 * 10)) != -1){
zipStream.write(buf, 0, read);
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
//关闭流
try {
if(null != bufferStream) bufferStream.close();
if(null != zipStream) zipStream.close();
if(null != zipSource) zipSource.close();
} catch (IOException e) {
e.printStackTrace();
}
}
//2.获取要下载的文件名
String fileName = path.substring(path.lastIndexOf("\\")+1);
//3.设置content-disposition响应头控制浏览器以下载的形式打开文件
File fi=new File(path);
response.reset();
response.setHeader("Content-Disposition", "attachment;filename="+URLEncoder.encode(fileName, "UTF-8"));
response.addHeader("Content-Length", "" + fi.length());
response.setContentType("application/octet-stream");
//4.获取要下载的文件输入流
InputStream in = new BufferedInputStream(new FileInputStream(fi));
int len = 0;
//5.创建数据缓冲区
byte[] buffer = new byte[1024];
//6.通过response对象获取OutputStream流
OutputStream out = new BufferedOutputStream(response.getOutputStream());
//7.将FileInputStream流写入到buffer缓冲区
while ((len = in.read(buffer)) !=-1) {
//8.使用OutputStream将缓冲区的数据输出到客户端浏览器
out.write(buffer,0,len);
}
out.flush();
out.close();
in.close();
}