localdir.java

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

JAVA
1,552
字号
                return;            }            Log.out("direct transfer started (upload)");            JFtp.remoteDir.getCon().upload(entry.file,                                           JFtp.localDir.getCon()                                                        .getDownloadInputStream(path +                                                                                entry.file));            JFtp.remoteDir.actionPerformed(con, "FRESH");        }    }    /**    * Transfers single file, or all selected files if index is -1    */    public void transfer(int i)    {        if(i == -2)        {            transfer();            return;        }        else if(dirEntry[i].selected)        {            startTransfer(dirEntry[i]);        }    }    /**    * Safely refresh the UI.    */    public void safeUpdate()    {        UpdateDaemon.updateLocalDir();    }    /**    * Called by FtpConnection    */    public void actionPerformed(Object target, String msg)    {        //System.out.print(msg);        //fresh();        safeUpdate();    }    /**    * Called by FtpConnection, DownloadList is updated from here    */    public void updateProgress(String file, String type, long bytes) // only for url-downloads, for other see RemoteDir    {        if((dList == null) || (dirEntry == null))        {            return;        }        boolean flag = false;        if(file.endsWith("/") && (file.length() > 1))        {            flag = true;            file = file.substring(0, file.lastIndexOf("/"));        }        file = file.substring(file.lastIndexOf("/") + 1);        if(flag)        {            file = file + "/";        }        long s = 0;        if(JFtp.dList.sizeCache.containsKey(file))        {            s = ((Long) JFtp.dList.sizeCache.get(file)).longValue();        }        else        {            s = new File(getPath() + file).length();            JFtp.dList.sizeCache.put(file, new Long(s));        }        dList.updateList(file, type, bytes, s);    }    /**    * Called by FtpConnection    */    public void connectionInitialized(BasicConnection con)    {    }    /**    * Called by FtpConnection    */    public void actionFinished(BasicConnection con)    {        //fresh();        safeUpdate();    }    /**    * Called by FtpConnection    */    public void connectionFailed(BasicConnection con, String reason)    {    }    private void setDate()    {        if(!(con instanceof FtpConnection) &&               !(con instanceof FilesystemConnection))        {            try            {                sorter.removeItem("Date");            }            catch(Exception ex)            {            }            dateEnabled = false;            return;        }        //Log.debug(">>> date gui init (local)");        if((con instanceof FtpConnection) &&               (((FtpConnection) con).dateVector.size() > 0))        {            if(!dateEnabled)            {                sorter.addItem("Date");                dateEnabled = true;                UpdateDaemon.updateRemoteDirGUI();            }        }        else if((con instanceof FilesystemConnection) &&                    (((FilesystemConnection) con).dateVector.size() > 0))        {            if(!dateEnabled)            {                sorter.addItem("Date");                dateEnabled = true;                UpdateDaemon.updateRemoteDirGUI();            }        }        else        {            if(dateEnabled)            {                sorter.removeItem("Date");                dateEnabled = false;                Settings.showLocalDateNoSize = false;                UpdateDaemon.updateRemoteDirGUI();            }        }    }    /**    * Called by FtpConnection    */    public void updateRemoteDirectory(BasicConnection c)    {        if(con instanceof FtpConnection)        {            path = ((FtpConnection) con).getCachedPWD();        }        else if(con instanceof SmbConnection && !path.startsWith("smb://"))        {            path = con.getPWD();        }        else        {            path = con.getPWD();        }        //Log.out(">>> update local dir");        //fresh();        safeUpdate();    }    /**    * Extract a zip file and change to the directory    */    private void setZipFilePath(DirEntry entry)    {        if(JFtp.mainFrame != null)        {            JFtp.mainFrame.setCursor(Cursor.WAIT_CURSOR);        }        String n = entry.toString();        String tmp = path + n + "-dir/";        Log.debug("\nExtracting: " + path + n);        File tmpDir = new File(tmp);        if(tmpDir.exists() || tmpDir.mkdir())        {            try            {                ZipFile z = new ZipFile(path + n);                Enumeration e = z.entries();                while(e.hasMoreElements())                {                    ZipEntry z1 = (ZipEntry) e.nextElement();                    Log.debug("-> " + z1.getName());                    BufferedInputStream in = new BufferedInputStream(z.getInputStream(z1));                    BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(tmp +                                                                                             z1.getName()));                    byte[] buf = new byte[8192];                    while(in.read(buf, 0, 8192) != StreamTokenizer.TT_EOF)                    {                        out.write(buf);                    }                    out.flush();                    out.close();                    in.close();                }                chdir(tmp);            }            catch(IOException ex)            {                ex.printStackTrace();                Log.debug("ERROR: " + ex);            }        }        else        {            Log.debug("Cannot create directory, skipping extraction...");        }        //chdir(path + tgt);        if(JFtp.mainFrame != null)        {            JFtp.mainFrame.setCursor(Cursor.DEFAULT_CURSOR);        }    }    /**    * Mime type handler for doubleclicks on files    */    public void showContentWindow(String url, DirEntry d)    {    	//------- popup -> run    	if(Settings.runtimeCommands > 0 && url.startsWith("popup-run@")) {    		String ext = url.substring(10);    		           	try {           		System.out.println("xx: "+ext);        		if(!Settings.askToRun || (Settings.askToRun && UITool.askToRun(this))) UIUtils.runCommand(ext);        	}        	catch(Exception ex) {        		Log.out("Could not launch file: "+ ext);        	}        	        	return;    	}    	    	//------------    	        if(d.toString().endsWith(".zip"))        {            setZipFilePath(d);        }        else        {            try            {                url = "file://" + url;                // -------- images ----------                String ext = url.toLowerCase();                if(ext.endsWith(".jpg") || ext.endsWith(".gif") ||                       ext.endsWith(".jpeg") || ext.endsWith(".png"))                {                    ImageViewer d1 = new ImageViewer(url);                    JFtp.desktop.add(d1, new Integer(Integer.MAX_VALUE - 11));                    return;                }                                if(Settings.runtimeCommands > 1) {                	try {                		if(!Settings.askToRun || (Settings.askToRun && UITool.askToRun(this))) {                			UIUtils.runCommand(ext);                        	return;                		}                	}                	catch(Exception ex) {                		Log.out("Could not launch file: "+ ext);                	}                }                                // -----------------                if(d.getRawSize() > 200000)                {                    Log.debug("File is too big - 200kb is the maximum, sorry.");                    return;                }                HPanel f = new HPanel();                JEditorPane pane = new JEditorPane(url);                pane.setEditable(false);                if(!pane.getEditorKit().getContentType().equals("text/html") &&                       !pane.getEditorKit().getContentType().equals("text/rtf"))                {                    if(!pane.getEditorKit().getContentType().equals("text/plain"))                    {                        Log.debug("Nothing to do with this filetype - use the buttons if you want to transfer files.");                        return;                    }                    pane.setEditable(false);                }                JScrollPane jsp = new JScrollPane(pane);                f.setLayout(new BorderLayout());                f.add("Center", jsp);                JFtp.statusP.jftp.addToDesktop(url, f, 600, 400);            }            catch(Exception ex)            {                Log.debug("File error: " + ex);            }        }    }    public void keyPressed(KeyEvent e)    {        if(e.getKeyCode() == KeyEvent.VK_ENTER)        {            Object o = jl.getSelectedValue();            if(o == null)            {                return;            }            String tmp = ((DirEntry) o).toString();            if(tmp.endsWith("/") || tmp.equals(".."))            {                chdir(tmp);            }            else            {                showContentWindow(path + tmp, (DirEntry) o);            }        }        else if(e.getKeyCode() == KeyEvent.VK_SPACE)        {            int x = ((DirPanel) JFtp.remoteDir).jl.getSelectedIndex();            if(x == -1)            {                x = 0;            }            ((DirPanel) JFtp.remoteDir).jl.grabFocus();            ((DirPanel) JFtp.remoteDir).jl.setSelectedIndex(x);        }    }    public void keyReleased(KeyEvent e)    {    }    public void keyTyped(KeyEvent e)    {    }}

⌨️ 快捷键说明

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