将多张照片合成zip文件,并上传sftp服务器,具体代码如下:
SFTPClientUtils sftpClientUtils = new SFTPClientUtils();
sftpClientUtils.makeConnection();
InputStream phtotZip=this.doZipPhoto(list,sftpClientUtils);
if (phtotZip == null) {
throw new RuntimeException();
}else{
boolean falg = sftpClientUtils.upload(upload, photo, phtotZip, fileName);
}
sftpClientUtils.logout();
/**
* 创建压缩文件
* @author zhangyajuan
* @Date 2020年3月15日 下午2:49:09
* tag@param list
* tag@param zipFilePath
* tag@param id
* tag@return
*/
private InputStream doZipPhoto(List<RecommQueryPhotoVO> list,SFTPClientUtils sftpClientUtils) {
try {
ByteArrayOutputStream baoss = null;
baoss = new ByteArrayOutputStream();
ZipOutputStream zipOutputStream=new ZipOutputStream(baoss);
byte[] buf = new byte[8192];
int len;
for (int i = 0; i < list.size(); i++) {
RecommQueryPhotoVO vo=list.get(i);
String title = StringUtils.isBlank(vo.getProjectName()) ? " " : vo.getProjectName();//项目名称
int num=i+1;
String folderName = title + "_" + String.valueOf(num) ;//文件夹名称
ZipEntry zipEntry = new ZipEntry(folderName + "/");
zipOutputStream.putNextEntry(zipEntry);
List<CompletePersonVO> personList = vo.getList();
for (int j = 0; j < personList.size(); j++) {
CompletePersonVO completePersonVO=personList.get(j);
String name = completePersonVO.getName();
String allName = completePersonVO.getAllName();
String phototype=completePersonVO.getFileType();//获取照片类型
String rank=completePersonVO.getPersonRank();//排名(正常唯一不重复)
if(StringUtils.isBlank(rank)){
rank=UUIDUtils.getUUID();
}
String picName=folderName + "/" +UUIDUtils.getUUID()+ "."+phototype;
if (StringUtils.isNotBlank(allName) ) {
if(StringUtils.isNotBlank(name) ){
picName=folderName + "/" + name +"_"+rank+ "."+phototype;
}
InputStream download = sftpClientUtils.download(allName);
zipEntry = new ZipEntry(picName);
zipOutputStream.putNextEntry(zipEntry);
BufferedInputStream bis = new BufferedInputStream(download);
while ((len = bis.read(buf)) > 0) {
zipOutputStream.write(buf, 0, len);
}
bis.close();
zipOutputStream.closeEntry();
logger.info("doZipPhoto:" + "压缩完成");
}
}
}
zipOutputStream.closeEntry();
zipOutputStream.close();
byte[] byteArray = baoss.toByteArray();
InputStream bis = new ByteArrayInputStream(byteArray);
return bis;
} catch (Exception e) {
logger.error("doZipPhoto压缩文件失败 : " + e);
return null;
}
}