上传文件至其他服务器的几种方式
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"); } }
本文作者:远山伴痴人
本文链接:https://www.cnblogs.com/BKYhailong/p/18267485
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步