I am making a FTP client using apache.commons.vfs and I want to add the copy and paste functionality for the files in the remote server.
I used the copyFrom method to copy the files:
destFile.copyFrom(srcFile, Selectors.SELECT_ALL)
The problem is that in this way the data goes from remote server to the FTP client and from there back to the remote server, making the copy speed very slow compared to copying them by the cp command.
Is there a way in apache.commons.vfs to do the copy using the host commands instead of network transfers?
Related
I need to know internal mechanism used by java.nio.file.Files.copy to copy a local file to remote server.
Like when we used we doesn't pass any port of remote server, so which port it uses to connect with remote server to copy file contents
The connection to the remote server via java.nio.files is not handled by java, it's handled by the underlying OS, so it is using the port normally used by the OS. In windows for example would be smb port:445.
Although the path syntax "\ip\path" make looks like java itself is connecting to the remote host, it's passed down for the OS solve and from program perspective it's a local file in the host OS,
I was wondering if there is a way to copy files on an SFTP server to another directory on the same SFTP server. I want to do this without getting the file in a client and then setting it in the other folder. Of course this would work fine but I guess that this would produce more overhead, so I would like to avoid this if at all possible. I'm currently working with Spring integration which is based on JCraft JSch.
So far I haven't been able to find any way to do this without an intermediary.
Another approach would be to open an SSH channel and just use the cp command but well that's not too pretty either in my opinion.
Thanks in advance!
A core SFTP protocol does not support duplicating a remote file.
There's a draft of copy-file extension to the protocol, but that's supported by only few SFTP servers (ProFTPD mod_sftp and Bitvise SFTP server for example).
In the most widespread OpenSSH SFTP server it is supported only by very recent version 9.0.
And it's also not supported by the JSch library.
See also my answer to How can I copy/duplicate a file to another directory using SFTP?
So actually using the cp shell command over an "exec" channel (ChannelExec) is unfortunately the best available approach (assuming you connect to a *nix server and you have a shell access).
If you do not have a shell access, then your only option is indeed to download the file to a local temporary folder and upload it back to the new location (or use streams, to avoid a temporary file). See also:
How do I transfer a file from one directory to another using Java SFTP Library JSch?
How do I copy files stored in a remote SFTP server to another folder in the same remote server using Java?
I have two servers, one that runs my program written in Java (Server A) and one that stores a graph (Server B) that must be continuously accessed by Server A. To access Server B you must ssh with a username and password using Server B's IP address.
As of now I have failed to find a method to continuously access a directory on a different server and I am wondering if anyone knows a method that lets me do this (or if it is not possible, if there is a workaround).
I have looked into SSH libraries, but they all seem to only give you access to the directory for a brief amount of time. I need continuos access because I write and read from the graph on Server B all the time.
I basically want to make a proxy directory on Server A that actually refers/links to the directory on Server B:
graphDb = new EmbeddedGraphDatabase("/192.168.1.**/media/graphDB");
Any help would be great.
Probably unrelated option:
If client and server are Linux machines, you can use rsync to synchronize files between them. In that way you have a copy of the files on server A. The rsync command could be executed from the Java program or periodically from a cronjob on server A.
You could write your own client/server service, so that the server service provide you with the means to send data over the network to. It tends to be a lot of work though.
You could write your self a "heart beat" service on the client that tests the SSH connection and reestablishes it if it closes
You could "test" the ssh connection before you writing/reading from the connection
You could do as AlperAkture suggests (and mount the directory as a remote drive)
The question us also related to linux but solution is needed for Java. So I have a data directory
/somedir/data
on linux server
servername
I can ssh to the server and do anything I want only from deployment machine (due public/private keys in place). But there's a Java process that should read files from that directory. How can I force it read that files? I was trying to use File("//servername/somedir/data") with no success. Any help would be appreciated.
You must share the file using one of the network file services.
For example:
NFS (check with showmount -e);
Samba (check with smbclient -L);
AFS;
HTTP/FTP (check first if there a HTTP/FTP-server on the host).
You can also access this file using SSH (you say that you have SSH connection to the host, that means that SSH is accessible anyway).
If you want to connect to the SSH server from Java program,
you can use (for example) JSch for that.
Example of JSch usage is here.
I want to establish a connection with my UNIX file system using java program.. So that I can make some File I/O operations and normally I can connect using Putty.
How can I do the same using java program
I have the Host name, username,password and Port number
Help appreciated :)
You need several things:
A server that takes commands (create directory, list directory, write data to a file, read data from a file) over the network. This server should listen to port1 on localhost
You need to configure putty to forward port2 on your local computer to port1 on the server.
A local client which allows you to connect to port2 on your local computer. Putty will tunnel any data send to port2 to port1 on the remote server and vice versa.
Or you get WinSCP which uses the SSH protocol (just like Putty) and maybe already does what you want.
There's a pure Java implementation of SSH/SCP available: http://www.cleondris.ch/opensource/ssh2/
You can use its SCPClient or SFTPv3Client classes to work on the remote file system.
Documentation is available at http://www.cleondris.ch/opensource/ssh2/javadoc.
If you want to do it from Java, you can use Apache Commons VFS. It provides a common approach to dealing with files on all of the supported file systems. SFTP is one of the supported types which is most likely what you would need if you have been connecting with PuTTY.
You need SSH client. There are various pure java SSH clients. Google "java ssh client" and try any one of them. I used Jsch http://www.jcraft.com/jsch/ and it worked fine for me.