这几天想写个上传下载的java工具,在网上找了半天都不太好用。
自己整了个小代码,可以上传下载多个文件。路径用“\\”指定。
需要jsch-0.1.8.jar
package jp.co.nri.kinshasa.util;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.StringTokenizer;
import java.util.Vector;
import com.jcraft.jsch.Channel;
import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.ChannelSftp;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException;
import com.jcraft.jsch.UserInfo;
public class Ssh2Util {
private static ArrayList filelist;
private String host;
private String user;
private String password;
private int port;
private Session session;
private File[] listFiles;
public Ssh2Util(String host, String user, String password, int port) {
this.host = host;
this.user = user;
this.password = password;
this.port = port;
}
public void initialSession() throws Exception {
if (session == null) {
JSch jsch = new JSch();
session = jsch.getSession(user, host, port);
session.setUserInfo(new UserInfo() {
public String getPassphrase() {
return null;
}
public String getPassword() {
return null;
}
public boolean promptPassword(String arg0) {
return false;
}
public boolean promptPassphrase(String arg0) {
return false;
}
public boolean promptYesNo(String arg0) {
return true;
}
public void showMessage(String arg0) {
}
});
session.setPassword(password);
session.connect();
}
}
/**
* 关闭连接
*
* @throws Exception
*/
public void close() throws Exception {
if (session != null && session.isConnected()) {
session.disconnect();
session = null;
}
}
public void putFile2(String localPath, String localFile, String remotePath,ChannelSftp c)
throws Exception {
c.put(localPath+"\\"+localFile, remotePath);
}
public void download(String directory, String downloadFile,
String saveFile, ChannelSftp sftp) {
try {
sftp.cd(directory);
File file = new File(saveFile);
sftp.get(downloadFile, new FileOutputStream(file));
} catch (Exception e) {
e.printStackTrace();
}
}
public void getFile(String directory, String downloadFile, String saveFile,
ChannelSftp sftp) throws SftpException {
sftp.get(directory + "/" + downloadFile, saveFile);
}
public void dirPath(String directory, String downloadFile, String saveFile,
ChannelSftp sftp) throws SftpException {
Vector v = sftp.ls(directory);
for (Iterator iterator = v.iterator(); iterator.hasNext();) {
Object object = (Object) iterator.next();
System.out.println(isDirectoryName(object.toString()));
System.out.println(getLastTokenName(object.toString()));
}
}
public String getLastTokenName(String str) throws SftpException {
String directoryName=null;
// ArrayList tokenList=new ArrayList();
StringTokenizer st=new StringTokenizer(str," ");
while(st.hasMoreTokens()){
// System.out.println(st.nextToken());
directoryName=st.nextToken();
// tokenList.add(st.nextToken());
}
return directoryName ;
}
public boolean isDirectoryName(String str) throws SftpException {
if(str.startsWith("d")){
return true;
}
return false;
}
public void downloadFiles(String remoteDirectory, String localDirectory ) throws Exception{
initialSession();
Channel channelSftp = getSession().openChannel("sftp");
channelSftp.connect();
ChannelSftp sftp= (ChannelSftp) channelSftp;
Vector v = sftp.ls(remoteDirectory);
String str;
for (Iterator iterator = v.iterator(); iterator.hasNext();) {
Object object = (Object) iterator.next();
str=getLastTokenName(object.toString());
if((!str.equals("."))&&(!str.equals(".."))){
if(isDirectoryName(object.toString())){
File file=new File(localDirectory+"/"+str);
file.mkdir();
downloadFiles(remoteDirectory+"/"+str,localDirectory+"/"+str);
}else{
getFile(remoteDirectory, str, localDirectory, sftp);
System.out.println(object.toString());
}
}
}
channelSftp.disconnect();
}
public void uploadFiles2( String localDirectory ,String remoteDirectory) throws Exception{
initialSession();
Channel channelSftp = getSession().openChannel("sftp");
channelSftp.connect();
ChannelSftp sftp= (ChannelSftp) channelSftp;
File file=new File(localDirectory);
listFiles = file.listFiles();
for (File f : listFiles) {
System.out.println(f.getName());
if(f.isDirectory()){
uploadFiles2(localDirectory+"\\"+f.getName() , remoteDirectory+"/"+f.getName());
}else{
putFile2(localDirectory,f.getName(), remoteDirectory,sftp);
}
}
channelSftp.disconnect();
}
public String getHost() {
return host;
}
public void setHost(String host) {
this.host = host;
}
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int getPort() {
return port;
}
public void setPort(int port) {
this.port = port;
}
public Session getSession() {
return session;
}
public void setSession(Session session) {
this.session = session;
}
}