jftp.java
来自「java ftp 操作代码,程序可以直接运行」· Java 代码 · 共 1,478 行 · 第 1/3 页
JAVA
1,478 行
/* * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */package net.sf.jftp;import java.awt.BorderLayout;import java.awt.Component;import java.awt.Dimension;import java.awt.FlowLayout;import java.awt.Image;import java.awt.Point;import java.awt.Toolkit;import java.awt.datatransfer.DataFlavor;import java.awt.datatransfer.Transferable;import java.awt.dnd.DnDConstants;import java.awt.dnd.DropTarget;import java.awt.dnd.DropTargetDragEvent;import java.awt.dnd.DropTargetDropEvent;import java.awt.dnd.DropTargetEvent;import java.awt.dnd.DropTargetListener;import java.awt.event.ActionListener;import java.awt.event.ComponentEvent;import java.awt.event.ComponentListener;import java.awt.event.WindowEvent;import java.awt.event.WindowListener;import java.io.BufferedInputStream;import java.io.BufferedOutputStream;import java.io.DataInputStream;import java.io.File;import java.io.FileOutputStream;import java.io.PrintWriter;import java.io.Reader;import java.io.StringWriter;import java.net.URL;import java.util.Hashtable;import javax.swing.JDesktopPane;import javax.swing.JFrame;import javax.swing.JInternalFrame;import javax.swing.JLabel;import javax.swing.JPanel;import javax.swing.JScrollBar;import javax.swing.JScrollPane;import javax.swing.JSplitPane;import javax.swing.JTabbedPane;import javax.swing.JTextArea;import javax.swing.JToolBar;import javax.swing.SwingUtilities;import javax.swing.UIManager;import javax.swing.event.ChangeEvent;import javax.swing.event.ChangeListener;import javax.swing.event.InternalFrameEvent;import javax.swing.event.InternalFrameListener;import javax.swing.plaf.basic.BasicInternalFrameTitlePane.RestoreAction;import net.sf.jftp.config.Settings;import net.sf.jftp.gui.base.AppMenuBar;import net.sf.jftp.gui.base.DownloadList;import net.sf.jftp.gui.base.DownloadQueue;import net.sf.jftp.gui.base.LocalDir;import net.sf.jftp.gui.base.LogFlusher;import net.sf.jftp.gui.base.RemoteDir;import net.sf.jftp.gui.base.StatusPanel;import net.sf.jftp.gui.base.dir.Dir;import net.sf.jftp.gui.base.dir.DirEntry;import net.sf.jftp.gui.framework.FileTransferable;import net.sf.jftp.gui.framework.GUIDefaults;import net.sf.jftp.gui.framework.HDesktopBackground;import net.sf.jftp.gui.framework.HImage;import net.sf.jftp.gui.hostchooser.HostChooser;import net.sf.jftp.gui.tasks.HostInfo;import net.sf.jftp.net.BasicConnection;import net.sf.jftp.net.ConnectionHandler;import net.sf.jftp.net.ConnectionListener;import net.sf.jftp.net.FilesystemConnection;import net.sf.jftp.net.FtpConnection;import net.sf.jftp.system.LocalIO;import net.sf.jftp.system.UpdateDaemon;import net.sf.jftp.system.logging.Log;import net.sf.jftp.system.logging.Logger;import net.sf.jftp.tools.RSSFeeder;public class JFtp extends JPanel implements WindowListener, ComponentListener, Logger, ChangeListener, InternalFrameListener{ public static boolean mainUsed = false; public static StatusPanel statusP; public static JLabel statusL = new JLabel("Welcome to JFtp... "); public static JFrame mainFrame; public static Dir localDir; public static Dir remoteDir; public static DownloadList dList = new DownloadList(); public static DownloadQueue dQueue = new DownloadQueue(); public static boolean uiBlocked = false; public static HostInfo hostinfo = new HostInfo(); //public static BasicConnection controlConnection = null; private static ConnectionHandler defaultConnectionHandler = new ConnectionHandler(); public static JDesktopPane desktop = new JDesktopPane(); private static JScrollPane logSp; public static JTextArea log; //***appMenuBar: this must now be a public object that JFtp refers to public static AppMenuBar menuBar = null; public static final int CAPACITY = 9; //number of connections remembered public static final int CONNECTION_DATA_LENGTH = 10; public static DropTarget dropTarget; public static DropTargetListener dtListener; public static int acceptableActions = DnDConstants.ACTION_COPY; private LogFlusher flusher; private boolean initSize = true; public JTabbedPane remoteConnectionPanel = new JTabbedPane(); public JTabbedPane localConnectionPanel = new JTabbedPane(); private String oldText = ""; private HDesktopBackground background; /** JSplitPane that holds the directory panes and the log/dl JSplitPane */ private JSplitPane workP = null; /** JSplitPane that holds the log download parts */ private JSplitPane logP = null; private JInternalFrame j1; private JInternalFrame j2; private JInternalFrame j3; private JInternalFrame j4; private JInternalFrame j5; private Hashtable internalFrames = new Hashtable(); public HostChooser hc; private String buffer = ""; private long oldtime = 0; private UpdateDaemon daemon; public RSSFeeder feeder; //*** public JFtp() { Log.setLogger(this); // we have jesktop-environment if(statusP != null) { statusP.remove(statusP.close); } init(); } public JFtp(boolean mainUsed) { Log.setLogger(this); this.mainUsed = mainUsed; init(); } public void init() { dtListener = new DTListener(); dropTarget = new DropTarget(this, this.acceptableActions, dtListener, true); setLayout(new BorderLayout()); setBackground(GUIDefaults.mainBack); setForeground(GUIDefaults.front); statusP = new StatusPanel(this); add("North", statusP); localDir = (Dir) new LocalDir(Settings.defaultWorkDir); localDir.setDownloadList(dList); remoteDir = (Dir) new RemoteDir(); remoteDir.setDownloadList(dList); desktop.setDropTarget(this.dropTarget); Dimension d = Settings.getWindowSize(); setPreferredSize(d); setSize(d); int width = (int) d.getWidth(); int height = (int) d.getHeight(); dList.setMinimumSize(new Dimension((int) (width / 2.5), (int) (height * 0.20))); dList.setPreferredSize(new Dimension((int) (width / 2.5), (int) (height * 0.25))); dList.setSize(new Dimension((int) (width / 2.5), (int) (height * 0.25))); desktop.setDragMode(JDesktopPane.OUTLINE_DRAG_MODE); addBackgroundImage(); j1 = new JInternalFrame("Local filesystem", true, false, true, true); j1.setMinimumSize(new Dimension(300, 300)); j1.setLocation(5, 5); localConnectionPanel.addTab("file://", null, (Component) localDir, "Filesystem"); localConnectionPanel.setSelectedIndex(0); localConnectionPanel.addChangeListener(this); j1.getContentPane().add(localConnectionPanel); localDir.fresh(); desktop.add(j1); j1.setDefaultCloseOperation(JInternalFrame.DO_NOTHING_ON_CLOSE); j1.addInternalFrameListener(this); j1.pack(); j1.setSize(new Dimension(400, j1.getSize().height)); j1.show(); j2 = new JInternalFrame("Remote connections", true, false, true, true); j2.setLocation(430, 5); remoteConnectionPanel.addTab("file://", null, (Component) remoteDir, "Filesystem"); remoteConnectionPanel.setSelectedIndex(0); remoteConnectionPanel.addChangeListener(this); j2.getContentPane().add(remoteConnectionPanel); desktop.add(j2); j2.setDefaultCloseOperation(JInternalFrame.DO_NOTHING_ON_CLOSE); j2.addInternalFrameListener(this); j2.pack(); j2.setSize(new Dimension(415, j1.getSize().height)); j2.show(); log = new JTextArea(); log.setBackground(GUIDefaults.light); log.setForeground(GUIDefaults.front); log.setEditable(false); logSp = new JScrollPane(log); logSp.setSize(new Dimension(438, 148)); j5 = new JInternalFrame("Queue System", true, false, true, true); j5.setLocation(5, 400); j5.getContentPane().add(dQueue, BorderLayout.CENTER); desktop.add(j5); j5.pack(); j5.setSize(new Dimension(440, 150)); j5.show(); j3 = new JInternalFrame("Log", true, false, true, true); int x = (int) (desktop.getSize().width / 2); j3.setLocation(5, 425); j3.getContentPane().add(logSp, BorderLayout.CENTER); desktop.add(j3); j3.pack(); j3.setSize(new Dimension(440, 150)); j3.show(); j4 = new JInternalFrame("Download Manager", true, false, true, true); j4.setLocation(450, 425); j4.getContentPane().add(dList, BorderLayout.CENTER); desktop.add(j4); j4.pack(); j4.setSize(new Dimension(380, 150)); j4.show(); j1.toFront(); j2.toFront(); add("Center", desktop); JToolBar j = new JToolBar(); j.setFloatable(false); j.add(statusP.status, FlowLayout.LEFT); if(Settings.getEnableRSS()) { feeder = new RSSFeeder(); j.add(feeder); } add("South", j); addComponentListener(this); componentResized(new ComponentEvent(log, 0)); restoreInternalPositions(); validate(); setVisible(true); if(!mainUsed) { SwingUtilities.invokeLater(new Runnable() { public void run() { chooseHost(); } }); } else { chooseHost(); } flusher = new LogFlusher(); daemon = new UpdateDaemon(this); } protected void chooseHost() { hc = new HostChooser(this); if(!mainUsed) { hc.update(); } } public static String getHost() { return statusP.getHost(); } public static void setHost(String which) { statusP.setHost(which); } public static void localUpdate() { localDir.fresh(); } public static void remoteUpdate() { remoteDir.fresh(); } private void saveInternalPositions() { saveInternalPosition(j1, "local"); saveInternalPosition(j2, "remote"); saveInternalPosition(j5, "queue"); saveInternalPosition(j3, "log"); saveInternalPosition(j4, "manager"); } private void restoreInternalPositions() { if(Settings.getProperty("jftp.iframes.resize").equals("false")) { } else { restoreInternalPosition(j1, "local"); restoreInternalPosition(j2, "remote"); restoreInternalPosition(j5, "queue"); restoreInternalPosition(j3, "log"); restoreInternalPosition(j4, "manager"); } } private void restoreInternalPosition(JInternalFrame f, String desc) { String x = Settings.getProperty("jftp.iframes."+desc+".x"); String y = Settings.getProperty("jftp.iframes."+desc+".y"); String w = Settings.getProperty("jftp.iframes."+desc+".width"); String h = Settings.getProperty("jftp.iframes."+desc+".height"); if(x.indexOf(".") >= 0) x = x.substring(0, x.indexOf(".")); if(y.indexOf(".") >= 0) y = y.substring(0, y.indexOf(".")); try { f.setLocation(Integer.parseInt(x), Integer.parseInt(y)); f.setSize(Integer.parseInt(w), Integer.parseInt(h)); } catch(Exception ex) { Log.out("Can not set internal fram position for: "+desc); ex.printStackTrace(); } } private void saveInternalPosition(JInternalFrame f, String desc) { Point p = f.getLocation(); Settings.setProperty("jftp.iframes."+desc+".x", ""+p.getX()); Settings.setProperty("jftp.iframes."+desc+".y", ""+p.getY()); Settings.setProperty("jftp.iframes."+desc+".width", f.getWidth()); Settings.setProperty("jftp.iframes."+desc+".height", f.getHeight()); } public void windowClosing(WindowEvent e) { saveInternalPositions(); Settings.setProperty("jftp.window.width", this.getWidth()); Settings.setProperty("jftp.window.height", this.getHeight()); if(!mainUsed) { Settings.setProperty("jftp.window.x", (int) this.getLocationOnScreen().getX()); Settings.setProperty("jftp.window.y", (int) this.getLocationOnScreen().getY()); } else { Settings.setProperty("jftp.window.x", (int) mainFrame.getLocationOnScreen().getX()); Settings.setProperty("jftp.window.y", (int) mainFrame.getLocationOnScreen().getY()); } Settings.save(); safeDisconnect(); if(Settings.isStandalone) { System.exit(0); } else { mainFrame.dispose(); } } public static void safeDisconnect() { BasicConnection con = remoteDir.getCon(); if((con != null) && con.isConnected()) { try { con.disconnect(); } catch(Exception ex) { } } FilesystemConnection c = new FilesystemConnection(); c.addConnectionListener((ConnectionListener) remoteDir); remoteDir.setCon(c); if(!c.chdir("/")) { c.chdir("C:\\"); } } public void windowClosed(WindowEvent e) { } public void windowActivated(WindowEvent e) { } public void windowDeactivated(WindowEvent e) { } public void windowIconified(WindowEvent e) { } public void windowDeiconified(WindowEvent e) { } public void windowOpened(WindowEvent e) { } public void componentHidden(ComponentEvent e) { } public void componentMoved(ComponentEvent e) { } public void componentShown(ComponentEvent e) { } public void componentResized(ComponentEvent e) { localDir.actionPerformed(this, "local"); remoteDir.actionPerformed(this, "remote");
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?