获取ftp上指定文件的InputStream

博客介绍了通过ftp.retrieveFileStream(fileName)可返回ftp文件输入流,当输入流关闭后,返回的inputStream对象为空。可通过字节数组byte[]保存,再用new ByteArrayInputStream(bytes)方法重新获得输入流。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

ftp.retrieveFileStream(fileName)可以返回ftp文件的输入流 ,但输入流关闭(inputStream.close())之后返回的inputStream对象就为空,可以通过字节数组byte[]保存后返回出去再new ByteArrayInputStream(bytes)方法重新获得输入流。

/**
 * 获取ftp上文件的InputStream
 * @param ftpDirName ftp文件夹名称
 * @param ftpFileName ftp文件名称
 * @return
 */
public byte[] getInputStream(String ftpDirName, String ftpFileName) {
    try {
        if ("".equals(ftpDirName)) {
            ftpDirName = "/";
        }
        String dir = new String(ftpDirName.getBytes("GBK"), "iso-8859-1");
        if (!ftp.changeWorkingDirectory(dir)) {
            System.out.println("切换目录失败:" + ftpDirName);
            return null;
        }
        // 一定要加上字符集指定,因为获取文件时有中文,会出现乱码而获取不到。
        String fileName = new String(ftpFileName.getBytes("GBK"), "iso-8859-1");
        ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
        // 每次数据连接之前,ftp client告诉ftp server开通一个端口来传输数据,ftp server可能每次开启不同的端口来传输数据,
        // 但是在Linux上,由于安全限制,可能某些端口没有开启,所以就出现阻塞。
        ftp.enterLocalPassiveMode();
        InputStream inputStream = ftp.retrieveFileStream(fileName);
        byte[] bytes = IOUtils.toByteArray(inputStream);
        if (inputStream != null) {
            inputStream.close();
        }
        ftp.getReply();
        ftp.logout();
        return bytes;
    } catch (Exception e) {
        log.error("获取文件流出现异常", e);
        return null;
    }
}

调用时代码如下:

byte[] inputStream = ftpHelper.getInputStream(ECG_PATH, fileName);
InputStream fileStream = new ByteArrayInputStream(inputStream);

 

Spring Boot 可以通过第三方库如spring-integration-file或Apache Commons VFS等集成FTP功能,以便从FTP服务器获取PDF文件。以下是一个简单的步骤说明: 1. 添加依赖:首先,在你的`pom.xml`中添加FTP相关的Spring Integration或Apache Commons VFS的依赖,例如: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-integration</artifactId> </dependency> <!-- 或者如果使用Apache Commons VFS --> <dependency> <groupId>commons-vfs</groupId> <artifactId>commons-vfs2</artifactId> </dependency> ``` 2. 配置FTP连接:在Spring Boot配置类中,配置FTP连接信息,包括主机名、端口、用户名和密码: ```java @Bean public FtpOperations ftpTemplate() { SimpleFtpClientFactory factory = new SimpleFtpClientFactory(); factory.setUser("your_username"); factory.setPassword("your_password"); factory.setHost("ftp.example.com"); factory.setPort(21); return new FtpOperations(new DefaultFileSystemResourceResolver(), factory); } ``` 3. 获取PDF文件:使用`FtpOperations`的`get()`方法来下载文件指定路径: ```java String filePath = "/path/to/your/file.pdf"; Resource resource = ftpTemplate().get(filePath); if (resource.exists()) { // 将资源换为InputStream或其他处理方式 InputStream inputStream = resource.getInputStream(); // ...进一步操作,比如读取内容、保存到本地或其他服务 } else { throw new FileNotFoundException("File not found on FTP server"); } ``` 4. 异步处理:如果你希望异步处理文件下载,可以考虑使用Spring Integration的`Poller`或者`Flux`。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值