import ch.ethz.ssh2.Connection;
import ch.ethz.ssh2.SCPClient;
import ch.ethz.ssh2.SCPInputStream;
import ch.ethz.ssh2.SCPOutputStream;
一、上传文件到linux服务器
/** * 上传文件到Linux服务器 * * @param hostName 服务器IP地址 * @param username 服务器用户名 * @param password 服务器密码 * @param port 服务器端口号 * @param f 文件对象 * @param length 文件大小 * @param remoteTargetDirectory 服务器上传文件的所在路径 * @param mode 默认为null */ public static void uploadFile(String hostName, String username, String password, int port, File f, long length, String remoteTargetDirectory, String mode) { Connection conn = new Connection(hostName, port); SCPOutputStream os = null; FileInputStream fis = null; try { // 连接到主机 conn.connect(); // 校验用户名密码 boolean isAuthenticated = conn.authenticateWithPassword(username,password); if(!isAuthenticated){ logger.error("建立连接失败,用户名或者密码不正确。"); return ; } SCPClient scpClient = new SCPClient(conn); os = scpClient.put(f.getName(),length,remoteTargetDirectory,mode); byte[] b = new byte[4096]; fis = new FileInputStream(f); int i; while ((i = fis.read(b)) != -1) { os.write(b, 0, i); } os.flush(); logger.info("上传文件成功。文件名:" + f.getName()); } catch (IOException e) { e.printStackTrace(); }finally{ try { fis.close(); } catch (IOException e) { e.printStackTrace(); } try { os.close(); } catch (IOException e) { e.printStackTrace(); } conn.close(); } } |
二、从linux上下载文件到本地
/** * 下载Linux文件到本地 * * @param hostName 服务器IP地址 * @param username 服务器用户名 * @param password 服务器密码 * @param port 服务器端口号 * @param remoteFile 服务器上的文件名 * @param remoteTargetDirectory 服务器上文件的所在路径 * @param newPath 下载文件的路径 */ public static void downloadFile(String hostName, String username, String password, int port, String remoteFile, String remoteTargetDirectory,String newPath){ Connection conn = new Connection(hostName, port); SCPInputStream sis = null; FileOutputStream fos = null; try { // 连接到主机 conn.connect(); // 校验用户名密码 boolean isAuthenticated = conn.authenticateWithPassword(username,password); if(!isAuthenticated){ logger.error("建立连接失败,用户名或者密码不正确。"); return ; } SCPClient scpClient = conn.createSCPClient(); sis = scpClient.get(remoteTargetDirectory + "/" + remoteFile); File f = new File(newPath); if(!f.exists()){ f.mkdirs(); } File newFile = new File(newPath + remoteFile); fos = new FileOutputStream(newFile); byte[] b = new byte[4096]; int i; while ((i = sis.read(b)) != -1){ fos.write(b,0, i); } fos.flush(); logger.info("下载文件成功。文件名:" + remoteFile); } catch (IOException e) { e.printStackTrace(); }finally{ try { sis.close(); } catch (IOException e) { e.printStackTrace(); } try { fos.close(); } catch (IOException e) { e.printStackTrace(); } conn.close(); } } |
应用实例:
public static void main(String[] args) {
String path = "/usr/temp/test";
String hostName = "174.22.70.33";
int port = 22;
String username = "root";
String password = "password";
// downloadFile(hostName,username,password,port,"a",path,"E:/");
File f = new File("E:/new.xml");
uploadFile(hostName,username,password,port,f,f.length(),path,null);
}
2.第三方工具 SecureCRT 点击下方按钮进行拖拽上传
3.在界面使用下载命令和上传命令
前提需要设置好:上传目录和下载目录。
下载:sz xx文件;上传:rz xx文件