下载压缩文件并打包

public String downloadjsglfj(){
String id = getServletRequest().getParameter("id");
String jsmc = getServletRequest().getParameter("jsmc");
List<FjxxbVO> list=fjxxService.getJsglFjByFId(id,"jsglfj");
if(list.size()>0){
try {
fileName= jsmc+".zip";
this.inputStream=SftpUtils.batchDownload(list);
} catch (Exception e) {
e.printStackTrace();
}
}else{
return null;
}


return SUCCESS;
}

--------------------------

public static InputStream batchDownload(List<FjxxbVO> list){
ByteArrayOutputStream bos=new ByteArrayOutputStream();
try{
sftp = connect(ftpServer, ftpPort, ftpAccount, ftpPassword);
ZipOutputStream out = new ZipOutputStream(bos);
byte[] buf = new byte[4096];
for (FjxxbVO fjxxbVO : list) {
InputStream in = null;
try {

in=sftp.get(fjxxbVO.getLj()+"/"+fjxxbVO.getSsmc());

out.putNextEntry(new ZipEntry(fjxxbVO.getOrgname()+"."+fjxxbVO.getWjlx()));

int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
out.closeEntry();
in.close();


} catch (Exception e) {
e.printStackTrace();
}

}

//inputStream=sftp.get(fileDir);
out.close();
bos.close();

} catch (FileNotFoundException e) {
} catch (Exception e) {
e.printStackTrace();
}

return new ByteArrayInputStream(bos.toByteArray());

}

原文地址:https://www.cnblogs.com/wanghongwei123/p/7154308.html