Studyon Minte9.com
Java SE




Sftp client




package testexample; import com.jcraft.jsch.*; import java.util.ArrayList; import java.util.List; import java.util.Vector; import javax.swing.*; public class Testexample { private static String javaVersion = "1.6"; public static String host; public static int port; public static String pass; public static String src; public static String dst; public static String cmd; public static Vector vector; public static String[] files; public static int filesno; public static String prefix; public static void main(String[] arg) { Testexample sftp = new Testexample(arg); if (cmd.equals("ls")) { sftp.getFilesno(); sftp.getFiles(); } } public Testexample(String[] arg) { // catalin@sftpIp 22 mypassword null null ls myprefix if (arg.length == 0) help(); if (arg.length >= 6) { host = arg[0]; port = new Integer(arg[1]); pass = arg[2]; src = arg[3]; dst = arg[4]; cmd = arg[5]; } if (arg.length >= 7) prefix = arg[6]; filesno = 0; try{ JSch jsch=new JSch(); String user=host.substring(0, host.indexOf('@')); host=host.substring(host.indexOf('@')+1); //int port=22; Session session=jsch.getSession(user, host, port); UserInfo ui = new M9MyUserInfo(pass); session.setUserInfo(ui); session.connect(); Channel channel=session.openChannel("sftp"); channel.connect(); ChannelSftp c=(ChannelSftp)channel; SftpProgressMonitor monitor=new MyProgressMonitor(); int mode=ChannelSftp.OVERWRITE; if (cmd.equals("put")) { c.put(src, dst, monitor, mode); } if (cmd.equals("get")) { c.get(dst, src, monitor, mode); } if (cmd.equals("ls")) { vector = c.ls("."); setFiles(); } session.disconnect(); } catch(Exception e) { System.out.println(e); } } private static void help() { System.out.println("--------------------"); System.out.println("Sftp for Java " + javaVersion); System.out.println("--------------------"); System.out.println("List: java -jar sftp.jar host port pass null null ls prefix"); System.out.println("Put: java -jar sftp.jar host port pass src dst put"); System.out.println("Get: java -jar sftp.jar host port pass src dst get"); System.exit(0); } public int getFilesno() { System.out.println("Files: " + filesno); return filesno; } public void setFiles() { List<String> fileList = new ArrayList<String>(); try{ int ii=0; for(ii=0; ii<vector.size(); ii++){ Object obj=vector.elementAt(ii); if(obj instanceof com.jcraft.jsch.ChannelSftp.LsEntry){ String fileName = ((com.jcraft.jsch.ChannelSftp.LsEntry)obj).getFilename(); if (prefix != null && ! fileName.startsWith(prefix)) continue; fileList.add(filesno++, fileName); } } } catch(Exception e){ System.out.println(e.toString()); } //ArrayList to String[] files = new String[fileList.size()]; files = fileList.toArray(files); } public String[] getFiles() { for(int i=0; i<files.length; i++) { System.out.println(files[i]); } return files; } public static class M9MyUserInfo implements UserInfo { String passwd; public M9MyUserInfo(String pass) { passwd = pass; } public String getPassword(){ return passwd; } public void showMessage(String string) {} public boolean promptPassphrase(String string) {return true;} public boolean promptYesNo(String str){ return true; } public boolean promptPassword(String message){ return true; } public String getPassphrase() {return "";} } public static class MyProgressMonitor implements SftpProgressMonitor{ ProgressMonitor monitor; long count=0; long max=0; public void init(int op, String src, String dest, long max){ this.max=max; monitor=new ProgressMonitor(null, ((op==SftpProgressMonitor.PUT)? "put" : "get")+": "+src, "", 0, (int)max); count=0; percent=-1; monitor.setProgress((int)this.count); monitor.setMillisToDecideToPopup(1000); } private long percent=-1; public boolean count(long count){ this.count+=count; if(percent>=this.count*100/max){ return true; } percent=this.count*100/max; monitor.setNote("Completed "+this.count+"("+percent+"%) out of "+max+"."); monitor.setProgress((int)this.count); return !(monitor.isCanceled()); } public void end(){ monitor.close(); } } }
 


http://www.jcraft.com/jsch/examples/Sftp.java
http://www.coreftp.com/server