-------------------------------------------------后端-------------------------------------------------
/**
* 图片下载
*/
@RequestMapping("/getTestinfoPhoto")
// public @VoidResponseBody void exportOutUserInfo(HttpServletResponse response, HttpServletRequest request)
// throws IOException {
// //接收前台传来的参数
// String paths = request.getParameter("paths");
// //参数格式是文件地址+;;+文件名称
// //将参数拆分成地址+文件名
// String str[] = paths.split(";;");
// String filepath="";
// String fileName="";
// //将文件名拆分出来
// String photoName[] = str[1].split(";");
// // 清空response
// response.reset();
// for(int i = 0; i<photoName.length ;i++){
// filepath = str[0];//文件地址
// fileName = photoName[i];//文件名
// String savePath = request.getSession().getServletContext().getRealPath("/");
// //获取项目的根目录因为tomcat和weblogic获取的根目录不一致,所以需要此方法
// if(savePath == null){
// savePath = this.getClass().getClassLoader().getResource("/").getPath();
// }
// String path = savePath+ File.separator +"mxuploadpath" + File.separator + "T_GQJ_TESTINFO" +
// File.separator + str[0] + File.separator + "attachment" + File.separator + fileName;
//
// // 以流的形式下载文件。
// InputStream fis = new BufferedInputStream(new FileInputStream(path));
//
// // 设置response的Header
// response.addHeader("Content-Disposition",
// "attachment;filename=" + new String(fileName.getBytes("GBK"), "iso8859-1"));
// response.setContentType("application/x-download; charset=GBK");
// OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
// int len = 0;
// byte[] buffer = new byte[1048576];
// while ((len = fis.read(buffer, 0, buffer.length)) != -1) {
// toClient.write(buffer, 0, len);
// }
// try {
// fis.close();
// } catch (Exception e1) {
// e1.printStackTrace();
// }
// try {
// toClient.flush();
// toClient.close();
// } catch (Exception e) {
// e.printStackTrace();
// }
//
// }
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
/**
* 图片下载
*/
@RequestMapping("/getTestinfoPhoto")
public @VoidResponseBody void exportOutUserInfo(HttpServletResponse response, HttpServletRequest request)
throws IOException {
//接收前台传来的参数
String paths = request.getParameter("paths");
//参数格式是文件地址+;;+文件名称
//将参数拆分成地址+文件名
String str[] = paths.split(";;");
String filepath="";
String fileName="";
//将文件名拆分出来
String photoName[] = str[1].split(";");
Map<String, InputStream> isMap = new HashMap();
String savePath = request.getSession().getServletContext().getRealPath("/");
//获取项目的根目录因为tomcat和weblogic获取的根目录不一致,所以需要此方法
if(savePath == null){
savePath = this.getClass().getClassLoader().getResource("/").getPath();
}
//遍历将文件流和文件名放入map
for(int i = 0; i<photoName.length ;i++){
filepath = str[0];//文件地址
fileName = photoName[i];//文件名
String path = savePath+ File.separator +"mxuploadpath" + File.separator + "T_GQJ_TESTINFO" +
File.separator + str[0] + File.separator + "attachment" + File.separator + fileName;
InputStream fis = new BufferedInputStream(new FileInputStream(path));
isMap.put(fileName, fis);
}
response.setContentType("application/octet-stream");
response.setHeader("Content-Disposition", "attachment; filename=Document.zip");
ZipOutputStream zipOut = new ZipOutputStream(response.getOutputStream());
//遍历map打包
for (String key : isMap.keySet()){
addToZip(isMap.get(key), zipOut, key);
}
zipOut.flush();
zipOut.close();
}
/*添加压缩文件*/
private void addToZip(InputStream is, ZipOutputStream zipOut, String fileName) {
try{
ZipEntry entry = new ZipEntry(fileName);
zipOut.putNextEntry(entry);
int len;
byte[] buffer = new byte[1024];
while ((len = is.read(buffer)) > 0) {
zipOut.write(buffer, 0, len);
}
zipOut.closeEntry();
is.close();
}catch (Exception e){
e.printStackTrace();
}
}
-------------------------------------------------前端-------------------------------------------------
window.open(gqjtest.mappath("~/rest/tgqjtestinfo/getTestinfoPhoto?paths="+paths+""),"_parent");