业务需要将ftp文件压缩加密并且转存,先读取ftp源文件再写入临时目录,然后在临时文件中加密压缩,再通过ftp上传,清除临时目录中的文件
java
public String getPdfByFtpUrl(RecTypeDictStorageEntity storageEntity,ViewArchiveDocumentsEntity viewArchiveDocuments) {
ByteArrayOutputStream fileData=new ByteArrayOutputStream();
FtpUtil ftpUtil = new FtpUtil();
String ftpConfig = "ftp://USER:PWD@IP:PORT";
ftpConfig = ftpConfig.replace("IP", storageEntity.getFtpIp())
.replace("USER", storageEntity.getFtpUser())
.replace("PWD", storageEntity.getFtpPwd())
.replace("PORT", storageEntity.getFtpPort());
ftpUtil.getConn(ftpConfig);
String remoteFile = viewArchiveDocuments.getDocfile().substring(0, viewArchiveDocuments.getDocfile().lastIndexOf("/") + 1);
String fileName = viewArchiveDocuments.getDocfile().substring(viewArchiveDocuments.getDocfile().lastIndexOf("/") + 1);
try {
boolean result = ftpUtil.fileExist(remoteFile, fileName);
if (result) {
fileData = ftpUtil.fileGet(remoteFile, fileName);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
ftpUtil.ftpDisconnect();
}
if (fileData!=null) {
try {
byte[] fileArray=fileData.toByteArray();
ByteArrayInputStream inputStream = new ByteArrayInputStream(fileArray);
return Base64.getEncoder().encodeToString(IOUtils.toByteArray(inputStream));
} catch (Exception ex) {
logger.info(ex.getMessage());
return "";
}
}
logger.info("找不到文件,获取不了文件信息");
return "";
}
java byte[] bytes = Base64.getDecoder().decode(base64Str);
改成
javabyte[] bytes = Base64.getMimeDecoder().decode(base64Str);
hanlp官网,HanLP 是由一系列模型与算法组成的工具包,目标是普及自然语言处理在生产环境中的应用。HanLP 具备功能完善、性能高效、架构清晰、语料时新、可自定义的特点。
HanLP 主要功能包括分词、词性标注、关键词提取、自动摘要、依存句法分析、命名实体识别、短语提取、拼音转换、简繁转换等等。
xml
<dependency>
<groupId>com.hankcs</groupId>
<artifactId>hanlp</artifactId>
<version>portable-1.7.3</version>
</dependency>
目的:要将ftp上的文件转存在另外的ftp地址,我们拆分成两步,先将文件输入流转为base64,再将base64字符串转成pdf存储在ftp上
java
public String getPdfByFtpUrl(RecTypeDictStorageEntity storageEntity,ViewArchiveDocumentsEntity viewArchiveDocuments) {
ByteArrayOutputStream fileData=new ByteArrayOutputStream();
FtpUtil ftpUtil = new FtpUtil();
String ftpConfig = "ftp://USER:PWD@IP:PORT";
ftpConfig = ftpConfig.replace("IP", storageEntity.getFtpIp())
.replace("USER", storageEntity.getFtpUser())
.replace("PWD", storageEntity.getFtpPwd())
.replace("PORT", storageEntity.getFtpPort());
ftpUtil.getConn(ftpConfig);
String remoteFile = viewArchiveDocuments.getDocfile().substring(0, viewArchiveDocuments.getDocfile().lastIndexOf("/") + 1);
String fileName = viewArchiveDocuments.getDocfile().substring(viewArchiveDocuments.getDocfile().lastIndexOf("/") + 1);
try {
boolean result = ftpUtil.fileExist(remoteFile, fileName);
if (result) {
fileData = ftpUtil.fileGet(remoteFile, fileName);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
ftpUtil.ftpDisconnect();
}
if (fileData!=null) {
try {
byte[] fileArray=fileData.toByteArray();
ByteArrayInputStream inputStream = new ByteArrayInputStream(fileArray);
return Base64.getEncoder().encodeToString(IOUtils.toByteArray(inputStream));
} catch (Exception ex) {
logger.info(ex.getMessage());
return "";
}
}
logger.info("找不到文件,获取不了文件信息");
return "";
}
java
public UpLoadResponse upload(String base64Str, RecTypeDictStorageEntity recTypeDictStorage, ViewArchiveDocumentsEntity viewArchiveDocuments){
UpLoadResponse upLoadResponse=new UpLoadResponse();
//base转输入流
ByteArrayInputStream stream = null;
try {
byte[] bytes = Base64.getDecoder().decode(base64Str);
stream = new ByteArrayInputStream(bytes);
} catch (Exception e) {
e.printStackTrace();
}
InputStream inputStream=stream;
FtpUtil ftpUtil = new FtpUtil();
String ftpConfig = "ftp://USER:PWD@IP:PORT";
ftpConfig = ftpConfig.replace("IP", recTypeDictStorage.getFtpIp())
.replace("USER", recTypeDictStorage.getFtpUser())
.replace("PWD", recTypeDictStorage.getFtpPwd())
.replace("PORT", recTypeDictStorage.getFtpPort());
try {
logger.info("上传首页 ftp配置:" + ftpConfig);
ftpUtil.getConn(ftpConfig);
String remoteFile = ftpUtil.getMedDocPath(viewArchiveDocuments.getPatientid(), viewArchiveDocuments.getEpisodeid());
String fileName = MessageFormat.format("{0}_{1}_{2}.pdf",viewArchiveDocuments.getEpisodeid(),viewArchiveDocuments.getDocdesc(),DateUtil.format(new Date(),"yyyy_MM_dd_HH_mm_ss"));
boolean result = ftpUtil.fileUpload(remoteFile,fileName, inputStream);
upLoadResponse.setResult(result);
upLoadResponse.setFileName(fileName);
upLoadResponse.setRemoteFile(remoteFile);
return upLoadResponse;
} catch (Exception e) {
logger.info(e.getMessage());
} finally {
ftpUtil.ftpDisconnect();
}
upLoadResponse.setResult(false);
return upLoadResponse;
}
通过base64字符串转成输入流,配置好路径地址ip以及端口信息、账号密码等,使用upload方法传输数据
注意
只有在编译的时候才会报错很容易忽视
javaList<RecTypeDictEntity> recTypeDictList=recTypeDictEntities.stream().filter(i->i.getRecTypeCode().equals(docitemecode)).collect(Collectors.toList());
看起来好像没什么问题,但是实际上一运行就报错,因为没有对list中的对象属性判断是否为空,直接使用就会出现空指针异常问题
javaList<RecTypeDictEntity> recTypeDictList=recTypeDictEntities.stream().filter(i->StrUtil.isNotBlank(i.getRecTypeCode())&&i.getRecTypeCode().equals(docitemecode)).collect(Collectors.toList());