编辑
2023-04-12
学习记录
00

简介

先读取压缩文件到本地,然后解压,再向前端输出文件夹中的内容

读取

这部分忽略,可以直接查看之前的文章,直接从ftpUtil获取文件,得到ByteArrayOutputStream开始,讲解

下载文件并解密

java
//通过zip获取pdf public ByteArrayOutputStream getPdfBase64ByZip(ByteArrayOutputStream fileData,String fileName) throws Exception { ByteArrayInputStream inputStream = new ByteArrayInputStream(fileData.toByteArray()); String base64= Base64Encoder.encode(IOUtils.toByteArray(inputStream)); System.out.println(base64); String unzipPath=RecMrDetailIndexController.class.getResource("/unzip/").getPath(); String zipName=unzipPath+fileName.split("as")[0]+"zip"; FileUtils.base64ToFile(zipName,base64); ZipUtil.unZip(zipName,unzipPath,"mars"); String pdfName=unzipPath+fileName.split("as")[0]+"pdf"; System.out.println("pdfName"+pdfName); File file=new File(pdfName); FileInputStream fileInputStream=new FileInputStream(file); byte[] bytes=IOUtils.toByteArray(fileInputStream); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(bytes.length); outputStream.write(bytes, 0, bytes.length); return outputStream; }
编辑
2023-04-12
学习记录
00

简介

业务需要将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 ""; }
编辑
2023-04-11
遇到的问题
00

base64转换错误

java
byte[] bytes = Base64.getDecoder().decode(base64Str);

改成

java
byte[] bytes = Base64.getMimeDecoder().decode(base64Str);
编辑
2023-04-07
实用工具
00

简介

hanlp官网,HanLP 是由一系列模型与算法组成的工具包,目标是普及自然语言处理在生产环境中的应用。HanLP 具备功能完善、性能高效、架构清晰、语料时新、可自定义的特点。

HanLP 主要功能包括分词、词性标注、关键词提取、自动摘要、依存句法分析、命名实体识别、短语提取、拼音转换、简繁转换等等。

引入依赖

xml
<dependency> <groupId>com.hankcs</groupId> <artifactId>hanlp</artifactId> <version>portable-1.7.3</version> </dependency>
编辑
2023-04-04
学习记录
00

简介

目的:要将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方法传输数据