使用Jsch可以很容易地实现sftp文件上传。具体代码如下:
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;
import java.util.Vector;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
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.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException;
public class SftpHelper extends Thread {
private String host;
private String username;
private String password;
private String location;
private int port;
private String knowHosts;
private String username;
private String password;
private String location;
private int port;
private String knowHosts;
private String osName;
private List filenames = new ArrayList();
public SftpHelper(String host, String username, String password, int port) {
this(host, username, password, port, "");
}
this(host, username, password, port, "");
}
public SftpHelper(String host, String username, String password, int port,
String location) {
this.host = host;
this.username = username;
this.password = password;
this.port = port;
osName = System.getProperty("os.name");
System.out.println(osName);
if (osName.toUpperCase().indexOf("WINDOWS") > -1) {
//Windows OS
} else {
//Linux OS
}
this.location = location;
}
String location) {
this.host = host;
this.username = username;
this.password = password;
this.port = port;
osName = System.getProperty("os.name");
System.out.println(osName);
if (osName.toUpperCase().indexOf("WINDOWS") > -1) {
//Windows OS
} else {
//Linux OS
}
this.location = location;
}
public void addFilename(String filename) {
filenames.add(filename);
}
filenames.add(filename);
}
public void setFilenames(List filenames) {
this.filenames = filenames;
}
this.filenames = filenames;
}
public void run() {
upload();
}
upload();
}
/**
* 要上传的文件必须包含完整的路径
*
*/
public boolean upload() {
if (filenames.size() == 0)
return false;
Session session;
Channel channel;
JSch jsch = new JSch();
try {
session = jsch.getSession(username, host, port);
session.setPassword(password);
Properties prop = new Properties();
prop.setProperty("StrictHostKeyChecking", "no");// StrictHostKeyChecking:
// ask | yes | no
session.setConfig(prop);
session.connect();
channel = session.openChannel("sftp");
channel.connect();
ChannelSftp c = (ChannelSftp) channel;
c.cd(location);
* 要上传的文件必须包含完整的路径
*
*/
public boolean upload() {
if (filenames.size() == 0)
return false;
Session session;
Channel channel;
JSch jsch = new JSch();
try {
session = jsch.getSession(username, host, port);
session.setPassword(password);
Properties prop = new Properties();
prop.setProperty("StrictHostKeyChecking", "no");// StrictHostKeyChecking:
// ask | yes | no
session.setConfig(prop);
session.connect();
channel = session.openChannel("sftp");
channel.connect();
ChannelSftp c = (ChannelSftp) channel;
c.cd(location);
InputStream in = null;
OutputStream out = null;
for (int i = 0; i < filenames.size(); i++) {
String filename = filenames.get(i);
if (filename == null || "".equals(filename)) {
// 当前没有要上传的文件
System.out.println("当前没有要上传的文件");
continue;
}
int idx = filename.lastIndexOf(File.separator);
OutputStream out = null;
for (int i = 0; i < filenames.size(); i++) {
String filename = filenames.get(i);
if (filename == null || "".equals(filename)) {
// 当前没有要上传的文件
System.out.println("当前没有要上传的文件");
continue;
}
int idx = filename.lastIndexOf(File.separator);
String uploadname = filename.substring(idx == -1 ? 0 : idx + 1);
System.out.println(uploadname);
out = c.put(uploadname);
System.out.println(uploadname);
out = c.put(uploadname);
sleep(5000);
in = new FileInputStream(filename);
byte[] b = new byte[1024];
int n;
while ((n = in.read(b)) != -1) {
out.write(b);
}
}
out.flush();
out.close();
in.close();
c.disconnect();
session.disconnect();
sleep(500);
return true;
} catch (JSchException e) {
System.out.println("JSchException");
e.printStackTrace();
} catch (SftpException e) {
System.out.println("SftpException");
e.printStackTrace();
} catch (IOException e) {
System.out.println("IOException");
e.printStackTrace();
} catch (InterruptedException e) {
System.out.println("InterruptedException");
e.printStackTrace();
}
return false;
}
int n;
while ((n = in.read(b)) != -1) {
out.write(b);
}
}
out.flush();
out.close();
in.close();
c.disconnect();
session.disconnect();
sleep(500);
return true;
} catch (JSchException e) {
System.out.println("JSchException");
e.printStackTrace();
} catch (SftpException e) {
System.out.println("SftpException");
e.printStackTrace();
} catch (IOException e) {
System.out.println("IOException");
e.printStackTrace();
} catch (InterruptedException e) {
System.out.println("InterruptedException");
e.printStackTrace();
}
return false;
}
public static void main(String[] args) {
String username = "kary";
String host = "localhost";
int port = 22;
String password = "123";
String path = "/sftphelper/";
SftpHelper helper = new SftpHelper(host, username, password, port, path);
helper.addFilename("D:\\a.txt");
helper.addFilename("D:\\b.txt");
helper.start();
}
}
String username = "kary";
String host = "localhost";
int port = 22;
String password = "123";
String path = "/sftphelper/";
SftpHelper helper = new SftpHelper(host, username, password, port, path);
helper.addFilename("D:\\a.txt");
helper.addFilename("D:\\b.txt");
helper.start();
}
}
附件中有一个Jsch包,和一个mini-sftp-server。