上传文件至其他服务器的几种方式
1.sftp文件上传(需要的lib文件地址:com.jcraft.jsch_0.1.55.jar )
import java.io.File; import java.io.FileInputStream; import com.jcraft.jsch.Channel; import com.jcraft.jsch.ChannelSftp; import com.jcraft.jsch.JSch; import com.jcraft.jsch.Session; public class SFtpUtils { public static boolean uploadFile(String host, int post, String user, String pwd, String dir, String src) { String SFTPHOST = host; int SFTPPORT = post; String SFTPUSER = user; String SFTPPASS = pwd; String SFTPWORKINGDIR = dir; String FILETOTRANSFER = src; Session session = null; Channel channel = null; ChannelSftp channelSftp = null; try { JSch jsch = new JSch(); session = jsch.getSession(SFTPUSER, SFTPHOST, SFTPPORT); session.setPassword(SFTPPASS); java.util.Properties config = new java.util.Properties(); config.put("StrictHostKeyChecking", "no"); config.put("kex", "diffie-hellman-group1-sha1"); // 适配新版本ssh需添加对应的加密算法 session.setConfig(config); session.connect(); channel = session.openChannel("sftp"); channel.connect(); channelSftp = (ChannelSftp) channel; channelSftp.cd(SFTPWORKINGDIR); File f = new File(FILETOTRANSFER); channelSftp.put(new FileInputStream(f), f.getName()); channelSftp.disconnect(); channel.disconnect(); session.disconnect(); } catch (Exception ex) { ex.printStackTrace(); return false; } return true; } public static void main(String[] args) { boolean result = uploadFile("192.168.182.128", 22, "root", "123456", "/opt/","E:\\file\\1.doc"); System.out.println("上传结果:" + result); } }
2.scp文件上传(需要的lib文件地址:ganymed-ssh2-build210.jar )
import java.io.IOException;
import ch.ethz.ssh2.Connection; import ch.ethz.ssh2.SCPClient; public class ScpUtils { private String ip; private int port; private String username; private String password; static private ScpUtils instance; static synchronized public ScpUtils getInstance(String ip, int port, String username, String passward) { if (instance == null) { instance = new ScpUtils(ip, port, username, passward); } return instance; } public ScpUtils(String ip, int port, String username, String passward) { this.ip = ip; this.port = port; this.username = username; this.password = passward; } public void getFile(String remoteFile, String localTargetDirectory) { Connection conn = new Connection(ip, port); try { conn.connect(); boolean isAuthenticated = conn.authenticateWithPassword(username, password); if (!isAuthenticated) { System.err.println("authentication failed"); } SCPClient client = new SCPClient(conn); client.get(remoteFile, localTargetDirectory); } catch (IOException ex) { ex.printStackTrace(); }finally{ conn.close(); } } public void putFile(String localFile, String remoteTargetDirectory) { putFile(localFile, null, remoteTargetDirectory); } public void putFile(String localFile, String remoteFileName, String remoteTargetDirectory) { putFile(localFile, remoteFileName, remoteTargetDirectory,null); } public void putFile(String localFile, String remoteFileName, String remoteTargetDirectory, String mode) { Connection conn = new Connection(ip, port); try { conn.connect(); boolean isAuthenticated = conn.authenticateWithPassword(username, password); if (!isAuthenticated) { System.err.println("authentication failed"); } SCPClient client = new SCPClient(conn); if ((mode == null) || (mode.length() == 0)) { mode = "0600"; } if (remoteFileName == null) { client.put(localFile, remoteTargetDirectory); } else { client.put(localFile, remoteFileName, remoteTargetDirectory, mode); } } catch (IOException ex) { ex.printStackTrace(); }finally{ conn.close(); } } public static void main(String[] args) { ScpUtils scpClient = getInstance("192.168.182.128", 22, "root", "123456"); scpClient.putFile("E:\\file\\1.doc","/opt"); } }