package rservice;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
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.ChannelSftp.LsEntry;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;
import com.jcraft.jsch.SftpException;
public class SFTP {
private String host;
private String username;
private String password;
private int port;
private ChannelSftp sftp;
public SFTP(String host,int port,String username,String password){
this.host=host;
this.port=port;
this.username=username;
this.password=password;
}
public void connect(){
try{
JSch jsch=new JSch();
Session session=jsch.getSession(username, host, port);
session.setPassword(password);
Properties config=new Properties();
config.put("StrictHostKeyChecking", "no");
session.setConfig(config);
session.connect();
Channel channel=session.openChannel("sftp");
channel.connect();
this.sftp=(ChannelSftp)channel;
}catch(Exception ex){
ex.printStackTrace();
}
}
public void disConnect(){
if(this.sftp!=null){
if(this.sftp.isConnected()){
this.sftp.disconnect();
}
}
}
public void upload(String localfile){
try {
File file=new File(localfile);
if(file.isFile()){
this.sftp.put(new FileInputStream(file), file.getName());
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SftpException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}
this.disConnect();
}
public boolean upload(String localfile,String ramotePath){
try {
this.sftp.cd(ramotePath);
File file=new File(localfile);
if(file.isFile()){
this.sftp.put(new FileInputStream(file), file.getName());
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
} catch (SftpException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}catch(Exception e){
e.printStackTrace();
return false;
}
this.disConnect();
return true;
}
public void download(String localFile,String remotefile){
try {
File file=new File(localFile);
this.sftp.get(remotefile,new FileOutputStream(file));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SftpException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}
this.disConnect();
}
public List<String> getFileList(String suffix,String remotePath){
List<String> list=new ArrayList<String>();
try {
Vector<?> vector=this.sftp.ls(remotePath);
if(vector!=null){
for(Object o:vector){
if(o instanceof LsEntry){
String name=((LsEntry)o).getFilename();
if(name.split(".")[1].toUpperCase().equals(suffix.toUpperCase())){
list.add(name);
}
}
}
}
} catch (SftpException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch(Exception e){
e.printStackTrace();
}
this.disConnect();
return list;
}
public static void main(String args[]){
SFTP s=new SFTP("IP",21,"用戶名","密碼");
s.connect();
//s.upload("D:\\EDM\\TEST.csv","ramotePath");
s.download("D:\\EDM\\TEST.CSV", "TEST.CSV");
}
}
JAVA SFTP上傳下載
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.