/**
*
* @Title downFileByZip
* @Description 文件批量下载(zip格式)
* @param list 集合中为map,map中有两个参数<br/>1:filePath文件路径,格式path1/path2/path3;<br/>2:fileName文件名字,格式file1,file2,file3
* @throws Exception
* @return void
* @author LQ
* @date 2015年11月14日
*/
public static void downFileByZip(List<Map<String, String>> list) throws SBRSysRuntimeException{
//生成的ZIP文件名
String tmpFileName = DateUtil.dateToString(new Date(), "yyyyMMddhhmmss")+".zip";
byte[] buffer = new byte[1024];
/*List<Map<String, String>> list = new ArrayList<Map<String,String>>();
Map<String, String> map=new HashMap<String, String>();
map.put("filePath", "2015年任务/检查小组1/北京分行/信息管理系统");
map.put("fileName", "test1.txt,测试2.txt");
list.add(map);
Map<String, String> map1=new HashMap<String, String>();
map1.put("filePath", "2015年任务/检查小组1/山东分行/OA系统");
map1.put("fileName", "test1.txt,测试2.txt");
list.add(map1);
Map<String, String> map2=new HashMap<String, String>();
map2.put("filePath", "2015年任务/检查小组2/河北分行/质检信息安全工作平台");
map2.put("fileName", "test1.txt,测试2.txt");
list.add(map2);*/
String strZipPath = FilePath + File.separator + tmpFileName;
ZipOutputStream out=null;
FileInputStream fis=null;
try {
HttpServletResponse response=getResponse();
//设置压缩文件内的字符编码,不然会变成乱码
response.setCharacterEncoding("GBK");
File zipPath=new File(FilePath);
if(!zipPath.exists()){
zipPath.mkdirs();
}
File file=new File(strZipPath);
if(!file.exists()){
file.createNewFile();
}
out = new ZipOutputStream(new FileOutputStream(strZipPath));
//处理文件,将文件打包到zip文件中,并且添加对应路径
if(list!=null && list.size()>0){
for(Map<String, String> fileMap:list){
//如果文件名字为空,不处理文件
if(fileMap.get("fileName")==null || "".equals(fileMap.get("fileName"))){
continue;
}
String fileStr=fileMap.get("fileName");//获取文件名字
String files[]=fileStr.split(",");
File[] file1=new File[files.length];//得到需要打包的文件
for (int i = 0; i < file1.length; i++) {
File f=new File(FilePath+File.separator+files[i]);
if(!f.exists()){
LOGGER.warn("打包下载未检索到文件,文件路径【"+FilePath+File.separator+files[i]+"】,打包文件路径【"+strZipPath+"】,此文件可能已被删除");
continue;
}
fis = new FileInputStream(new File(FilePath+File.separator+files[i]));//将文件读取到输入流中
String outPath= fileMap.get("filePath")==null||"".equals(fileMap.get("filePath"))?"":fileMap.get("filePath");
/*if(!"".equals(outPath)){
outPath=new String(outPath.getBytes("gbk"), "ISO8859-1");
}*/
String outName =files[i];// new String(files[i].getBytes("gbk"), "ISO8859-1");
out.putNextEntry(new ZipEntry(((outPath==null || "".equals(outPath))?"":(outPath+File.separator))+outName));//将文件放入到压缩文件中(带文件路径)
int len;
// 读入需要下载的文件的内容,打包到zip文件
while ((len = fis.read(buffer)) > 0) {
out.write(buffer, 0, len);//将文件输出
}
out.flush();
}
}
}else{
LOGGER.error("未检索到文件信息,请确认下载文件是否正确!");
throw new SBRSysRuntimeException("未检索到文件信息,请确认下载文件是否正确!");
}
} catch (Exception e) {
LOGGER.error("打包下载文件异常,错误原因:",e);
throw new SBRSysRuntimeException("打包下载文件异常!");
} finally{
try {
if(out!=null){
out.closeEntry();
out.close();
}
if(fis!=null){
fis.close();
}
} catch (Exception e2) {
LOGGER.error("关闭输入输出流异常,错误原因:",e2);
throw new SBRSysRuntimeException("关闭流异常,请联系管理员!");
}
}
downFile(getResponse(), tmpFileName);
}
/**
*
* @Title downFile
* @Description 文件下载
* @param response
* @param fileName 文件名字
* @throws Exception
* @return void
* @author LQ
* @date 2015年11月14日
*/
public static void downFile(HttpServletResponse response, String fileName) throws SBRSysRuntimeException{
InputStream ins=null;
BufferedInputStream bins=null;
OutputStream out=null;
BufferedOutputStream bouts=null;
try {
response.setCharacterEncoding("GBK");
String path = FilePath + File.separator + fileName;
File file = new File(path);
if (file.exists()) {
ins = new FileInputStream(path);
bins = new BufferedInputStream(ins);// 放到缓冲流里面
out = response.getOutputStream();// 获取文件输出IO流
bouts = new BufferedOutputStream(out);
response.setContentType("application/x-download");// 设置response内容的类型
response.setHeader("Content-disposition","attachment;filename="+ URLEncoder.encode(fileName, "UTF-8"));// 设置头部信息
int bytesRead = 0;
byte[] buffer = new byte[8192];
// 开始向网络传输文件流
while ((bytesRead = bins.read(buffer, 0, 8192)) != -1) {
bouts.write(buffer, 0, bytesRead);//输出文件
}
bouts.flush();// 这里一定要调用flush()方法
} else {
throw new SBRSysRuntimeException("未检索到需要下载的文件,请检查文件路径是否正确");
}
}catch(Exception e){
LOGGER.error("文件下载异常,错误原因:", e);
throw new SBRSysRuntimeException("文件下载异常,请联系管理员!");
}finally{
try {
if(ins!=null){
ins.close();
}
if(bins!=null){
bins.close();
}
if(out!=null){
out.close();
}
if(bouts!=null){
bouts.close();
}
} catch (Exception e2) {
LOGGER.error("关闭输入输出流异常,错误原因:",e2);
throw new SBRSysRuntimeException("关闭流异常,请联系管理员!");
}
}
}
*
* @Title downFileByZip
* @Description 文件批量下载(zip格式)
* @param list 集合中为map,map中有两个参数<br/>1:filePath文件路径,格式path1/path2/path3;<br/>2:fileName文件名字,格式file1,file2,file3
* @throws Exception
* @return void
* @author LQ
* @date 2015年11月14日
*/
public static void downFileByZip(List<Map<String, String>> list) throws SBRSysRuntimeException{
//生成的ZIP文件名
String tmpFileName = DateUtil.dateToString(new Date(), "yyyyMMddhhmmss")+".zip";
byte[] buffer = new byte[1024];
/*List<Map<String, String>> list = new ArrayList<Map<String,String>>();
Map<String, String> map=new HashMap<String, String>();
map.put("filePath", "2015年任务/检查小组1/北京分行/信息管理系统");
map.put("fileName", "test1.txt,测试2.txt");
list.add(map);
Map<String, String> map1=new HashMap<String, String>();
map1.put("filePath", "2015年任务/检查小组1/山东分行/OA系统");
map1.put("fileName", "test1.txt,测试2.txt");
list.add(map1);
Map<String, String> map2=new HashMap<String, String>();
map2.put("filePath", "2015年任务/检查小组2/河北分行/质检信息安全工作平台");
map2.put("fileName", "test1.txt,测试2.txt");
list.add(map2);*/
String strZipPath = FilePath + File.separator + tmpFileName;
ZipOutputStream out=null;
FileInputStream fis=null;
try {
HttpServletResponse response=getResponse();
//设置压缩文件内的字符编码,不然会变成乱码
response.setCharacterEncoding("GBK");
File zipPath=new File(FilePath);
if(!zipPath.exists()){
zipPath.mkdirs();
}
File file=new File(strZipPath);
if(!file.exists()){
file.createNewFile();
}
out = new ZipOutputStream(new FileOutputStream(strZipPath));
//处理文件,将文件打包到zip文件中,并且添加对应路径
if(list!=null && list.size()>0){
for(Map<String, String> fileMap:list){
//如果文件名字为空,不处理文件
if(fileMap.get("fileName")==null || "".equals(fileMap.get("fileName"))){
continue;
}
String fileStr=fileMap.get("fileName");//获取文件名字
String files[]=fileStr.split(",");
File[] file1=new File[files.length];//得到需要打包的文件
for (int i = 0; i < file1.length; i++) {
File f=new File(FilePath+File.separator+files[i]);
if(!f.exists()){
LOGGER.warn("打包下载未检索到文件,文件路径【"+FilePath+File.separator+files[i]+"】,打包文件路径【"+strZipPath+"】,此文件可能已被删除");
continue;
}
fis = new FileInputStream(new File(FilePath+File.separator+files[i]));//将文件读取到输入流中
String outPath= fileMap.get("filePath")==null||"".equals(fileMap.get("filePath"))?"":fileMap.get("filePath");
/*if(!"".equals(outPath)){
outPath=new String(outPath.getBytes("gbk"), "ISO8859-1");
}*/
String outName =files[i];// new String(files[i].getBytes("gbk"), "ISO8859-1");
out.putNextEntry(new ZipEntry(((outPath==null || "".equals(outPath))?"":(outPath+File.separator))+outName));//将文件放入到压缩文件中(带文件路径)
int len;
// 读入需要下载的文件的内容,打包到zip文件
while ((len = fis.read(buffer)) > 0) {
out.write(buffer, 0, len);//将文件输出
}
out.flush();
}
}
}else{
LOGGER.error("未检索到文件信息,请确认下载文件是否正确!");
throw new SBRSysRuntimeException("未检索到文件信息,请确认下载文件是否正确!");
}
} catch (Exception e) {
LOGGER.error("打包下载文件异常,错误原因:",e);
throw new SBRSysRuntimeException("打包下载文件异常!");
} finally{
try {
if(out!=null){
out.closeEntry();
out.close();
}
if(fis!=null){
fis.close();
}
} catch (Exception e2) {
LOGGER.error("关闭输入输出流异常,错误原因:",e2);
throw new SBRSysRuntimeException("关闭流异常,请联系管理员!");
}
}
downFile(getResponse(), tmpFileName);
}
/**
*
* @Title downFile
* @Description 文件下载
* @param response
* @param fileName 文件名字
* @throws Exception
* @return void
* @author LQ
* @date 2015年11月14日
*/
public static void downFile(HttpServletResponse response, String fileName) throws SBRSysRuntimeException{
InputStream ins=null;
BufferedInputStream bins=null;
OutputStream out=null;
BufferedOutputStream bouts=null;
try {
response.setCharacterEncoding("GBK");
String path = FilePath + File.separator + fileName;
File file = new File(path);
if (file.exists()) {
ins = new FileInputStream(path);
bins = new BufferedInputStream(ins);// 放到缓冲流里面
out = response.getOutputStream();// 获取文件输出IO流
bouts = new BufferedOutputStream(out);
response.setContentType("application/x-download");// 设置response内容的类型
response.setHeader("Content-disposition","attachment;filename="+ URLEncoder.encode(fileName, "UTF-8"));// 设置头部信息
int bytesRead = 0;
byte[] buffer = new byte[8192];
// 开始向网络传输文件流
while ((bytesRead = bins.read(buffer, 0, 8192)) != -1) {
bouts.write(buffer, 0, bytesRead);//输出文件
}
bouts.flush();// 这里一定要调用flush()方法
} else {
throw new SBRSysRuntimeException("未检索到需要下载的文件,请检查文件路径是否正确");
}
}catch(Exception e){
LOGGER.error("文件下载异常,错误原因:", e);
throw new SBRSysRuntimeException("文件下载异常,请联系管理员!");
}finally{
try {
if(ins!=null){
ins.close();
}
if(bins!=null){
bins.close();
}
if(out!=null){
out.close();
}
if(bouts!=null){
bouts.close();
}
} catch (Exception e2) {
LOGGER.error("关闭输入输出流异常,错误原因:",e2);
throw new SBRSysRuntimeException("关闭流异常,请联系管理员!");
}
}
}