What is the best method of performing an scp transfer via the Java programming language? It seems I may be able to perform this via JSSE, JSch or the bouncy castle java libraries. None of these solutions seem to have an easy answer.
相关问题
- Delete Messages from a Topic in Apache Kafka
- Jackson Deserialization not calling deserialize on
- How to maintain order of key-value in DataFrame sa
- StackExchange API - Deserialize Date in JSON Respo
- Difference between Types.INTEGER and Types.NULL in
This is high-level solution, no need to reinvent. Quick and dirty!
1) First, go to http://ant.apache.org/bindownload.cgi and download latest Apache Ant binary. (nowadays, apache-ant-1.9.4-bin.zip).
2) Extract the downloaded file and find the JAR ant-jsch.jar ("apache-ant-1.9.4/lib/ant-jsch.jar"). Add this JAR in your project. Also ant-launcher.jar and ant.jar.
3) Go to Jcraft jsch SouceForge Project and download the jar. Nowadays, jsch-0.1.52.jar. Also Add this JAR in your project.
Now, can you easyly use into java code the Ant Classes Scp for copy files over network or SSHExec for commands in SSH servers.
4) Code Example Scp:
jsCH has worked great for me. Below is an example of a method that will connect to sftp server and download files to specified directory. It is recommended to stay away from disabling StrictHostKeyChecking. Although a little bit more difficult to set up, for security reasons specifying the known hosts should be the norm.
jsch.setKnownHosts("C:\Users\test\known_hosts"); recommended
JSch.setConfig("StrictHostKeyChecking", "no"); - not recommended
plug: sshj is the only sane choice! See these examples to get started: download, upload.
Here is an example to upload a file using JSch:
ScpUploader.java
:AppEntryPoint.java
:I looked at a lot of these solutions and didn't like many of them. Mostly because the annoying step of having to identify your known hosts. That and JSCH is at a ridiculously low level relative to the scp command.
I found a library that doesn't require this but it's bundled up and used as a command line tool. https://code.google.com/p/scp-java-client/
I looked through the source code and discovered how to use it without the command line. Here's an example of uploading:
The biggest downside is that it's not in a maven repo (that I could find). But, the ease of use is worth it to me.
JSch is a nice library to work with. It has quite an easy answer for your question.
You can find complete code at
http://faisalbhagat.blogspot.com/2013/09/java-uploading-file-remotely-via-scp.html