国产片侵犯亲女视频播放_亚洲精品二区_在线免费国产视频_欧美精品一区二区三区在线_少妇久久久_在线观看av不卡

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術|正則表達式|C/C++|IOS|C#|Swift|Android|VB|R語言|JavaScript|易語言|vb.net|

服務器之家 - 編程語言 - Java教程 - java實現sftp客戶端上傳文件以及文件夾的功能代碼

java實現sftp客戶端上傳文件以及文件夾的功能代碼

2020-08-06 15:01java.小菜鳥 Java教程

本篇文章主要介紹了java實現sftp客戶端上傳文件以及文件夾的功能代碼,具有一定的參考價值,有興趣的可以了解一下。

1.依賴的jar文件 jsch-0.1.53.jar

2.登錄方式有密碼登錄,和密匙登錄

 代碼:

主函數:

java" id="highlighter_779889">
?
1
2
3
4
5
6
7
8
9
10
11
import java.util.Properties;
 
import com.cloudpower.util.Login;
import com.util.LoadProperties;
 
public class Ftp {
  public static void main(String[] args) {
    Properties properties = LoadProperties.getProperties();
    Login.login(properties);
  }
}

登陸頁面的代碼:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
package com.cloudpower.util;
 
import java.io.Console;
import java.util.Properties;
 
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
 
public class Login {
 
  public static void login(Properties properties) {
    String ip = properties.getProperty("ip");
    String user = properties.getProperty("user");
    String pwd = properties.getProperty("pwd");
    String port = properties.getProperty("port");
    String privateKeyPath = properties.getProperty("privateKeyPath");
    String passphrase = properties.getProperty("passphrase");
    String sourcePath = properties.getProperty("sourcePath");
    String destinationPath = properties.getProperty("destinationPath");
 
    if (ip != null && !ip.equals("") && user != null && !user.equals("")
        && port != null && !port.equals("") && sourcePath != null
        && !sourcePath.equals("") && destinationPath != null
        && !destinationPath.equals("")) {
 
      if (privateKeyPath != null && !privateKeyPath.equals("")) {
        sshSftp2(ip, user, Integer.parseInt(port), privateKeyPath,
            passphrase, sourcePath, destinationPath);
      } else if (pwd != null && !pwd.equals("")) {
        sshSftp(ip, user, pwd, Integer.parseInt(port), sourcePath,
            destinationPath);
      } else {
        Console console = System.console();
        System.out.print("Enter password:");
        char[] readPassword = console.readPassword();
        sshSftp(ip, user, new String(readPassword),
            Integer.parseInt(port), sourcePath, destinationPath);
      }
    } else {
      System.out.println("請先設置配置文件");
    }
  }
 
  /**
   * 密碼方式登錄
   *
   * @param ip
   * @param user
   * @param psw
   * @param port
   * @param sPath
   * @param dPath
   */
  public static void sshSftp(String ip, String user, String psw, int port,
      String sPath, String dPath) {
    System.out.println("password login");
    Session session = null;
 
    JSch jsch = new JSch();
    try {
      if (port <= 0) {
        // 連接服務器,采用默認端口
        session = jsch.getSession(user, ip);
      } else {
        // 采用指定的端口連接服務器
        session = jsch.getSession(user, ip, port);
      }
 
      // 如果服務器連接不上,則拋出異常
      if (session == null) {
        throw new Exception("session is null");
      }
 
      // 設置登陸主機的密碼
      session.setPassword(psw);// 設置密碼
      // 設置第一次登陸的時候提示,可選值:(ask | yes | no)
      session.setConfig("StrictHostKeyChecking", "no");
      // 設置登陸超時時間
      session.connect(300000);
      UpLoadFile.upLoadFile(session, sPath, dPath);
    } catch (Exception e) {
      e.printStackTrace();
    }
 
    System.out.println("success");
  }
 
  /**
   * 密匙方式登錄
   *
   * @param ip
   * @param user
   * @param port
   * @param privateKey
   * @param passphrase
   * @param sPath
   * @param dPath
   */
  public static void sshSftp2(String ip, String user, int port,
      String privateKey, String passphrase, String sPath, String dPath) {
    System.out.println("privateKey login");
    Session session = null;
    JSch jsch = new JSch();
    try {
      // 設置密鑰和密碼
      // 支持密鑰的方式登陸,只需在jsch.getSession之前設置一下密鑰的相關信息就可以了
      if (privateKey != null && !"".equals(privateKey)) {
        if (passphrase != null && "".equals(passphrase)) {
          // 設置帶口令的密鑰
          jsch.addIdentity(privateKey, passphrase);
        } else {
          // 設置不帶口令的密鑰
          jsch.addIdentity(privateKey);
        }
      }
      if (port <= 0) {
        // 連接服務器,采用默認端口
        session = jsch.getSession(user, ip);
      } else {
        // 采用指定的端口連接服務器
        session = jsch.getSession(user, ip, port);
      }
      // 如果服務器連接不上,則拋出異常
      if (session == null) {
        throw new Exception("session is null");
      }
      // 設置第一次登陸的時候提示,可選值:(ask | yes | no)
      session.setConfig("StrictHostKeyChecking", "no");
      // 設置登陸超時時間
      session.connect(300000);
      UpLoadFile.upLoadFile(session, sPath, dPath);
      System.out.println("success");
    } catch (Exception e) {
      e.printStackTrace();
    }
  }
 
}

文件上傳的代碼:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
package com.cloudpower.util;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Scanner;
 
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException;
 
