Java浏览器弹出下载框,多个文件导出压缩包

时间:2023-03-08 22:30:13

项目里一直有这个功能,也一直没怎么注意,今天研究了一下

依据逻辑往下走:

首先是要下载的ajax的Java方法,只有返回值需要设定一下,其他的不用管:

     Map<String, Object> resultMap = pdfService.createPDF(dest,jsonObject);
//文件保存路径,不包含文件名,例:d:\\aaa\\uploadFileRoot\\
resultMap.put("filePath", savePath);
//文件名
resultMap.put("fileName", fileName); return JSON.toJSONString(resultMap);

然后前端接收返回值:

            res = JSON.stringify(res)
var data = JSON.parse(res);
// 本地 将路径中的\\用字母z代替,需要注意,如果下面open里的方法有z就需要换字母,换成没有的,不然后面解析会出错
var a = data.filePath.replace(/\\/g, "z");
// 发布
// var a = data1.a.replace(/\//g, "z");
window
.open("/pdf/downLoadTemp.do?a="
+ a + "&b=" + data.fileName);

把这个代码单独放到一个方法中方便重用(如果中间文件不需要保存,可以在finally中删除这个文件):

@RequestMapping("downLoadTemp.do")
public void downLoadTemp(HttpServletRequest req,HttpServletResponse res,String a,String b) throws Exception {   
String filePath = a.replace("z", downLoadTemp);// downLoadTemp如果项目在本地是\\,服务器是/ String fileName = b;// 保存窗口中显示的文件名
res.setContentType("APPLICATION/OCTET-STREAM"); /*
* 要显示到客户端的文件名转码是必需的,特别是中文名 否则可能出现文件名乱码甚至是浏览器显示无法下载的问题
*/
ServletOutputStream out = null;
// PrintWriter out = null;
InputStream inStream = null;
try {
fileName = res.encodeURL(new String(fileName.getBytes("UTF-8"), "ISO8859_1"));// res.setHeader("Content-Disposition", "attachment; filename=\"" + fileName + "\"");
// inline
out = res.getOutputStream();
System.out.println(
filePath + toUtf8String(fileName) + "..........................................................");
inStream = new FileInputStream(filePath + toUtf8String(fileName)); // 下载需要加密
// DrmEdiClient.getInstance().encryptAuthFile(inStream,out,
// ?,?,?); // 循环取出流中的数据
byte[] by = new byte[1024];
int len;
while ((len = inStream.read(by)) > 0)
out.write(by, 0, len);
res.setStatus(res.SC_OK);
res.flushBuffer(); } catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (RuntimeException e) {
e.printStackTrace();
} finally {
if (out != null)
out.close();
inStream.close();
}
}

既然写了这方面的,那就把多个文件打包下载也写了把:(fileInfo为我们公司的存储文件属性的类),这是我之前写的代码了,过去好久都忘了

  if (list != null && list.size() > 0) {
        
Map<String, String> temp = new HashMap<String, String>();
       //要下载的文件的集合
File[] fileArray = new File[list.size()];
Map<String, String> newName = new HashMap<String, String>();
for (int i = 0; i < list.size(); i++) {
FileInfo fi = list.get(i);
String deskName = new File(fi.getFilePath()).getName();
String fileName = fi.getFileName();
String fn = temp.get(fileName);
if (fn != null) {// 打包下载版本文件保证文件不同名
String uploadDate = DateUtil.format(fi.getUploadTime(),
"yyyyMMddHHmmss");
fileName = uploadDate + fileName;
if (i == 1) {
FileInfo _fi = list.get(0);
String _deskName = new File(_fi.getFilePath())
.getName();
String _fileName = _fi.getFileName();
String _uploadDate = DateUtil.format(
_fi.getUploadTime(), "yyyyMMddHHmmss");
_fileName = _uploadDate + _fileName;
newName.put(_deskName, _fileName);
}
}
temp.put(fileName, "");
newName.put(deskName, fileName);
File f = new File(fi.getFilePath());
fileArray[i] = f;
}
File zipFile = new File(new Date().getTime() + ".zip");
        //ZipUtil为公司封装的zip下载工具类
ZipUtil.zip(fileArray, zipFile, newName); String pathSaveFile = zipFile.getAbsolutePath();// 要下载的文件
String fileName = "files.zip";// 保存窗口中显示的文件名
super.getResponse().reset();
super.getResponse().setContentType("APPLICATION/OCTET-STREAM"); /*
* 要显示到客户端的文件名转码是必需的,特别是中文名 否则可能出现文件名乱码甚至是浏览器显示无法下载的问题
*/
ServletOutputStream out = null;
InputStream inStream = null;
try {
fileName = super.getResponse().encodeURL(
new String(fileName.getBytes("GBK"), "ISO8859_1"));// super.getResponse().setHeader("Content-Disposition",
"attachment; filename=\"" + fileName + "\"");
// inline
out = super.getResponse().getOutputStream();
inStream = new FileInputStream(pathSaveFile); // 循环取出流中的数据
byte[] b = new byte[1024];
int len;
while ((len = inStream.read(b)) > 0)
out.write(b, 0, len);
super.getResponse().setStatus(super.getResponse().SC_OK);
super.getResponse().flushBuffer();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (out != null)
out.close();
if (inStream != null)
inStream.close();
zipFile.delete();
}
}
ZipUtil.zip的方法:
public static void zip(File[] files, File out, Map<String, String> newName)
{
if (files != null) {
Map<String, File> map = new HashMap();
for (File f : files) {
list(f, null, map);
}
if (!map.isEmpty()) {
try {
ZipArchiveOutputStream zaos = new ZipArchiveOutputStream(out);
for (Map.Entry<String, File> entry : map.entrySet()) {
File file = (File)entry.getValue();
String name = null;
if ((newName != null) && (newName.size() > 0)) {
String deskName = file.getName();
name = (String)newName.get(deskName);
}
name = name == null ? (String)entry.getKey() : name;
ZipArchiveEntry zae = new ZipArchiveEntry(file, name);
zaos.putArchiveEntry(zae);
InputStream is = new FileInputStream(file);
byte[] b = new byte['?'];
int i = -1;
while ((i = is.read(b)) != -1) {
zaos.write(b, 0, i);
}
is.close();
zaos.closeArchiveEntry();
}
zaos.finish();
zaos.close();
} catch (IOException ex) {
log.error(ex.getMessage(), ex);
}
}
}
}
public static String toUtf8String(String s) {
StringBuffer sb = new StringBuffer();
for (int i = ; i < s.length(); i++) {
char c = s.charAt(i);
if (c >= && c <= ) {
sb.append(c);
} else {
byte[] b;
try {
b = Character.toString(c).getBytes("utf-8");
} catch (Exception ex) {
System.out.println(ex);
b = new byte[];
}
for (int j = ; j < b.length; j++) {
int k = b[j];
if (k < )
k += ;
sb.append("%" + Integer.toHexString(k).toUpperCase());
}
}
}
return sb.toString();
}