上一节小编介绍了:Java FTP文件上传客户端编写教程,因为考虑到我们的系统搭建FTP服务器端比较麻烦,再者现在大部分使用的是Linux服务器,而Linux服务器本身就自带了SFTP功能,就不需要我们额外的再搭建FTP来占用我们的Linux服务器资源了,非常的方便。
SFTP是一个Secure File Transfer Protocol安全文件传送协议,用来做文件的上传下载非常的方便,所以今天小编就用Java来编写了一个Java SFTP客户端,用于连接SFTP服务器,并且实现文件的上传下载功能,很简单!
步骤一:加入com.jcraft.jsch.JSch.jar包,这个2016年已经停止更新,因为版本已经非常稳定了,小编使用maven来引入JSch包,代码如下:
<dependency> <groupId>com.jcraft</groupId> <artifactId>jsch</artifactId> <version>0.1.54</version> </dependency>
步骤二:实现Java SFTP文件上传下载功能,小编新建了一个SFTPExample类,代码如下:
package demo;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.OutputStream;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
public class SFTPExample {
private String host;
private Integer port;
private String user;
private String password;
private JSch jsch;
private Session session;
private Channel channel;
private ChannelSftp sftpChannel;
public SFTPExample(String host, Integer port, String user, String password) {
this.host = host;
this.port = port;
this.user = user;
this.password = password;
}
//连接SFTP服务器
public void connect() {
System.out.println("connecting..."+host);
try {
jsch = new JSch();
session = jsch.getSession(user, host,port);
session.setConfig("StrictHostKeyChecking", "no");
session.setPassword(password);
session.connect();
channel = session.openChannel("sftp");
channel.connect();
sftpChannel = (ChannelSftp) channel;
} catch (JSchException e) {
e.printStackTrace();
}
}
//端口SFTP连接
public void disconnect() {
System.out.println("disconnecting...");
sftpChannel.disconnect();
channel.disconnect();
session.disconnect();
}
//文件上传
//参数一:需要上传的文件路径
//参数二:Linux服务器目录
public void uploadFile(String localFileName, String remoteDir) {
FileInputStream fis = null;
connect();
try {
// Change to output directory
sftpChannel.cd(remoteDir);
// Upload file
File file = new File(localFileName);
fis = new FileInputStream(file);
sftpChannel.put(fis, file.getName());
fis.close();
System.out.println("File uploaded successfully - "+ file.getAbsolutePath());
} catch (Exception e) {
e.printStackTrace();
}
disconnect();
}
//文件下载
//参数一:远程文件路径
//参数二:本地文件夹目录
public void downloadFile(String romoteFileName, String localDir) {
byte[] buffer = new byte[1024];
BufferedInputStream bis;
connect();
try {
// Change to output directory
String cdDir = romoteFileName.substring(0, romoteFileName.lastIndexOf("/") + 1);
sftpChannel.cd(cdDir);
File file = new File(romoteFileName);
bis = new BufferedInputStream(sftpChannel.get(file.getName()));
File newFile = new File(localDir + "/" + file.getName());
// Download file
OutputStream os = new FileOutputStream(newFile);
BufferedOutputStream bos = new BufferedOutputStream(os);
int readCount;
while ((readCount = bis.read(buffer)) > 0) {
bos.write(buffer, 0, readCount);
}
bis.close();
bos.close();
System.out.println("File downloaded successfully - "+ file.getAbsolutePath());
} catch (Exception e) {
e.printStackTrace();
}
disconnect();
}
//测试
public static void main(String[] args) {
String localPath = "D:/DT940/";
String remotePath = "/root/";
SFTPExample ftp = new SFTPExample("192.168.1.118", 22, "root", "root");
ftp.uploadFile(localPath+"test.txt", remotePath);
ftp.downloadFile(remotePath+"test.txt", localPath);
}
}备注:其实SFTP用来做文件上传下载会比HTTP的形式简单很多,在移动设备中会有很广泛的应用,特别是物流行业的巴枪扫描上传数据这一块,特别重要。