开发者

How can I duplicate files on a remote server in Java?

I'm trying to find a way to duplicate a series of files/folders from one section on a server, to a new directory on the same server. I looked into doing this with FTP, but it seems t开发者_如何学Goo be strictly for transfer and not so much changing files on the server itself. As such, I've been looking into SSH and wondering if it might be possible that way. Is it possible? If not, is there another way, or perhaps and easier way to do this? Any help would be much appreciated thanks!


I'm not sure if this is the best way, but, if you have SSH access to the server, you can SSH in and use the system's native copy command. I would recommend you use the Jsch library to SSH into the box and then just call the appropriate command (cp or copy).

Jsch supplies no documentation, but they include tons of example code. You can follow the Exec.java file to show you how to execute commands on the remote server. Also, fyi, they use tons of swing code in their examples. You can easily remove all of that if you don't want swing username/password prompts.


Yeah, both FTP and SFTP (which is the file transfer mode of SSH) are mainly for transferring files between client and server. Additionally they also support some management tasks (like creating directories, setting modes, listing files, removing files/directories, even renaming files), but no copying of files without downloading and uploading again.

As Jon7 and Mark proposed, you can (per ssh) invoke the remote server's native copy command (copy or xcopy on Windows, cp on Unix-like systems) to do the job, assuming you have shell access (not only SFTP or some forced command).

If using JSch, an exec channel would be the thing to use here.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