java - How to browse subdirectories at ftp server -
i building swing ftp client.
here code jframe :-
package jframe; import jframe.swing.download.*; import org.apache.commons.net.ftp.ftpclient; import org.apache.commons.net.ftp.ftpfile; import test.ftpclient; import javax.swing.*; import javax.swing.table.tablemodel; import javax.swing.tree.defaultmutabletreenode; import javax.swing.tree.defaulttreemodel; import java.awt.event.*; import java.awt.*; import java.io.ioexception; import java.util.arraylist; import java.util.enumeration; import javax.swing.tree.defaultmutabletreenode; public class undecoratedframedemo { private static point point = new point(); arraylist arraylist; static string server = ""; static int port = 21; static string user = ""; static string pass = ""; jpopupmenu popup; public undecoratedframedemo() { final ftp_by_apache ftpbyapache=new ftp_by_apache(server,user,pass); arraylist=ftpbyapache.getallfile("/"); final jframe frame = new jframe("ftp client mpst"); frame.setundecorated(false); frame.addmouselistener(new mouseadapter() { public void mousepressed(mouseevent e) { point.x = e.getx(); point.y = e.gety(); } }); frame.addmousemotionlistener(new mousemotionadapter() { public void mousedragged(mouseevent e) { point p = frame.getlocation(); frame.setlocation(p.x + e.getx() - point.x, p.y + e.gety() - point.y); } }); frame.setsize(700, 500); frame.setlocation(300, 150); frame.setlayout(new borderlayout()); image icon = new javax.swing.imageicon("ftp-big-icon.jpg").getimage(); frame.seticonimage(icon); container pane=frame.getcontentpane(); pane.setlayout(new borderlayout()); jmenubar menubar = new jmenubar(); jmenu file = new jmenu("file"); menubar.add(file); jmenuitem new = new jmenuitem("new server"); new.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent e) { /*code new ftp server*/ } }); file.add(new); jmenuitem rename = new jmenuitem("rename"); new.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent e) { /*code rename*/ } }); file.add(rename); jmenuitem delete = new jmenuitem("delete"); new.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent e) { /*code delete*/ } }); file.add(delete); jmenuitem connect = new jmenuitem("connect"); new.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent e) { /*code connect differnt ftp*/ } }); file.add(connect); jmenuitem item = new jmenuitem("exit"); item.addactionlistener(new actionlistener(){ @override public void actionperformed(actionevent e) { system.exit(0); } }); file.add(item); jmenu edit = new jmenu("edit"); menubar.add(edit); jmenuitem copy = new jmenuitem("copy"); item.addactionlistener(new actionlistener(){ @override public void actionperformed(actionevent e) { /* code copy*/ } }); edit.add(copy); jmenuitem paste = new jmenuitem("paste"); item.addactionlistener(new actionlistener(){ @override public void actionperformed(actionevent e) { /* code paste*/ } }); edit.add(paste); jmenuitem mkdir = new jmenuitem("new directory"); new.addactionlistener(new actionlistener(){ @override public void actionperformed(actionevent e) { system.exit(0); } }); file.add(mkdir); popup = new jpopupmenu(); jmenuitem popupcopy= new jmenuitem("copy"); popup.add(popupcopy ); jmenuitem popuprename= new jmenuitem("rename"); popup.add( popuprename ); jmenuitem popupdelete= new jmenuitem("delete"); popup.add( popupdelete ); jmenuitem menuitem = new jmenuitem("open"); menuitem.addactionlistener(new actionlistener(){ public void actionperformed(actionevent event){ component c = (component)event.getsource(); jpopupmenu popup = (jpopupmenu)c.getparent(); jtable table = (jtable)popup.getinvoker(); system.out.println(table.getselectedrow() + " : " + table.getselectedcolumn()); if(table.getvalueat(table.getselectedrow(),table.getselectedcolumn()+1).equals("file folder")) { system.out.println("file folder clicked"); swingutilities.updatecomponenttreeui(frame); } else { try { ftpbyapache.download(table.getvalueat(table.getselectedrow(),table.getselectedcolumn()).tostring(),"x:/"); } catch (exception e) { e.printstacktrace(); } } } }); popup.add(menuitem); filetablemodel filetablemodel =new filetablemodel(arraylist) ; jtable table = new jtable(filetablemodel) { public boolean iscelleditable(int data, int column) { return false; } }; table.addmouselistener(new mouseadapter() { public void mousepressed(mouseevent e) { system.out.println("pressed"); } public void mousereleased(mouseevent e) { if (e.ispopuptrigger()) { jtable source = (jtable) e.getsource(); int row = source.rowatpoint(e.getpoint()); int column = source.columnatpoint(e.getpoint()); if (!source.isrowselected(row)) source.changeselection(row, column, false, false); popup.show(e.getcomponent(), e.getx(), e.gety()); } } }); table.setpreferredscrollableviewportsize(table.getpreferredsize()); jscrollpane scrollpane = new jscrollpane(table); frame.getcontentpane().add(scrollpane, borderlayout.center); frame.setjmenubar(menubar); frame.setvisible(true); } public static void main(string[] args) { new undecoratedframedemo(); } public string[] getdir(string dir){ //gets of directories given directory , returns them string array string[] directories; try{ ftpclient ftpclient = new ftpclient(); ftpfile[] objects = ftpclient.listdirectories(dir); ftpclient.connect(server, port); ftpclient.login(user, pass); ftpclient.enterlocalpassivemode(); ftpclient.setfiletype(ftpclient.binary_file_type); directories = new string[objects.length]; for(int i=0; i<objects.length; i++){ directories[i]=objects[i].getname(); } return directories; } catch (ioexception ex) { string[] dir1={}; system.out.println("error"); return dir1; } } }
and here ftp class code
package jframe; import java.io.fileinputstream; import java.io.filenotfoundexception; import java.io.fileoutputstream; import java.io.ioexception; import java.io.inputstream; import java.io.outputstream; import java.text.dateformat; import java.text.parseexception; import java.text.simpledateformat; import java.util.arraylist; import java.util.date; import java.util.list; import javax.imageio.stream.fileimageinputstream; import org.apache.commons.net.*; import org.apache.commons.net.ftp.ftpclient; import org.apache.commons.net.ftp.ftpfile; public class ftp_by_apache { ftpclient f = null; // public ftp_by_apache(string url, string username, string password) { f = new ftpclient(); // f.enterlocalpassivemode(); this.get_connection(url, username, password); } public string printtime(string time) { simpledateformat dateformat = new simpledateformat("ddmmyyyyhhmmss"); try { string timepart = time.split(" ")[1]; date modificationtime = dateformat.parse(timepart); system.out.println("file modification time: " + modificationtime); return modificationtime.tostring(); } catch (parseexception ex) { ex.printstacktrace(); return "undefined"; } } // public void get_connection(string url, string username, string password) { try { f.connect(url); system.out.println("connect success!"); f.setcontrolencoding("gbk"); boolean login = f.login(username, password); if (login) system.out.println("logged in"); else system.out.println("login failed!"); } catch (ioexception e) { e.printstacktrace(); } } public void close_connection() { boolean logout = false; try { logout = f.logout(); } catch (ioexception e1) { // todo auto-generated catch block e1.printstacktrace(); } if (logout) { system.out.println("log out successfull!"); } else { system.out.println("try again!"); } if (f.isconnected()) try { system.out.println("connection alive!"); f.disconnect(); } catch (ioexception e) { e.printstacktrace(); } } // public arraylist getallfile(string dir) { f.enterlocalpassivemode(); /* try { f.changeworkingdirectory(dir); } catch (exception e) { e.printstacktrace(); }*/ dateformat dateformater = new simpledateformat("dd/mm/yyyy hh:mm:ss"); arraylist<arraylist> arraylist= new arraylist<arraylist>(); ftpfile[] files = null; try { files = f.listfiles(); if(f.isavailable()) { system.out.println("connected"); } system.out.println("length: "+files.length); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } (ftpfile file : files) { arraylist arraylist1=new arraylist(); arraylist1.add(file.getname()); if (file.isdirectory()) { arraylist1.add("file folder"); } else if (file.getname().endswith(".exe")) { arraylist1.add("application"); } else if (file.getname().endswith(".zip")) { arraylist1.add("compressed (zip folder)"); } else if (file.getname().endswith(".xls")||file.getname().endswith(".xlsx")||file.getname().endswith(".doc")||file.getname().endswith(".docx")||file.getname().endswith(".rtf")) { arraylist1.add("microsoft office document"); } else if (file.getname().endswith(".sql")) { arraylist1.add("sql file"); } else if (file.getname().endswith(".txt")) { arraylist1.add("text document"); } else if (file.getname().endswith(".pdf")) { arraylist1.add("pdf document"); } else { arraylist1.add("file"); } arraylist1.add(dateformater.format(file.gettimestamp().gettime())); arraylist1.add(file.getsize()+" kb"); arraylist.add(arraylist1); if(file.isdirectory()) system.out.println(file.getname()+": directory"); if(file.isfile()) system.out.println(file.getname()+": file"); } system.out.println(arraylist); return arraylist; } // public void upload(string file_path) throws ioexception { inputstream input = null; string[] file_name = null; try { input = new fileinputstream(file_path); file_name = file_path.split("\\\\"); system.out.println(file_name[file_name.length - 1]); } catch (filenotfoundexception e) { // todo auto-generated catch block e.printstacktrace(); } system.out.println(file_name[file_name.length - 1]); f.storefile(file_name[file_name.length - 1], input); system.out.println("file uploaded successfully"); if (input != null) input.close(); } public void download(string from_file_name, string to_path) throws ioexception { outputstream output = null; try { output = new fileoutputstream(to_path + from_file_name); } catch (filenotfoundexception e) { // todo auto-generated catch block e.printstacktrace(); } f.retrievefile(from_file_name, output); if (output != null) { try { if (output != null) output.close(); } catch (ioexception e) { // todo auto-generated catch block e.printstacktrace(); } } } }
and output
these directories of ftp server want browse subdirectories when parent directory clicked, want add progressbar file download can help??
i have solved problem cunstructor chaining.
here second constructor
public undecoratedframedemo(final string childdir,ftp_by_apache ftpbyapache2) { final ftp_by_apache ftpbyapache12=ftpbyapache2; arraylist1=ftpbyapache12.getsublfile(childdir); system.out.println("initial directory: " + childdir); final jframe frame = new jframe("ftp client mpst"); frame.setundecorated(false); frame.addmouselistener(new mouseadapter() { public void mousepressed(mouseevent e) { point.x = e.getx(); point.y = e.gety(); } }); frame.addmousemotionlistener(new mousemotionadapter() { public void mousedragged(mouseevent e) { point p = frame.getlocation(); frame.setlocation(p.x + e.getx() - point.x, p.y + e.gety() - point.y); } }); frame.setsize(700, 500); frame.setlocation(300, 150); frame.setlayout(new borderlayout()); image icon = new javax.swing.imageicon("ftp-big-icon.jpg").getimage(); frame.seticonimage(icon); container pane=frame.getcontentpane(); pane.setlayout(new borderlayout()); jmenubar menubar = new jmenubar(); jmenu file = new jmenu("file"); menubar.add(file); jmenuitem new = new jmenuitem("new server"); new.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent e) { /*code new ftp server*/ } }); file.add(new); jmenuitem rename = new jmenuitem("rename"); new.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent e) { /*code rename*/ } }); file.add(rename); jmenuitem delete = new jmenuitem("delete"); new.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent e) { /*code delete*/ } }); file.add(delete); jmenuitem connect = new jmenuitem("connect"); new.addactionlistener(new actionlistener() { @override public void actionperformed(actionevent e) { /*code connect differnt ftp*/ } }); file.add(connect); jmenuitem item = new jmenuitem("exit"); item.addactionlistener(new actionlistener(){ @override public void actionperformed(actionevent e) { system.exit(0); } }); file.add(item); jmenu edit = new jmenu("edit"); menubar.add(edit); jmenuitem copy = new jmenuitem("copy"); item.addactionlistener(new actionlistener(){ @override public void actionperformed(actionevent e) { /* code copy*/ } }); edit.add(copy); jmenuitem paste = new jmenuitem("paste"); item.addactionlistener(new actionlistener(){ @override public void actionperformed(actionevent e) { /* code paste*/ } }); edit.add(paste); jmenuitem mkdir = new jmenuitem("new directory"); new.addactionlistener(new actionlistener(){ @override public void actionperformed(actionevent e) { system.exit(0); } }); file.add(mkdir); popup = new jpopupmenu(); jmenuitem popupcopy= new jmenuitem("copy"); popup.add(popupcopy ); jmenuitem popuprename= new jmenuitem("rename"); popup.add( popuprename ); jmenuitem popupdelete= new jmenuitem("delete"); popup.add( popupdelete ); jmenuitem menuitem = new jmenuitem("open"); menuitem.addactionlistener(new actionlistener(){ public void actionperformed(actionevent event){ component c = (component)event.getsource(); jpopupmenu popup = (jpopupmenu)c.getparent(); jtable table = (jtable)popup.getinvoker(); system.out.println(table.getselectedrow() + " : " + table.getselectedcolumn()); if(table.getvalueat(table.getselectedrow(),table.getselectedcolumn()+1).equals("file folder")) { system.out.println("file folder clicked"); new undecoratedframedemo(childdir+"/"+table.getvalueat(table.getselectedrow(),table.getselectedcolumn()).tostring(),ftpbyapache12); // frame.add(table1); frame.hide(); } else { try { ftpbyapache12.download(table.getvalueat(table.getselectedrow(),table.getselectedcolumn()).tostring(),"x:/"); } catch (exception e) { e.printstacktrace(); } } } }); popup.add(menuitem); filetablemodel1 filetablemodel1 =new filetablemodel1(arraylist1) ; jtable table = new jtable(filetablemodel1) { public boolean iscelleditable(int data, int column) { return false; } }; table.addmouselistener(new mouseadapter() { public void mousepressed(mouseevent e) { system.out.println("pressed"); } public void mousereleased(mouseevent e) { if (e.ispopuptrigger()) { jtable source = (jtable) e.getsource(); int row = source.rowatpoint(e.getpoint()); int column = source.columnatpoint(e.getpoint()); if (!source.isrowselected(row)) source.changeselection(row, column, false, false); popup.show(e.getcomponent(), e.getx(), e.gety()); } } }); table.setpreferredscrollableviewportsize(table.getpreferredsize()); jscrollpane scrollpane = new jscrollpane(table); frame.getcontentpane().add(scrollpane, borderlayout.center); // jfilepicker filepicker = new jfilepicker("save file to: ","browse..."); // frame.getcontentpane().add(filepicker); frame.setjmenubar(menubar); frame.setvisible(true); }
Comments
Post a Comment