public class UpLoadFile {
  public static void upLoadFile(Session session, String sPath, String dPath) {
 
    Channel channel = null;
    try {
      channel = (Channel) session.openChannel("sftp");
      channel.connect(10000000);
      ChannelSftp sftp = (ChannelSftp) channel;
      try {
        sftp.cd(dPath);
        Scanner scanner = new Scanner(System.in);
        System.out.println(dPath + ":此目錄已存在,文件可能會被覆蓋!是否繼續y/n?");
        String next = scanner.next();
        if (!next.toLowerCase().equals("y")) {
          return;
        }
 
      } catch (SftpException e) {
 
        sftp.mkdir(dPath);
        sftp.cd(dPath);
 
      }
      File file = new File(sPath);
      copyFile(sftp, file, sftp.pwd());
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      session.disconnect();
      channel.disconnect();
    }
  }
 
  public static void copyFile(ChannelSftp sftp, File file, String pwd) {
 
    if (file.isDirectory()) {
      File[] list = file.listFiles();
      try {
        try {
          String fileName = file.getName();
          sftp.cd(pwd);
          System.out.println("正在創建目錄:" + sftp.pwd() + "/" + fileName);
          sftp.mkdir(fileName);
          System.out.println("目錄創建成功:" + sftp.pwd() + "/" + fileName);
        } catch (Exception e) {
          // TODO: handle exception
        }
        pwd = pwd + "/" + file.getName();
        try {
 
          sftp.cd(file.getName());
        } catch (SftpException e) {
          // TODO: handle exception
          e.printStackTrace();
        }
      } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
      for (int i = 0; i < list.length; i++) {
        copyFile(sftp, list[i], pwd);
      }
    } else {
 
      try {
        sftp.cd(pwd);
 
      } catch (SftpException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
      }
      System.out.println("正在復制文件:" + file.getAbsolutePath());
      InputStream instream = null;
      OutputStream outstream = null;
      try {
        outstream = sftp.put(file.getName());
        instream = new FileInputStream(file);
 
        byte b[] = new byte[1024];
        int n;
        try {
          while ((n = instream.read(b)) != -1) {
            outstream.write(b, 0, n);
          }
        } catch (IOException e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }
 
      } catch (SftpException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      } finally {
        try {
          outstream.flush();
          outstream.close();
          instream.close();
 
        } catch (Exception e2) {
          // TODO: handle exception
          e2.printStackTrace();
        }
      }
    }
  }
 
}

讀取配置文件的代碼:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package com.util;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
 
public class LoadProperties {
  public static Properties getProperties() {
 
    File file = new File(Class.class.getClass().getResource("/").getPath()
        + "properties.properties");
 
    InputStream inputStream = null;
 
    try {
      inputStream = new FileInputStream(file);
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
 
    Properties properties = new Properties();
    try {
      properties.load(inputStream);
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
 
    return properties;
 
  }
}

代碼目錄結構:

java實現sftp客戶端上傳文件以及文件夾的功能代碼

測試運行時配置文件放在項目的bin目錄下(打包成可運行jar文件的時候要刪除,打包完成后將配置文件和jar包放在同級目錄下即可):

properties.properties

?
1
2
3
4
5
6
7
8
ip=
user=
pwd=
port=22
privateKeyPath=
passphrase=
sourcePath=
destinationPath=/home/dbbs/f

打包可運行jar文件:

Export->java->Runnabe JAR file

完成后

在控制臺運行java -jar  導出jar包的名字.jar 即可

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。

原文鏈接:http://www.cnblogs.com/zuo-java/p/5913295.html

延伸 · 閱讀

精彩推薦
Weibo Article 1 Weibo Article 2 Weibo Article 3 Weibo Article 4 Weibo Article 5 Weibo Article 6 Weibo Article 7 Weibo Article 8 Weibo Article 9 Weibo Article 10 Weibo Article 11 Weibo Article 12 Weibo Article 13 Weibo Article 14 Weibo Article 15 Weibo Article 16 Weibo Article 17 Weibo Article 18 Weibo Article 19 Weibo Article 20 Weibo Article 21 Weibo Article 22 Weibo Article 23 Weibo Article 24 Weibo Article 25 Weibo Article 26 Weibo Article 27 Weibo Article 28 Weibo Article 29 Weibo Article 30 Weibo Article 31 Weibo Article 32 Weibo Article 33 Weibo Article 34 Weibo Article 35 Weibo Article 36 Weibo Article 37 Weibo Article 38 Weibo Article 39 Weibo Article 40
主站蜘蛛池模板: 久久久一 | 久久中文字幕一区二区 | 日韩精品在线观看一区 | 精品在线视频一区 | 人人爱人人爽 | 日本一区二区三区视频免费看 | 亚洲每日更新 | 99手机在线视频 | 精品伊人久久 | 欧美在线小视频 | 欧美国产日韩视频 | 日韩精品视频在线播放 | 亚洲文字幕| 在线观看欧美一区 | 刘亦菲的毛片 | 色偷偷888欧美精品久久久 | 国产精品久久久久久久久久久久久 | 国产成人自拍视频在线 | 亚洲欧美自拍偷拍 | 一区二区三区视频在线观看 | 久久草在线视频 | 婷婷精品久久久久久久久久不卡 | 国产精品久久久久久久久久久免费看 | 国产欧美日韩综合精品一区二区 | 精品无人乱码一区二区三区 | 亚洲午夜精品视频 | 欧美一区二区三区在线观看视频 | 美日韩成人 | 中文字幕在线观看一区 | 亚洲国产高清高潮精品美女 | 日韩免费电影 | 黄色av网站在线免费观看 | 免费成人小视频 | 亚洲国产中文字幕 | 在线免费观看a视频 | 波多野结衣一区二区三区中文字幕 | av在线综合网 | 尤物视频在线观看 | 日韩第一区 | 一区二区成人 | 老丁头电影在线观看 |