多文件打包下载
/**
* 下载
* @param response
* info 文件夹名
* filenames 文件名们
*/
@RequestMapping(value = "/imgdownload", method = RequestMethod.GET)
public void imgDownload(GeneratorPojo generatorPojo, String info, HttpServletResponse response) throws Exception {
generatorPojo.setSpecificPath("F:\\generotar\\jinbeijie-customer\\src\\main\\java\\com\\heyi\\jinbeijie\\platform\\");
String filenames="";
if(generatorPojo.getFileName() ==null || generatorPojo.getFileName() ==""){
List<String> list =generatorService.getAllCode(generatorPojo.getFolderName(),generatorPojo.getSpecificPath());
for(String l:list){
if(filenames==""){
filenames=l;
}else{
filenames=filenames+","+l;
}
}
}else{
filenames=generatorPojo.getFileName();
}
String[] names=filenames.split(",");
String filepath=generatorPojo.getSpecificPath()+generatorPojo.getFolderName();
//存放--服务器上zip文件的目录
String directory = "D:\\repository\\"+generatorPojo.getFolderName();
File directoryFile=new File(directory);
if(!directoryFile.isDirectory() && !directoryFile.exists()){
directoryFile.mkdirs();
}
//设置最终输出zip文件的目录+文件名
String zipFileName = generatorPojo.getFolderName()+".zip";
String strZipPath = directory+"\\"+zipFileName;
ZipOutputStream zipStream = null;
FileInputStream zipSource = null;
BufferedInputStream bufferStream = null;
File zipFile = new File(strZipPath);
try{
//构造最终压缩包的输出流
zipStream = new ZipOutputStream(new FileOutputStream(zipFile));
for (int i = 0; i<names.length ;i++){
//解码获取真实路径与文件名
String realFileName = java.net.URLDecoder.decode(names[i],"UTF-8");
String realFilePath = java.net.URLDecoder.decode(filepath+"\\"+ names[i],"UTF-8");
File file = new File(realFilePath);
//TODO:未对文件不存在时进行操作,后期优化。
System.out.println(realFilePath);
System.out.println(file);
if(file.exists()){
zipSource = new FileInputStream(file);//将需要压缩的文件格式化为输入流
/**
* 压缩条目不是具体独立的文件,而是压缩包文件列表中的列表项,称为条目,就像索引一样这里的name就是文件名,
* 文件名和之前的重复就会导致文件被覆盖
*/
ZipEntry zipEntry = new ZipEntry(realFileName);//在压缩目录中文件的名字
zipStream.putNextEntry(zipEntry);//定位该压缩条目位置,开始写入文件到压缩包中
bufferStream = new BufferedInputStream(zipSource, 1024 * 10);
int read = 0;
byte[] buf = new byte[1024 * 10];
while((read = bufferStream.read(buf, 0, 1024 * 10)) != -1)
{
zipStream.write(buf, 0, read);
}
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
//关闭流
try {
if(null != bufferStream)
{bufferStream.close();}
if(null != zipStream){
zipStream.flush();
zipStream.close();
}
if(null != zipSource)
{zipSource.close();}
} catch (IOException e) {
e.printStackTrace();
}
}
//判断系统压缩文件是否存在:true-把该压缩文件通过流输出给客户端后删除该压缩文件 false-未处理
if(zipFile.exists()){
downImg(response,zipFileName,strZipPath);
zipFile.delete();
}
}
public void downImg(HttpServletResponse response,String filename,String path ) {
if (filename != null) {
FileInputStream is = null;
BufferedInputStream bs = null;
OutputStream os = null;
try {
File file = new File(path);
if (file.exists()) {
//设置Headers
response.setHeader("Content-Type", "application/octet-stream");
//设置下载的文件的名称-该方式已解决中文乱码问题
response.setHeader("Content-Disposition", "attachment;filename=" + new String(filename.getBytes("gb2312"), "ISO8859-1"));
is = new FileInputStream(file);
bs = new BufferedInputStream(is);
os = response.getOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while ((len = bs.read(buffer)) != -1) {
os.write(buffer, 0, len);
}
} else {
response.sendRedirect("/imgUpload/imgList?error=" + "\"下载的文件资源不存在\"");
}
} catch (IOException ex) {
ex.printStackTrace();
} finally {
try {
if (is != null) {
is.close();
}
if (bs != null) {
bs.close();
}
if (os != null) {
os.flush();
os.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
posted on 2019-06-28 11:34 superficial。 阅读(672) 评论(0) 收藏 举报
浙公网安备 33010602011771号