FTP获取InputStream再上传返回null

FTP获取InputStream再上传返回null

开发FTP工具类的过程中,设计了一个获取FTP文件的InputStream流的获取方法(方法名为getInputStream(String filePath)),ftp获取InputStream流的方法网上有很多,所以获取InputStream流的方法在此不再赘述。问题出现在测试的过程中。
因为要验证获取到InputStream流是否正确,所以测试的时候先调用getInputStream方法得到InputStream,然后再调用ftp的上传方法(uploadFile(InputStream inputStream))测试是否能上传成功。上传成功则说明getInputStream方法没有问题。
代码如下:

 @Test
    public void fun8() throws IOException {

        StorageService storageService = new FtpStorageServiceImpl();
        // 获取ftp返回的inputStream
        InputStream inputStream = storageService.getInputStream("nnn/20221210/b63a0734ef444941837794de3c7b52eb.txt");
        将获取到的inputStream传入uploadFile进行文件上传
        String filePath= storageService.uploadFile(inputStream, "txt", "test1210");
        System.out.println("filePath = " + filePath);
    }

执行返回的结果如下:

filePath = null

但是在测试的时候,将返回的inputStream再调用uploadFile(inputStream)进行上传,但是调用getInputStream方法一直返回null,调试发现返回的inputStream是没有问题的,uploadFile方法本身也是没有问题的,但是一直返回空,排查了大半天,发现是因为使用了同一个ftp实现类的实例导致的,即上传和下载不能使用同一个工具类实例。用新的实例进行上传可以上传成功。代码如下:

 @Test
    public void fun8() throws IOException {

        StorageService storageService = new FtpStorageServiceImpl();
        // 获取ftp返回的inputStream
        InputStream inputStream = storageService.getInputStream("nnn/20221210/b63a0734ef444941837794de3c7b52eb.txt");
        将获取到的inputStream传入uploadFile进行文件上传
        String filePath= new FtpStorageServiceImpl().uploadFile(inputStream, "txt", "test1210");
        System.out.println("filePath = " + filePath);
    }

执行返回的结果如下:

filePath = test1210/20221211/c0b5bb75d89741e6bc7009a044e469ba.txt

修改后测试通过。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
你可以使用 Apache Commons Net 库来实现将 MultipartFile 上传FTP 服务器的功能。以下是一个示例代码: ```kotlin import org.apache.commons.net.ftp.FTP import org.apache.commons.net.ftp.FTPClient import org.springframework.web.multipart.MultipartFile import java.io.IOException import java.io.InputStream fun uploadFileToFTP(file: MultipartFile, ftpServer: String, username: String, password: String, remoteFilePath: String, remoteFileName: String): Boolean { val ftpClient = FTPClient() var inputStream: InputStream? = null try { ftpClient.connect(ftpServer) ftpClient.login(username, password) // 设置文件类型为二进制文件 ftpClient.setFileType(FTP.BINARY_FILE_TYPE) // 创建远程目录 val remoteDirectoryPath = remoteFilePath.replace("\\", "/") val directories = remoteDirectoryPath.split("/") for (directory in directories) { if (directory.isNotEmpty()) { ftpClient.makeDirectory(directory) ftpClient.changeWorkingDirectory(directory) } } // 上传文件 inputStream = file.inputStream ftpClient.storeFile(remoteFileName, inputStream) return true } catch (e: IOException) { e.printStackTrace() return false } finally { try { inputStream?.close() ftpClient.logout() ftpClient.disconnect() } catch (e: IOException) { e.printStackTrace() } } } ``` 在该示例中,我们首先创建了一个 FTPClient 对象并连接到 FTP 服务器。然后,我们设置了文件类型为二进制文件,并创建了远程目录。最后,我们使用 MultipartFile 的 inputStream 将文件上传FTP 服务器中。 注意,这里的 remoteFileName 参数应该包含文件名和扩展名。例如,如果你要上传名为 example.png 的文件,则 remoteFileName 应该为 "example.png"。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值