方法一:【成功】
将文件压缩为一个包再下载,期间出现一个问题是本地没问题,sit环境出现压缩文件解压后里面的文件名乱码,后来发现自己使用的是jdk1.8,而项目用的是jdk1.6。jdk1.6关于压缩文件有一个bug,在jdk1.7的时候才修复。
前端代码:
/**下载按钮事件*/
$("#downloadBtn").click(function(){
/*图片路径集合*/
var _list = [];
var i=-1;
$.each($('input:checkbox'),function(){
if(this.checked && $(this).val()!=null &&$(this).val()!='on'){
i=i+1
_list[i]=$(this).val();
}
});
/*传递路径至后台*/
if(_list.length!=null&&_list.length!=0){
submitPath(_list)
}else{
alert("请选择一条记录下载")
}
});
/**表单提交所有路径进行下载*/
function submitPath(list){
var form=$("<form>");
form.attr("style","display:none");
form.attr("target","_blank");
form.attr("method","post");
form.attr("action","downloadFile.html");
var input1=$("<input>");
input1.attr("type","hidden")
input1.attr("name","list")
input1.attr("value",list)
$("body").append(form);
form.append(input1);
form.submit();
}
后台代码:
/**
* 下载附件
*/
@ResponseBody
@RequestMapping(value = "downloadFile", method = { RequestMethod.POST, RequestMethod.GET })
public void downloadFile(@RequestParam("list")List<String> paths,QueryEntity entity, ModelMap model, HttpServletResponse response) throws Exception {
log.info("==========下载附件==========");
List<File> files = new ArrayList();
int allpaths = paths.size();
/*传递参数:附件所在路径是集合*/
//循环将路径传入下载方法
for (String path : paths) {
String filename = path.substring(0, path.indexOf(";"))+path.substring(path.indexOf("."), path.length()); //单个附件名称
// filename = new String(filename.getBytes("ISO-8859-1"), "UTF-8");
String imgName = path.substring(path.lastIndexOf("/")+1);//单独的图片路径
ChannelSftp channelSftp = null;
OutputStream os = null;
String localPathFile =this.localPathName + "/" + StringUtils.substringBeforeLast(imgName, ".");
try {
channelSftp = connectFTP();
//获取文件分隔符
String fileSeparator = System.getProperty("file.separator");
//获取加密后文件的相对路径 开发环境 "/root//home/ips/cc"
String srcPath = remotePathCc + "/" + StringUtils.substringBeforeLast(imgName, ".");
// String srcPath = "/root//home/ips/cc" + "/" + StringUtils.substringBeforeLast(imgName, ".");
File localPath = new File(localPathFile);
if(!localPath.exists()) {
localPath.mkdirs();
}
//目标路径
String destPath = this.localPathName + fileSeparator;
//密钥
String key = idCardInfoService.getKey();
//获取照片文件后缀名
String suffix = "." + StringUtils.substringAfter(imgName, ".");
//下载文件到服务器临时文件夹
FtpManageUtil.SftpDownloadEncryptFile(channelSftp, srcPath, localPathFile);
//解密合并图片
// IpsFileUtils.decryptMerge(localPathFile + fileSeparator, destPath, "", suffix, filename, key);
IpsFileUtils.decryptMerge(localPathFile + fileSeparator, destPath, "", suffix, imgName, key);
// rename(imgName,filename);
//读取图片输入流
// File file = new File(destPath+imgName);
File file = new File(destPath+imgName);
file.renameTo(new File(destPath+filename));
File newfile = new File(destPath+filename);
// files.add(file);
files.add(newfile);
log.info("<<<<<<"+"localPathFile{}"+localPathFile+" *fileSeparator{}"+fileSeparator+" *destPath{}"+destPath+" *suffix{}"+suffix+" *imgName{}"+imgName+" *key{}"+key+" *newfilename{}"+newfile.getName()+">>>>>>");
//只有一个文件就不压缩
if (allpaths<=1) {
response.setContentType("application/OCTET-STREAM");
// response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(imgName, "UTF-8"));
response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(filename, "UTF-8"));
//读取要下载的文件,保存到文件输入流
// FileInputStream in = new FileInputStream(destPath + imgName);
FileInputStream in = new FileInputStream(destPath + filename);
//创建输出流
OutputStream out = response.getOutputStream();
//创建缓冲区
byte buffer[] = new byte[1024];
int len = 0;
//循环将输入流中的内容读取到缓冲区当中
while((len=in.read(buffer))>0){
//输出缓冲区的内容到浏览器,实现文件下载
out.write(buffer, 0, len);
}
//关闭文件输入流
in.close();
//关闭输出流
out.close();
}
file.delete(); //删除合成后的文件
FileUtils.delAllFile(localPathFile); //删除解密前文件方式
} catch (Exception e) {
log.error("sftp下载异常", e.getMessage(), e);
// model.addAttribute(Constants._message, e.getMessage());
throw new BizException("sftp下载异常");
} finally {
if (null != os) {
try {
os.close();
} catch (IOException e) {
log.error("outputstream 关闭异常",e.getMessage(),e);
}
}
if (null != channelSftp) {
try {
if (null != channelSftp.getSession()) {
channelSftp.getSession().disconnect();
}
channelSftp.disconnect();
} catch (Exception e2) {
log.error("sftp关闭异常",e2.getMessage(),e2);
model.addAttribute(Constants._message, msa.getMessage(Errors.E999999));
}
}
}
/*下载ftp图片结束*/
}
log.info("文件list{}"+files);
if (allpaths>1&&files.size()!=0) {
this.downLoadFiles(files, response);
}
FileUtils.delAllFile(this.localPathName); //删除解密前文件方式
}
private static final String COMPLAINTEVENT_Details_URL = "/userlevel/complainManage/complainDetails";
@Autowired
private IdCardInfoService idCardInfoService;
@Value("${remote_Path}")
private String remotePathCc;// = "/root//home/ips/cc";// FTP服务器上的相对路径
@Value("${img_ftp_username}")
private String username; // FTP登录账号
@Value("${img_ftp_password}")
private String password; // FTP登录密码
@Value("${img_ftp_host}")
private String url;// FTP服务器hostname
@Value("${img_ftp_port}")
private int port;// FTP服务器端口
private ChannelSftp connectFTP() {
JSch jsch = new JSch();
ChannelSftp sftp = null;
try {
Session session = jsch.getSession(username, url, port);
session.setPassword(password);
Properties sshConfig = new Properties();
sshConfig.put("StrictHostKeyChecking", "no");
session.setConfig(sshConfig);
session.connect();
Channel channel = session.openChannel("sftp");
channel.connect();
sftp = (ChannelSftp)channel;
} catch (Exception e) {
log.error("连接ftp异常"+"[url:"+this.url+"|user:"+this.username+"]",e.getMessage(),e);
}
return sftp;
}
public HttpServletResponse downLoadFiles(List<File> files,HttpServletResponse response)throws Exception {
try {
//List<File> 作为参数传进来,就是把多个文件的路径放到一个list里面
//创建一个临时压缩文件
//临时文件可以放在CDEF盘中,但不建议这么做,因为需要先设置磁盘的访问权限,最好是放在服务器上,方法最后有删除临时文件的步骤
String zipFilename = this.localPathName+"运管客诉附件.zip" ;
File file = new File(zipFilename);
if (!file.exists()){
file.createNewFile();
}
response.reset();
//response.getWriter()
//创建文件输出流
/* FileOutputStream fous = new FileOutputStream(file);
ZipOutputStream zipOut = new ZipOutputStream(fous);
zipFile(files, zipOut);
zipOut.close();
fous.close();*///这个方法及下面的代码都不适用了
/*由于jdk1.6使用Java自带的压缩方法会出现文件名中文乱码,顾改成tomcat的压缩方法*/
FileCompressUtil.compressFiles2Zip(files, file, true);
response.reset();
return downloadZip(file,response);
}catch (Exception e) {
e.printStackTrace();
}
return response ;
}
/**
* 把接受的全部文件打成压缩包
* @param List<File>;
* @param org.apache.tools.zip.ZipOutputStream
*/
public static void zipFile (List files,ZipOutputStream outputStream) {
int size = files.size();
for(int i = 0; i < size; i++) {
File file = (File) files.get(i);
zipFile(file, outputStream);
}
}
/**
* 根据输入的文件与输出流对文件进行打包
* @param File
* @param org.apache.tools.zip.ZipOutputStream
*/
public static void zipFile(File inputFile, ZipOutputStream ouputStream) {
try {
if(inputFile.exists()) {
if (inputFile.isFile()) {
FileInputStream IN = new FileInputStream(inputFile);
BufferedInputStream bins = new BufferedInputStream(IN, 512);
ZipEntry entry = new ZipEntry(inputFile.getName());
ouputStream.putNextEntry(entry);
// 向压缩文件中输出数据
int nNumber;
byte[] buffer = new byte[512];
while ((nNumber = bins.read(buffer)) != -1) {
ouputStream.write(buffer, 0, nNumber);
}
// 关闭创建的流对象
bins.close();
IN.close();
} else {
try {
File[] files = inputFile.listFiles();
for (int i = 0; i < files.length; i++) {
zipFile(files[i], ouputStream);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
// public static HttpServletResponse downloadZip(File file,HttpServletResponse response) {
public static HttpServletResponse downloadZip(File file,HttpServletResponse response) {
if(file.exists() == false){
System.out.println("待压缩的文件目录:"+file+"不存在.");
}else{
try {
// 以流的形式下载文件。
InputStream fis = new BufferedInputStream(new FileInputStream(file.getPath()));
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
fis.close();
// 清空response
response.reset();
OutputStream toClient = new BufferedOutputStream(response.getOutputStream());
response.setContentType("application/octet-stream");
//如果输出的是中文名的文件,在此处就要用URLEncoder.encode方法进行处理
/* response.setHeader("Content-Disposition", "attachment;filename="
+ new String(file.getName().getBytes("GB2312"), "ISO8859-1"));*/
response.setHeader("Content-Disposition", "attachment;filename="
+ URLEncoder.encode(file.getName(), "UTF-8"));
toClient.write(buffer);
toClient.flush();
toClient.close();
} catch (IOException ex) {
ex.printStackTrace();
}finally{
try {
File f = new File(file.getPath());
f.delete();
} catch (Exception e) {
e.printStackTrace();
}
}
}
return response;
}
使用tomcat的压缩方法的类
/**
* Copyright 2017 by IPS. Floor 3,Universal Industrial Building,
* Tian Yaoqiao Road 1178,Shanghai, P.R. China,200300. All rights reserved.
*
* This software is the confidential and proprietary information of IPS
* ("Confidential Information"). You shall not disclose such
* Confidential Information and shall use it only in accordance with the terms
* of the license agreement you entered into with IPS.
*/
package com.ips.boms.utils;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import org.apache.commons.compress.archivers.ArchiveException;
import org.apache.commons.compress.archivers.ArchiveOutputStream;
import org.apache.commons.compress.archivers.ArchiveStreamFactory;
import org.apache.commons.compress.archivers.zip.Zip64Mode;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipArchiveOutputStream;
/**
*
*
* Create by tyshu 2019年3月5日 下午5:07:06
* @Copyright: 2019 IPS Inc. All rights reserved.
*
*/
public class FileCompressUtil {
/**
* 将文件打包成zip压缩包文件
*
* @param files 要压缩的文件
* @param zipFile 压缩后的文件
* @param deleteFileAfterZip 压缩文件后删除原来的文件,临时文件时记得删除
* @return 是否压缩成功
*/
public static boolean compressFiles2Zip(List<File> files, File zipFile, boolean deleteFileAfterZip) {
InputStream inputStream = null;
ZipArchiveOutputStream zipArchiveOutputStream = null;
try {
zipArchiveOutputStream = new ZipArchiveOutputStream(zipFile);
//Use Zip64 extensions for all entries where they are required
zipArchiveOutputStream.setUseZip64(Zip64Mode.AsNeeded);
for (File file : files) {
//将每个文件用ZipArchiveEntry封装,使用ZipArchiveOutputStream写到压缩文件
ZipArchiveEntry zipArchiveEntry = new ZipArchiveEntry(file, file.getName());
zipArchiveOutputStream.putArchiveEntry(zipArchiveEntry);
inputStream = new FileInputStream(file);
byte[] buffer = new byte[1024 * 5];
int len = -1;
while ((len = inputStream.read(buffer)) != -1) {
//把缓冲区的字节写入到ZipArchiveEntry
zipArchiveOutputStream.write(buffer, 0, len);
}
}
zipArchiveOutputStream.closeArchiveEntry();
zipArchiveOutputStream.finish();
if (deleteFileAfterZip) {
for (File file : files) {
file.deleteOnExit();
}
}
} catch (IOException e) {
e.printStackTrace();
return false;
} finally {
try {
//关闭输入流
if (null != inputStream) {
inputStream.close();
}
//关闭输出流
if (null != zipArchiveOutputStream) {
zipArchiveOutputStream.close();
}
} catch (IOException e) {
//e.printStackTrace();
}
}
return true;
}
public static void main(String[] args) {
}
}
需要引入一个包:
<!-- 在pom.xml中引入文件 -->
<!-- tomcat的压缩方法 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
<version>1.5</version>
</dependency>
方法二:【失败】
是没有用的,妥协使用压缩下载的方式,但还是记录一下。
需求:前端勾选附件,点击下载即下载被勾选的所有文件。
方法:采用前台循环请求后台的方式,下载被勾选的文件。请求一次下载一个文件。以下为前台代码,后台为普通的下载代码。
尝试:1.后台for循环调用下载方法【失败】因为response只能下载一次。
2.循环Ajax发送请求【失败】因为Ajax不能传io流。
3.循环使用 window.open()请求【失败】原因不详
4.循环使用from表单发送请求【失败】alert发现每次前台执行完for才网后台发送请求。
5.使用以下这个方法也失败【失败】但是要注意循环时要有间隔时间,虽然请求很快但还是还会需要时间,避免同时发送请求所以设置了间隔时间,因为不知道下载要多少时间所以每次下载下来的数据都被破坏打不开。
前端 js代码:
/**下载按钮事件*/
$("#downloadBtn").click(function(){
/*图片路径集合*/
var _list = [];
var i=-1;
$.each($('input:checkbox'),function(){
if(this.checked && $(this).val()!=null &&$(this).val()!='on'){
i=i+1
_list[i]=$(this).val();
}
});
/*传递路径至后台*/
if(_list.length!=null&&_list.length!=0){
//循环向后台发送请求
for(i=0;i<_list.length;i++){ //调用submitPath方法,向后台发送请求
setTimeout(submitPath(_list[i]), 1000);
}
}else{
alert("请选择一条记录下载")
}
});/**循环提交表单提交路径进行下载*/
function submitPath(path){
var a = document.createElement("a"), //创建a标签
e = document.createEvent("MouseEvents"); //创建鼠标事件对象
e.initEvent("click", false, false); //初始化事件对象
a.href = "downloadFile.html?path="+path; //设置下载地址
a.download = path.substr(path.lastIndexOf("/") + 1); //设置下载文件名
a.dispatchEvent(e); //给指定的元素,执行事件click事件
}
后台控制层代码:
/**
* 下载附件【该方法涉及sftp解密下载,】
*/
@ResponseBody
@RequestMapping(value = "downloadFile", method = { RequestMethod.POST, RequestMethod.GET })
public void downloadFile(String path,QueryEntity entity, ModelMap model, HttpServletResponse response) throws Exception {
log.info("==========下载附件{}"+path);
OutputStream out = response.getOutputStream();
/*传递参数:附件所在路径是集合*/
//循环将路径传入下载方法
String imgName = path.substring(path.lastIndexOf("/")+1);//单独的图片路径
ChannelSftp channelSftp = null;
OutputStream os = null;
try {
channelSftp = connectFTP();
//获取文件分隔符
String fileSeparator = System.getProperty("file.separator");
//获取加密后文件的相对路径
String srcPath = this.remotePath + "/" + StringUtils.substringBeforeLast(imgName, ".");
String localPathFile =this.localPathName + "/" + StringUtils.substringBeforeLast(imgName, ".");
File localPath = new File(localPathFile);
if(!localPath.exists()) {
localPath.mkdirs();
}
//目标路径
String destPath = this.localPathName + fileSeparator;
//密钥
String key = idCardInfoService.getKey();
//获取照片文件后缀名
String suffix = "." + StringUtils.substringAfter(imgName, ".");
log.info("<<<<<<"+"channelSftp:"+channelSftp+"?srcPath:"+srcPath+"?localPathFile:"+localPathFile+">>>>>>");
log.info("<<<<<<"+"localPathFile"+localPathFile+"?fileSeparator:"+fileSeparator+"?destPath:"+destPath+"?suffix:"+suffix+"?imgName:"+imgName+"?key:"+key+">>>>>>");
//下载文件到服务器临时文件夹
FtpManageUtil.SftpDownloadEncryptFile(channelSftp, srcPath, localPathFile);
//解密合并图片
IpsFileUtils.decryptMerge(localPathFile + fileSeparator, destPath, "", suffix, imgName, key);
//读取图片输入流
File file = new File(destPath+imgName);
response.setContentType("application/OCTET-STREAM");
response.setHeader("content-disposition", "attachment;filename=" + URLEncoder.encode(imgName, "UTF-8"));
log.debug("下载地址 {} " , destPath + imgName);
//读取要下载的文件,保存到文件输入流
FileInputStream in = new FileInputStream(destPath + imgName);
// in = new FileInputStream(destPath + imgName);
//创建输出流
// OutputStream out = response.getOutputStream();
//创建缓冲区
byte buffer[] = new byte[1024];
int len = 0;
//循环将输入流中的内容读取到缓冲区当中
while((len=in.read(buffer))>0){
//输出缓冲区的内容到浏览器,实现文件下载
out.write(buffer, 0, len);
}
//关闭文件输入流
in.close();
out.close();
file.delete();
//删除多余文件
FileUtils.delAllFile(localPathFile); //删除解密前文件方式
} catch (Exception e) {
log.error("sftp下载异常", e.getMessage(), e);
} finally {
if (null != os) {
try {
os.close();
} catch (IOException e) {
log.error("outputstream 关闭异常",e.getMessage(),e);
}
}
if (null != channelSftp) {
try {
if (null != channelSftp.getSession()) {
channelSftp.getSession().disconnect();
}
channelSftp.disconnect();
} catch (Exception e2) {
log.error("sftp关闭异常",e2.getMessage(),e2);
model.addAttribute(Constants._message, msa.getMessage(Errors.E999999));
}
}
} /*下载ftp图片结束*/
}
private static final String COMPLAINTEVENT_Details_URL = "/userlevel/complainManage/complainDetails";
@Autowired
private IdCardInfoService idCardInfoService;
// @Value("${remote_Path}")
private String remotePath = "/root//home/ips/cc";// FTP服务器上的相对路径
@Value("${img_ftp_username}")
private String username; // FTP登录账号
@Value("${img_ftp_password}")
private String password; // FTP登录密码
@Value("${img_ftp_host}")
private String url;// FTP服务器hostname
@Value("${img_ftp_port}")
private int port;// FTP服务器端口
private ChannelSftp connectFTP() {
JSch jsch = new JSch();
ChannelSftp sftp = null;
try {
Session session = jsch.getSession(username, url, port);
session.setPassword(password);
Properties sshConfig = new Properties();
sshConfig.put("StrictHostKeyChecking", "no");
session.setConfig(sshConfig);
session.connect();
Channel channel = session.openChannel("sftp");
channel.connect();
sftp = (ChannelSftp)channel;
} catch (Exception e) {
log.error("连接ftp异常"+"[url:"+this.url+"|user:"+this.username+"]",e.getMessage(),e);
}
return sftp;
}