前端代码
function uf_download_new(){
var list=[];
var i=0;
var y=999;
for(var j = 0; j < ds_djmx.recordCount; j++){
if(ds_djmx.getValueAt(j,"sel")==1 && i<=y){
var sub={};
sub.path=ds_djmx.getValueAt(j,"FolderURLall"); //系统图片绝对路径
list[i]=sub;
i++;
}
}
if(i==0){
alert("请选择需要下载的图片信息.....");
return;
}
var prams={};
prams.list=list;
prams.name=FormUtil.getIdentity("serialno", "ZIP", ""); //生成随机字符串 做为下载文件名称
var url= "http://192.168.2.248:908/download_file"; //后端 java程序地址
httpPost_downLoadFile (url,prams,prams.name);
}
function httpPost_downLoadFile (url,params,fileName) {
var xhr = new XMLHttpRequest();
xhr.open('post', url,true);
xhr.setRequestHeader('Content-Type', 'application/json');
xhr.responseType = "blob"; //返回类型blob
xhr.send(Utility.toJSONString(params));
xhr.onload = function(){
// 兼容IE,只有IE浏览器的navigator有这个方法,谷歌浏览器没有;
if (navigator.msSaveBlob){
return navigator.msSaveBlob(xhr.response,fileName+'.zip'); //fileName+'.zip'前端下载的文件名 zip格式
}
var blobUrl = URL.createObjectURL(xhr.response);
//alert(blobUrl);
window.location.blobUrl;
}
}
时空软件图片路径格式
前端拼接后 prams={} 参数 格式如下图
后端java 代码 使用了spring boot 框架
package com.example.kmsk.control;
import cn.hutool.core.io.FastByteArrayOutputStream;
import cn.hutool.core.io.IoUtil;
import com.alibaba.fastjson2.JSONObject;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import java.io.*;
import java.util.*;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;
@Controller
@RestController
@CrossOrigin //跨域访问
public class control_download_file {
@RequestMapping("/download_file")
@ResponseBody
public ResponseEntity<byte[]> DownloadFile(@RequestBody String requestPrams) throws IOException {
JSONObject json_prams = JSONObject.parseObject(requestPrams);
String name = json_prams.getString("name"); // 打包后的文件名称 前端传过来的随机文件名 ,也可以后端生成
System.out.println(requestPrams);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
ZipOutputStream zipOutputStream = new ZipOutputStream(stream);
HttpHeaders headers = new HttpHeaders();
//设置文件名编码格式
headers.setContentDispositionFormData("attachment", new String(name.getBytes("UTF-8"),"ISO-8859-1"));
headers.setContentType(MediaType.APPLICATION_OCTET_STREAM);
ArrayList file_list_old = json_prams.getJSONArray("list"); //数组可能重复
//去重复
Set set = new HashSet<>(file_list_old);
ArrayList file_list = new ArrayList<>(set);//存入新数组
for ( int k=0;k<file_list.size();k++){
Map file_info = (Map) file_list.get(k);
String file_path = (String) file_info.get("path");
File file = new File(file_path); //读取文件
if (file.exists()){ //盘点文件是否存在
//转为字节流程
InputStream inputStream = new FileInputStream(file);
FastByteArrayOutputStream read = IoUtil.read(inputStream);
byte[] bytes = read.toByteArray();
//字节流 写入 zip 包
ZipEntry Ze = new ZipEntry(file.getName());
zipOutputStream.putNextEntry(Ze);
zipOutputStream.write(bytes);
inputStream.close();
}
}
zipOutputStream.closeEntry();
zipOutputStream.close();
return new ResponseEntity<byte[]>(stream.toByteArray(),headers, HttpStatus.CREATED);
}
}