Pada contoh yang akan saya berikan adalah contoh penggunaan authentikasi dengan password atau Public Private Key. Jadi bisa di remark saja salah satu baris untuk jenis authentikasi yang tidak digunakan.
Put File
public boolean uploadFile(String srcFileInLocal, String dstFileInServer){
Session session = null;
ChannelSftp channelSftp = null;
FileInputStream localFileStream = null;
boolean result= false;
try{
JSch jsch= new JSch();
// untuk penggunaan file PPK sebagai authentikasi
File ppkFile= new File("C:\key.ppk");
jsch.addIdentity(ppkFile.getAbsolutePath());
// username, server, dan port
session = jsch.getSession("root", "localhost", 22 );
// untuk penggunaan password sebagai authentikasi
session.setPassword("password");
session.setConfig("StrictHostKeyChecking", "no");
session.setTimeout(15000);
session.connect();
channelSftp = (ChannelSftp) session.openChannel("sftp");
channelSftp.connect();
File localFile = new File (srcFileInLocal);
if (localFile.exists()){
localFileStream = new FileInputStream(localFile);
channelSftp.put(localFileStream, dstFileInServer);
localFileStream.close();
result=true;
}else{
logger.warn("Local file not found " + localFile.getAbsolutePath());
}
}catch (Exception ex) {
logger.error(ex.getMessage(), ex);
result = false;
}finally{
if(channelSftp != null){
channelSftp.disconnect();
}
if(session != null){
session.disconnect();
}
}
return result;
}
Download File
public boolean downloadFile(String srcFileInServer, String dstFileInLocal){ Session session = null; ChannelSftp channelSftp = null; boolean result = false; try{ JSch jsch = new JSch(); // untuk penggunaan file PPK sebagai authentikasi File ppkFile = new File(
"C:\key.ppk"
); jsch.addIdentity(ppkFile.getAbsolutePath()); // username, server, dan port session = jsch.getSession("root", "localhost", 22 ); // untuk penggunaan password sebagai authentikasi session.setPassword("password"); session.setConfig("StrictHostKeyChecking", "no"); session.setTimeout(15000); session.connect(); channelSftp = (ChannelSftp) session.openChannel("sftp"); channelSftp.connect(); byte[] buffer = new byte[1024]; BufferedInputStream bis = new BufferedInputStream(channelSftp.get(srcFileInServer)); File newFile = new File(dstFileInLocal); FileOutputStream fos = new FileOutputStream(newFile); BufferedOutputStream bos = new BufferedOutputStream(fos); int readCount; while( (readCount = bis.read(buffer)) > 0) { System.out.println("Writing: " ); bos.write(buffer, 0, readCount); } bis.close(); bos.close(); result=true; }catch(Exception ex){ logger.error(ex.getMessage(), ex); result= false; }finally{ if(channelSftp != null){ channelSftp.disconnect(); } if(session != null){ session.disconnect(); } } return result; }
Delete File
public boolean deleteRemoteFile(String fielPathInServer){ Session session = null; ChannelSftp channelSftp = null; boolean result= false; try{ JSch jsch = new JSch(); // untuk penggunaan file PPK sebagai authentikasi File ppkFile= new File(
"C:\key.ppk"
); jsch.addIdentity(ppkFile.getAbsolutePath()); // username, server, dan port session = jsch.getSession(username, urlHost, Integer.parseInt(portHost) ); // untuk penggunaan password sebagai authentikasi session.setPassword("password"); session.setConfig("StrictHostKeyChecking", "no"); session.setTimeout(15000); session.connect(); channelSftp = (ChannelSftp) session.openChannel("sftp"); channelSftp.connect(); channelSftp.rm(fielPathInServer); result= true; }catch(Exception ex){ logger.error(ex.getMessage(), ex); result = false; }finally{ if(channelSftp != null){ channelSftp.disconnect(); } if(session != null){ session.disconnect(); } } return result; }
Get List File
public List<String> getSFTPListFile(String remoteDir){ List<String> listFiles = null; Session session = null; ChannelSftp channelSftp = null; try{ JSch jsch= new JSch(); // untuk penggunaan file PPK sebagai authentikasi File ppkFile= new File(
"C:\key.ppk"
); jsch.addIdentity(ppkFile.getAbsolutePath()); // username, server, dan port session = jsch.getSession(username, urlHost, Integer.parseInt(portHost) ); // untuk penggunaan password sebagai authentikasi session.setPassword("password"); session.setConfig("StrictHostKeyChecking", "no"); session.setTimeout(15000); session.connect(); channelSftp = (ChannelSftp) session.openChannel("sftp"); channelSftp.connect(); Vector<LsEntry> listEntry= channelSftp.ls(remoteDir); listFiles= new ArrayList<String>(); for(LsEntry lsEntry : listEntry){ if(!lsEntry.getAttrs().isDir()){ listFiles.add(lsEntry.getFilename()); } } }catch (Exception e) { logger.error(e.getMessage(), e); }finally{ if(channelSftp != null){ channelSftp.disconnect(); } if(session != null){ session.disconnect(); } } return listFiles; }
No comments:
Post a Comment