sftp 上传文件

下载jsch-0.1.46.zip.

 

 下载

引用:http://www.xfok.net/2009/10/124485.html

public class MySFTP {
Logger log=Logger.getLogger(this.getClass());
/**
* 连接服务器
* 
* @param host
* 主机ip
* @param port
* 服务器端口
* @param username
* 用户名
* @param password
* 密码
* @return
*/
ChannelSftp sftp = null;
public boolean connect(String host, int port, String username,
String password) {
boolean b=true;
try {
JSch jsch = new JSch();
jsch.getSession(username, host, port);
Session sshSession = jsch.getSession(username, host, port);
//     log.debug("Session created.");
sshSession.setPassword(password);
Properties sshConfig = new Properties();
sshConfig.put("StrictHostKeyChecking", "no");
sshSession.setConfig(sshConfig);
sshSession.connect();
//     log.debug("Session connected.");
//     log.debug("Opening Channel.");
Channel channel = sshSession.openChannel("sftp");
channel.connect();
sftp = (ChannelSftp) channel;
log.info("Connected to " + host + ".");
} catch (Exception e) {
log.error("e--"+e);
b=false;
}
return b;

}

/**
* 通过sftp上传
* 
* @param directory
* 上传的目标目录
* @param uploadFile
* 被上传的文件
* @param sftp
* @throws SftpException 
* @throws FileNotFoundException 
*/
public void upload(String directory, String uploadFile) throws SftpException, FileNotFoundException {
sftp.cd(directory);
File file = new File(uploadFile);
FileInputStream fis=new FileInputStream(file);
sftp.put(fis, file.getName());
try {
fis.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

/**
* 下载文件
* 
* @param directory
* 文件所在目录
* @param downloadFile
* 需要下载的文件
* @param saveFile
* 保存为本地文件
* @param sftp
*/
public void download(String directory, String downloadFile,
String saveFile) {
FileOutputStream fos=null;
try {
sftp.cd(directory);
File file = new File(saveFile);
fos=new FileOutputStream(file);
sftp.get(downloadFile, fos);
} catch (Exception e) {
e.printStackTrace();
}finally{
if(fos!=null){
try {
fos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}

/**
* 删除服务器上的文件
* 
* @param directory
* 需要被删除的文件所在的目录
* @param deleteFile
* 需要被删除的文件
* @param sftp
*/
public void delete(String directory, String deleteFile) {
try {
sftp.cd(directory);
sftp.rm(deleteFile);
} catch (Exception e) {
e.printStackTrace();
}
}

/**
* 列出目录下的文件
* 
* @param directory
* 目标目录
* @param sftp
* @return
* @throws SftpException
*/
public Vector<?> listFiles(String directory)
throws SftpException {
return sftp.ls(directory);
}
/**
* Disconnect with server
* @param id 
*/
public void disconnect() {
if(this.sftp != null){
if(this.sftp.isConnected()){
this.sftp.disconnect();
// log.info("disconnect from the host! EventId:"+id);
}else if(this.sftp.isClosed()){
log.info("sftp is already closed");
}
}

}
public static void main(String[] args) {
/*     MySFTP sf = new MySFTP();
String host = "localhost";
int port = 22;
String username = "root";
String password = "root";
String directory = "/home/siya/ftp/";
String uploadFile = "/home//siya/test/localfile.gz";
sf.connect(host, port, username, password);
String[] file={"a","b","c"};

for(int i=0;i<file.length;i++){
sf.upload(directory, file[i]);
}
sf.disconnect();


}
}
posted @ 2012-06-06 11:20  eyotata  阅读(873)  评论(1)    收藏  举报