How to copy a file in FTP server using FTPClient in Java?(如何在 Java 中使用 FTPClient 复制 FTP 服务器中的文件?)
问题描述
我有一个 CSV 文件,我需要复制它并在同一路径中重命名它.
I have a CSV file, and I need to copy it and rename it in the same path.
我在 FTP 登录后尝试了这个:
I tried this after the FTP login:
InputStream inputStream = ftpClient.retrieveFileStream(cvs_name +".csv");
ftpClient.storeFile(cvs_name2 + ".csv",inputStream);
但是当我在服务器上验证文件时,它是空的.如何复制文件并重命名?
But when I verify the file on the server, it's empty. How can I copy a file and rename it?
推荐答案
我相信你的代码不能工作.您不能同时通过一个 FTP 连接下载和上传文件.
I believe your code cannot work. You cannot download and upload a file over a single FTP connection at the same time.
你有两个选择:
首先将文件完全下载(到临时文件或内存中).
Download the file completely first (to a temporary file or to a memory).
如何将 ftp 服务器上的文件复制到 java 中同一服务器上的目录?显示记忆"解决方案.注意 outputStream.toByteArray()
调用.
The accepted answer to How to copy a file on the ftp server to a directory on the same server in java? shows the "to memory" solution. Note the outputStream.toByteArray()
call.
打开两个连接(FTPClient
的两个实例)并在实例之间复制文件.
Open two connections (two instances of the FTPClient
) and copy the file between the instances.
InputStream inputStream = ftpClient1.retrieveFileStream(cvs_name + ".csv");
ftpClient2.storeFile(cvs_name2 + ".csv", inputStream);
这篇关于如何在 Java 中使用 FTPClient 复制 FTP 服务器中的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 Java 中使用 FTPClient 复制 FTP 服务器中的文


基础教程推荐
- 降序排序:Java Map 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01