编辑
2023-04-04
学习记录
00
请注意,本文编写于 730 天前,最后修改于 721 天前,其中某些信息可能已经过时。

目录

简介
读取代码
上传代码
依赖引入

简介

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

依赖引入

xml
<!-- commons-lang3 --> <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-lang3</artifactId> <version>3.4</version> </dependency> <!--commons-io--> <dependency> <groupId>commons-io</groupId> <artifactId>commons-io</artifactId> <version>2.6</version> </dependency> <!--文件上传--> <dependency> <groupId>commons-fileupload</groupId> <artifactId>commons-fileupload</artifactId> <version>1.3.1</version> </dependency> <dependency> <groupId>commons-net</groupId> <artifactId>commons-net</artifactId> <version>3.6</version> </dependency>

本文作者:Weee

本文链接:

版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!