问题描述
我正在使用 Apache Commons FTP 上传文件.在上传之前,我想检查该文件是否已存在于服务器上,并将其备份到同一服务器上的备份目录中.
I'm using Apache Commons FTP to upload a file. Before uploading I want to check if the file already exists on the server and make a backup from it to a backup directory on the same server.
有人知道如何将文件从 FTP 服务器复制到同一服务器上的备份目录吗?
<小时>
编辑代码:还是有问题,当我备份 zip 文件时,备份的文件已损坏.
Edited code: still there is a problem, when i backup zip file, the backup-ed file is corrupted.
有人知道原因吗?
推荐答案
如果你使用的是 apache commons net FTPClient
,有一个直接的方法可以将文件从一个位置移动到另一个位置(如果user
有适当的权限).
If you are using apache commons net FTPClient
, there is a direct method to move a file from one location to another location (if the user
has proper permissions).
或者,如果你熟悉 ftp 命令
,你可以使用类似
or, If you are familiar with ftp commands
, you can use something like
如果您使用任何其他客户端,请阅读文档,客户端实现之间不会有太大变化.
If you are using any other client, go through the documentation, There wont be much changes between client implementations.
更新:
上述方法将文件移动到 to
目录,即该文件将不再存在于 from
目录中.基本上 ftp 协议意味着从 local <-> 传输文件.远程
或远程<->其他远程
,但不能在服务器中传输.
Above approach moves the file to to
directory, i.e, the file won't be there in from
directory anymore. Basically ftp protocol meant to be transfer the files from local <-> remote
or remote <-> other remote
but not to transfer with in the server.
这里的工作会更简单,将完整的文件获取到本地 InputStream
并将其作为备份目录中的新文件写回服务器.
The work around here, would be simpler, get the complete file to a local InputStream
and write it back to the server as a new file in the back up directory.
要获取完整的文件,
现在,将此流存储到备份目录.首先,我们需要将工作目录更改为备份目录.
now, store this stream to backup directory. First we need to change working directory to backup directory.
希望对你有所帮助..
这篇关于java - 如何将FTP服务器上的文件复制到Java中同一服务器上的目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!