第二十篇:JAVA SFTP(安全文件传送协议)操作

来源:互联网 发布:帝国时代 亚洲王朝java 编辑:程序博客网 时间:2024/06/08 07:33

sftp是Secure File Transfer Protocol的缩写,安全文件传送协议。可以为传输文件提供一种安全的加密方法。sftp 与 ftp 有着几乎一样的语法和功能。SFTP 为 SSH的一部分,是一种传输档案至 Blogger 伺服器的安全方式。其实在SSH软件包中,已经包含了一个叫作SFTP(Secure File Transfer Protocol)的安全文件传输子系统,SFTP本身没有单独的守护进程,它必须使用sshd守护进程(端口号默认是22)来完成相应的连接操作,所以从某种意义上来说,SFTP并不像一个服务器程序,而更像是一个客户端程序。SFTP同样是使用加密传输认证信息和传输的数据,所以,使用SFTP是非常安全的。但是,由于这种传输方式使用了加密/解密技术,所以传输效率比普通的FTP要低得多,如果您对网络安全性要求更高时,可以使用SFTP代替FTP。

本篇示例中使用的第三方包为:jsch.jar

import java.io.File;import java.io.FileInputStream;import java.io.FileOutputStream;import java.util.ArrayList;import java.util.Iterator;import java.util.List;import java.util.Properties;import java.util.Vector;import com.jcraft.jsch.Channel;import com.jcraft.jsch.ChannelSftp;import com.jcraft.jsch.ChannelSftp.LsEntry;import com.jcraft.jsch.JSch;import com.jcraft.jsch.Session;/** * SFTP客户端工具类 *  * @author jianggujin *  */public class SftpClientTool{   /** Sftp */   private ChannelSftp sftp = null;   /** 主机 */   private String host = "";   /** 端口 */   private int port = 22;   /** 用户名 */   private String username = "";   /** 密码 */   private String password = "";   /**    * 构造函数    *     * @param host    *           主机    * @param port    *           端口    * @param username    *           用户名    * @param password    *           密码    *     */   public SftpClientTool(String host, int port, String username, String password)   {      this.host = host;      this.port = port;      this.username = username;      this.password = password;   }   /**    * 构造函数    *     * @param host    *           主机    * @param username    *           用户名    * @param password    *           密码    *     */   public SftpClientTool(String host, String username, String password)   {      this.host = host;      this.username = username;      this.password = password;   }   /**    * 连接sftp服务器,默认超时时间20000    *     * @throws Exception    */   public void connect() throws Exception   {      connect(20000);   }   /**    * 连接sftp服务器    *     * @param timeout    *           超时时间    *     * @throws Exception    */   public void connect(int timeout) throws Exception   {      JSch jsch = new JSch();      Session sshSession = jsch.getSession(this.username, this.host, this.port);      sshSession.setPassword(password);      Properties sshConfig = new Properties();      sshConfig.put("StrictHostKeyChecking", "no");      sshSession.setConfig(sshConfig);      sshSession.connect(timeout);      Channel channel = sshSession.openChannel("sftp");      channel.connect();      this.sftp = (ChannelSftp) channel;   }   /**    * 断开sftp服务器连接    *     * @throws Exception    */   public void disconnect() throws Exception   {      if (this.sftp != null)      {         if (this.sftp.isConnected())         {            this.sftp.disconnect();         }         else if (this.sftp.isClosed())         {         }         this.sftp = null;      }   }   /**    * 上传单个文件    *     * @param directory    *           上传的目录    * @param uploadFile    *           要上传的文件    *     * @throws Exception    */   public void upload(String directory, String uploadFile) throws Exception   {      this.sftp.cd(directory);      File file = new File(uploadFile);      this.sftp.put(new FileInputStream(file), file.getName());   }   /**    * 下载单个文件    *     * @param directory    *           下载目录    * @param downloadFile    *           下载的文件    * @param saveFile    *           保存文件    *     * @throws Exception    */   public void download(String directory, String downloadFile, String saveFile)         throws Exception   {      this.sftp.cd(directory);      File file = new File(saveFile);      this.sftp.get(downloadFile, new FileOutputStream(file));   }   /**    * 删除文件    *     * @param directory    *           要删除文件所在目录    * @param deleteFile    *           要删除的文件    *     * @throws Exception    */   public void delete(String directory, String deleteFile) throws Exception   {      this.sftp.cd(directory);      this.sftp.rm(deleteFile);   }   /**    * 列出目录下的文件    *     * @param directory    *           要列出的目录    *     * @return list 文件名列表    *     * @throws Exception    */   @SuppressWarnings("unchecked")   public List<String> listFiles(String directory) throws Exception   {      List<String> fileNameList = new ArrayList<String>();      Vector<LsEntry> fileList = this.sftp.ls(directory);      Iterator<LsEntry> it = fileList.iterator();      while (it.hasNext())      {         String fileName = it.next().getFilename();         // 特殊文件名,忽略         if (".".equals(fileName) || "..".equals(fileName))         {            continue;         }         fileNameList.add(fileName);      }      return fileNameList;   }   /**    * 更改文件名    *     * @param directory    *           文件所在目录    * @param oldFileNm    *           原文件名    * @param newFileNm    *           新文件名    *     * @throws Exception    */   public void rename(String directory, String oldFileNm, String newFileNm)         throws Exception   {      this.sftp.cd(directory);      this.sftp.rename(oldFileNm, newFileNm);   }}
  • 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
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
  • 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
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218
  • 219
  • 220
  • 221
  • 222
  • 223
  • 224
  • 225
  • 226
原创粉丝点击