jftp.java

来自「java ftp 操作代码,程序可以直接运行」· Java 代码 · 共 1,478 行 · 第 1/3 页

JAVA
1,478
字号
        desktop.remove(background);        addBackgroundImage();        validate();    }    public void addBackgroundImage()    {        try        {            background = new HDesktopBackground(Settings.background, null);            background.setBounds(0, 0, getSize().width, getSize().height);            desktop.add(background, new Integer(Integer.MIN_VALUE));        }        catch(Exception ex)        {            Log.out(Settings.background + " missing, no background image used");        }    }    private static void compile()    {        /*        try        {               Compiler.enable();           System.out.println(Compiler.compileClass(Class.forName("java.util.Hashtable")));           System.out.println(Compiler.compileClasses("net.sf.jftp"));           System.out.println(Compiler.compileClasses("net.sf.jftp.JFtp"));           System.out.println(Compiler.compileClasses("JFtp"));         }         catch(ClassNotFoundException ex)         {                 ex.printStackTrace();         }         */    }    private static void setSocksProxyOptions(String proxy, String port)    {        if(proxy.equals("") || port.equals(""))        {            return;        }        java.util.Properties sysprops = System.getProperties();        // Remove previous values        sysprops.remove("socksProxyHost");        sysprops.remove("socksProxyPort");        // Set your values        sysprops.put("socksProxyHost", proxy);        sysprops.put("socksProxyPort", port);        Log.out("socks proxy: " + sysprops.get("socksProxyHost") + ":" +                sysprops.get("socksProxyPort"));    }    public static void main(String[] argv)    {    	try {    		long start = System.currentTimeMillis();    		Log.out("starting up jftp...");    		compile();    		System.setProperty("sshtools.logfile", Settings.appHomeDir +    		"log4.txt");    		if(Settings.autoUpdate)    		{    			checkForUpgrade();    		}    		Settings.enableResuming = true;    		Settings.enableUploadResuming = true;    		Settings.noUploadResumingQuestion = false;    		setSocksProxyOptions(Settings.getSocksProxyHost(),    				Settings.getSocksProxyPort());    		JFtp jftp = new JFtp(true);    		UIManager.getLookAndFeelDefaults().put("ClassLoader",    				jftp.getClass().getClassLoader());    		//boolean lookSet = false;    		String tmp = Settings.getLookAndFeel();    		//UIManager.installLookAndFeel("Metouia", "net.sourceforge.mlf.metouia.MetouiaLookAndFeel");    		if(tmp != null)    		{    			jftp.setLookAndFeel(Settings.getLookAndFeel());    		}    		else    		{    			jftp.setLookAndFeel("net.sourceforge.mlf.metouia.MetouiaLookAndFeel");    		}    		//else    		//{    		//	jftp.setLookAndFeel("com.incors.plaf.kunststoff.KunststoffLookAndFeel");    		//	lookSet = true;    		//}    		//if(!lookSet) UIManager.installLookAndFeel("Kunststoff", "com.incors.plaf.kunststoff.KunststoffLookAndFeel");    		/*    		 * Don't try to add the Kunststoff look and feel if it has    		 * already been added.    		 */    		if((Settings.getLookAndFeel() == null) ||    				!Settings.getLookAndFeel().equals("com.incors.plaf.kunststoff.KunststoffLookAndFeel"))    		{    			/*    			 * Somehow even though UIManager.installLookAndFeel throws a    			 * ClassNotFoundException, it ends up getting added to the    			 * list of installed look and feels anyway. What we do here is    			 * make Java throw a ClassNotFoundException before then by    			 * checking whether or not the class exists, and only if the    			 * class exists does it then get added to the list of    			 * available look and feel's.    			 */    			try    			{    				Class.forName("com.incors.plaf.kunststoff.KunststoffLookAndFeel");    				UIManager.installLookAndFeel("Kunststoff",    				"com.incors.plaf.kunststoff.KunststoffLookAndFeel");    			}    			catch(ClassNotFoundException cnfe)    			{    			}    		}    		/*    		 * Don't try to add the Metouia look and feel if it has    		 * already been added.    		 */    		if((Settings.getLookAndFeel() == null) ||    				!Settings.getLookAndFeel().equals("net.sourceforge.mlf.metouia.MetouiaLookAndFeel"))    		{    			/*    			 * Somehow even though UIManager.installLookAndFeel throws a    			 * ClassNotFoundException, it ends up getting added to the    			 * list of installed look and feels anyway. What we do here is    			 * make Java throw a ClassNotFoundException before then by    			 * checking whether or not the class exists, and only if the    			 * class exists does it then get added to the list of    			 * available look and feel's.    			 */    			try    			{    				Class.forName("net.sourceforge.mlf.metouia.MetouiaLookAndFeel");    				UIManager.installLookAndFeel("Metouia",    				"net.sourceforge.mlf.metouia.MetouiaLookAndFeel");    			}    			catch(ClassNotFoundException cnfe)    			{    			}    		}    		/*                try                {                    Class.forName("com.jgoodies.looks.plastic.PlasticXPLookAndFeel");                    UIManager.installLookAndFeel("PlasticXP",                                                 "com.jgoodies.looks.plastic.PlasticXPLookAndFeel");                    Class.forName("com.jgoodies.looks.plastic.Plastic3DLookAndFeel");                    UIManager.installLookAndFeel("Plastic3D",                                                 "com.jgoodies.looks.plastic.Plastic3DLookAndFeel");                    Class.forName("com.jgoodies.looks.plastic.PlasticLookAndFeel");                    UIManager.installLookAndFeel("Plastic",                                                 "com.jgoodies.looks.plastic.PlasticLookAndFeel");                    Class.forName("com.jgoodies.looks.windows.WindowsLookAndFeel");                    UIManager.installLookAndFeel("Windows",                                                 "com.jgoodies.looks.windows.WindowsLookAndFeel");                }                catch(ClassNotFoundException cnfe)                {                    ex.printStackTrace();                }    		 */    		mainFrame = new JFrame();    		mainFrame.setLocation(Settings.getWindowLocation());    		mainFrame.setTitle(Settings.title + " - Version " + getVersion());    		mainFrame.setResizable(Settings.resize);    		mainFrame.addWindowListener(jftp);    		Image icon = HImage.getImage(jftp, Settings.iconImage);    		mainFrame.setIconImage(icon);    		mainFrame.setFont(GUIDefaults.font);    		//*** MY CHANGES    		//mainFrame.setJMenuBar(new AppMenuBar(jftp));    		menuBar = new AppMenuBar(jftp);    		mainFrame.setJMenuBar(menuBar);    		//***    		mainFrame.getContentPane().setLayout(new BorderLayout());    		mainFrame.getContentPane().add("Center", jftp);    		SwingUtilities.updateComponentTreeUI(mainFrame);    		mainFrame.pack();    		mainFrame.validate();    		mainFrame.setVisible(true);    		if(argv.length > 0)    		{    			jftp.hc.update(argv[0]);    		}    		else    		{    			//jftp.hc.update();    		}    		Log.out("jftp is up and running.");    		long end = System.currentTimeMillis();    		Log.out("startup time: " + (end - start) + "ms.");    	}    	catch(Error ex) {    		ex.printStackTrace();    	}    }    private void log(String msg)    {        if(msg.startsWith("200") || msg.startsWith("227"))        {            if((msg.indexOf("NOOP") > 0) || (msg.indexOf("Type") > 0) ||                   (msg.indexOf("MODE") > 0) || (msg.indexOf("Passive") > 0))            {                if(Settings.hideStatus)                {                    return;                }            }        }        else if(log == null)        {            return;        }        if(!msg.equals(""))        {            buffer = buffer + " " + msg;        }        log.append(buffer);        buffer = "";        long time = System.currentTimeMillis();        if(((time - oldtime) < Settings.uiRefresh))        {            UpdateDaemon.updateLog();            return;        }        oldtime = time;        JScrollBar bar = logSp.getVerticalScrollBar();        bar.setValue(bar.getMaximum());        //logSp.paintImmediately(0, 0, logSp.getSize().width,        //                       logSp.getSize().height);        /*        SwingUtilities.invokeLater(new Runnable()            {                public void run()                {                    repaint();                    //paintImmediately(0, 0, getSize().width, getSize().height);                }            });        */        repaint();        revalidate();    }    private void logRaw(String msg)    {        log.append(" " + msg);        Log.out("NOTE: logRaw called");        paintImmediately(0, 0, getSize().width, getSize().height);        //logSp.paintImmediately(0,0,logSp.getSize().width,logSp.getSize().height);    }    public void clearLog()    {        log.setText("");        logSp.paintImmediately(0, 0, logSp.getSize().width,                               logSp.getSize().height);    }    private void log(String msg, Throwable throwable)    {        PrintWriter p = new PrintWriter(new StringWriter());        throwable.printStackTrace(p);        log(msg);        log(p.toString());    }    public void debug(String msg)    {        log(msg + "\n");    }    public void debugRaw(String msg)    {        logRaw(msg);    }    public void debug(String msg, Throwable throwable)    {        log(msg, throwable);    }    public void warn(String msg)    {        log(msg);    }    public void warn(String msg, Throwable throwable)    {        log(msg, throwable);    }    public void error(String msg)    {        log(msg);    }    public void error(String msg, Throwable throwable)    {        log(msg, throwable);    }    public void info(String msg)    {        log(msg);    }    public void info(String msg, Throwable throwable)    {        log(msg, throwable);    }    public void fatal(String msg)    {        log(msg);    }    public void fatal(String msg, Throwable throwable)    {        log(msg, throwable);    }    public void debugSize(int size, boolean recv, boolean last, String file)    {    }    public static String getVersion()    {        try        {            URL u = ClassLoader.getSystemResource(Settings.readme);            if(u == null)            {                u = HImage.class.getResource("/" + Settings.readme);            }            DataInputStream i = new DataInputStream(u.openStream());            String tmp = i.readLine();            tmp = tmp.substring(tmp.lastIndexOf(">") + 1);            tmp = tmp.substring(0, tmp.indexOf("<"));            return tmp;        }        catch(Exception ex)        {        }        return "";    }    private static void checkForUpgrade()    {        try        {            Log.out("Checking for updates...");            URL u = ClassLoader.getSystemResource(Settings.readme);            if(u == null)            {                u = HImage.class.getResource("/" + Settings.readme);            }            DataInputStream i = new DataInputStream(u.openStream());            String tmp = i.readLine();            tmp = tmp.substring(tmp.lastIndexOf(">"));            tmp = tmp.substring(tmp.indexOf(".") + 1);            tmp = tmp.substring(0, tmp.indexOf("<"));            int x = Integer.parseInt(tmp) + 1;            String nextVersion = "jftp-1.";            if(x < 10)            {                nextVersion = nextVersion + "0";            }            nextVersion = nextVersion + x + ".tar.gz";            //System.out.println(nextVersion);            File dl = new File(nextVersion);            if(!dl.exists() || (dl.length() <= 0))            {                URL url = new URL("http://osdn.dl.sourceforge.net/sourceforge/j-ftp/" +                                  nextVersion);                BufferedOutputStream f = new BufferedOutputStream(new FileOutputStream(dl));                BufferedInputStream in = new BufferedInputStream(url.openStream());                byte[] buf = new byte[4096];                int stat = 1;                Log.out("\ndownloading update: " + dl.getAbsolutePath() +                        "\n\n");                while(stat > 0)                {                    //while(in.available() <= 0) LocalIO.pause(10);                    stat = in.read(buf);                    if(stat == -1)                    {                        break;                    }                    f.write(buf, 0, stat);                    System.out.print(".");                }                f.flush();                f.close();                in.close();            }            Log.out("\n\n\na newer version was found!\nplease install the File " +                    dl.getAbsolutePath() +                    " or even better visit the homepage to download the latest version...\n" +                    "you can turn this feature off if you don't like it (view readme for details)\n\nStarting anyway...\n\n");            //LocalIO.pause(5000);            //System.exit(0);        }        catch(Exception ex)        {            // FileNotFoundException, everything is ok            //ex.printStackTrace();        }        Log.out("finished check...");    }    public static ConnectionHandler getConnectionHandler()    {        BasicConnection con = remoteDir.getCon();        if((con != null) && con instanceof FtpConnection)        {            return ((FtpConnection) con).getConnectionHandler();        }        else        {            return defaultConnectionHandler;        }    }    public void fireUpdate()    {        // if(!(JFtp.remoteDir.getCon() instanceof FilesystemConnection))        LocalIO.pause(200);        repaint();        /*

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?