java获取远程服务器目录,在远程服务器创建三级目录
1、添加依赖
<dependency>
<groupId>com.jcraft</groupId>
<artifactId>jsch</artifactId>
<version>0.1.49</version>
</dependency>
<dependency>
<groupId>com.trilead</groupId>
<artifactId>trilead-ssh2</artifactId>
<version>1.0.0-build222</version>
</dependency>
2、获取远程服务器目录代码
/**
* 判断远程服务器路径是否是目录
* @param client
* @param path
* @return
*/
private static boolean isDirectory(SFTPv3Client client, String path) {
try {
SFTPv3FileAttributes attributes = client.stat(path);
return attributes.isDirectory();
} catch(IOException e) {
System.out.println("获取文件属性异常"+ e);
}
return false;
}
/**
* 获取conn
* @param ip
* @param port
* @param user
* @param pwd
* @return
*/
private static Connection getConn(String ip, int port, String user, String pwd) {
Connection conn = new Connection(ip, port);
if(conn.isAuthenticationComplete()) {
return conn;
}
try {
conn.connect();
boolean isAuthenticated = conn.authenticateWithPassword(user, pwd);
if(!isAuthenticated) {
throw new Exception("authentication failed!");
}
return conn;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 获取SFTPv3Client
* @param conn
* @return
* @throws IOException
*/
public static SFTPv3Client getClient(Connection conn) throws IOException {
SFTPv3Client client = new SFTPv3Client(conn);
return client;
}
3、创建三级目录代码
//主方法
public static void main(String[] args) {
String path="/data/video/rt/down1/load/";
mkdir(path,"192.168.15.166",22,"username","123456");
}
/**
* 创建远程服务器文件夹主方法(本方法只支持三级及以下目录创建,三级以上需修改代码)
* @param path 远程服务器目录相对路径(例如:/data/video/rt/down1/load/)
* @param ip 远程服务器ip(例如:192.168.14.155)
* @param port 远程服务器端口(例如:22)
* @param username 远程服务器用户名(例如:root)
* @param password 远程服务器密码(例如:123456)
*/
public static void mkdir(String path,String ip,int port,String username,String password) {
//写入路径
//三级路径
String path1=path.substring(0,path.lastIndexOf("/"));
//二级路径
String path2=path1.substring(0,path1.lastIndexOf("/"));
//一级路径
String path3=path2.substring(0,path2.lastIndexOf("/"));
System.out.println("path1="+path1);
System.out.println("path2="+path2);
System.out.println("path3="+path3);
try {
//判断文件夹是否存在
Connection conn=getConn(ip,port,username,password);
SFTPv3Client client=getClient(conn);
boolean isExist1 = isDirectory(client, path3);
boolean isExist2 = isDirectory(client, path2);
boolean isExist3 = isDirectory(client, path1);
JSch jsch = new JSch();
com.jcraft.jsch.Session session = jsch.getSession(username, ip, port);
if (password != null) {
session.setPassword(password);
}
Properties config = new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
Channel channel = session.openChannel("sftp");
channel.connect();
ChannelSftp sftp = (ChannelSftp) channel;
if(!isExist3){
if(!isExist2){
if(!isExist1){
sftp.mkdir(path3);
sftp.mkdir(path2);
sftp.mkdir(path1);
System.out.println("全部新建存入视频");
}else {
sftp.mkdir(path2);
sftp.mkdir(path1);
System.out.println("新建到二级目录存入");
}
}else {
// file3.mkdir();
sftp.mkdir(path1);
System.out.println("新建三级目录存入");
}
}else {
System.out.println("直接存入");
}
//关闭所有连接
sftp.disconnect();
session.disconnect();
channel.disconnect();
conn.close();
client.close();
} catch (Exception e) {
e.printStackTrace();
}
}