localdir.java
来自「java ftp 操作代码,程序可以直接运行」· Java 代码 · 共 1,552 行 · 第 1/4 页
JAVA
1,552 行
{ blockedTransfer(-2); } else if(e.getActionCommand().equals("<-")) { blockedTransfer(-2); } else if(e.getActionCommand().equals("rn")) { Object[] target = jl.getSelectedValues(); if((target == null) || (target.length == 0)) { Log.debug("No file selected"); return; } else if(target.length > 1) { Log.debug("Too many files selected"); return; } //Renamer r = new Renamer(target[0].toString(), path); //fresh(); String val = JOptionPane.showInternalInputDialog(JFtp.desktop, "Choose a name..."); if(val != null) { if(!con.rename(target[0].toString(), val)) { Log.debug("Rename failed."); } else { Log.debug("Successfully renamed."); fresh(); } } } else if(e.getSource() == props) { JFtp.statusP.jftp.clearLog(); int x = currentPopup.getPermission(); String tmp; if(x == FtpConnection.R) { tmp = "read only"; } else if(x == FtpConnection.W) { tmp = "read/write"; } else if(x == FtpConnection.DENIED) { tmp = "denied"; } else { tmp = "undefined"; } String msg = "File: " + currentPopup.toString() + "\n" + " Size: " + currentPopup.getFileSize() + " raw size: " + currentPopup.getRawSize() + "\n" + " Symlink: " + currentPopup.isLink() + "\n" + " Directory: " + currentPopup.isDirectory() + "\n" + " Permission: " + tmp + "\n"; Log.debug(msg); } else if(e.getSource() == viewFile) { if(currentPopup.isDirectory()) { Log.debug("This is a directory, not a file."); return; } String url = JFtp.localDir.getPath() + currentPopup.toString(); showContentWindow(url, currentPopup); } else if(e.getSource() == runFile) { if(currentPopup.isDirectory()) { Log.debug("This is a directory, not a file."); return; } String url = JFtp.localDir.getPath() + currentPopup.toString(); showContentWindow("popup-run@"+url, currentPopup); } else if(e.getSource() == sorter) { sortMode = (String) sorter.getSelectedItem(); if(sortMode.equals("Date")) { Settings.showLocalDateNoSize = true; } else { Settings.showLocalDateNoSize = false; } fresh(); } else if(e.getActionCommand().equals("cdUp")) { chdir(".."); } } private void copy(Object[] fRaw, String path, String offset, String target) throws Exception { String[] files = new String[fRaw.length]; for(int j = 0; j < fRaw.length; j++) { files[j] = fRaw[j].toString(); } for(int i = 0; i < files.length; i++) { File f = new File(path + offset + files[i]); BufferedInputStream in = null; BufferedOutputStream out = null; byte[] buf = new byte[4096]; if(f.exists() && !f.isDirectory()) { in = new BufferedInputStream(new FileInputStream(path + offset + files[i])); out = new BufferedOutputStream(new FileOutputStream(target + offset + files[i])); int len = 0; while(in != null) { len = in.read(buf); if(len == StreamTokenizer.TT_EOF) { break; } out.write(buf, 0, len); } out.flush(); out.close(); } else if(f.exists()) { if(!files[i].endsWith("/")) { files[i] = files[i] + "/"; } File f2 = new File(target + offset + files[i]); if(!f.exists()) { f.mkdir(); } copy(f.list(), path, offset + files[i], target); } } } /** * Initiate a tranfer with ui locking enabled */ public synchronized void blockedTransfer(int index) { tmpindex = index; Runnable r = new Runnable() { public void run() { // --------------- local ------------------- boolean block = !Settings.getEnableMultiThreading(); if(!(con instanceof FtpConnection)) { block = true; } if(block || Settings.getNoUploadMultiThreading()) { lock(false); } transfer(tmpindex); if(block || Settings.getNoUploadMultiThreading()) { unlock(false); } //{ //JFtp.remoteDir.fresh(); //unlock(false); //} } }; Thread t = new Thread(r); t.start(); } /** * Lock the gui. */ public void lock(boolean first) { JFtp.uiBlocked = true; jl.setEnabled(false); if(!first) { JFtp.remoteDir.lock(true); } } /** * Unlock the gui. */ public void unlock(boolean first) { JFtp.uiBlocked = false; jl.setEnabled(true); if(!first) { JFtp.remoteDir.unlock(true); } } /** * Do a hard UI refresh - do no longer call this directly, use * safeUpdate() instead. */ public void fresh() { if(JFtp.mainFrame != null) { JFtp.mainFrame.setCursor(Cursor.WAIT_CURSOR); } String i = ""; int idx = jl.getSelectedIndex(); if(idx >= 0) { Object o = jl.getSelectedValue(); if(o != null) { i = o.toString(); } } chdir(path); if((idx >= 0) && (idx < jl.getModel().getSize())) { if(jl.getModel().getElementAt(idx).toString().equals(i)) { jl.setSelectedIndex(idx); } else { jl.setSelectedIndex(0); } } update(); if(JFtp.mainFrame != null) { JFtp.mainFrame.setCursor(Cursor.DEFAULT_CURSOR); } } /** * Transfers all selected files */ public synchronized void transfer() { boolean[] bFileSelected = new boolean[dirEntry.length + 1]; DirEntry[] cacheEntry = new DirEntry[dirEntry.length]; System.arraycopy(dirEntry, 0, cacheEntry, 0, cacheEntry.length); for(int i = 0; i < dirEntry.length; i++) { bFileSelected[i] = cacheEntry[i].selected; if(!cacheEntry[i].equals(dirEntry[i])) { Log.out("mismatch"); } } for(int i = 0; i < cacheEntry.length; i++) { if(bFileSelected[i]) { startTransfer(cacheEntry[i]); } } } /** * Start a file transfer. * Depending on the local and remote connection types some things like * local working directory have to be set, resuming may have to be checked etc. * As with ftp to ftp transfers the action used to download a file might actually be * an upload. * * WARNING: If you do anything here, please check RemoteDir.startTransfer(), too! */ public void startTransfer(DirEntry entry) { if(con instanceof FilesystemConnection && JFtp.remoteDir.getCon() instanceof FtpConnection) { if((entry.getRawSize() < Settings.smallSizeUp) && !entry.isDirectory()) { JFtp.remoteDir.getCon().upload(path + entry.file); } else { JFtp.remoteDir.getCon().handleUpload(path + entry.file); } } else if(con instanceof FtpConnection && JFtp.remoteDir.getCon() instanceof FtpConnection) { // local: ftp, remote: ftp if(entry.isDirectory()) { Log.debug("Directory transfer between remote connections is not supported yet!"); return; } Log.out("direct ftp transfer started (upload)"); ((FtpConnection) JFtp.remoteDir.getCon()).upload(entry.file, ((FtpConnection) JFtp.localDir.getCon()).getDownloadInputStream(path + entry.file)); } else if(con instanceof FtpConnection && JFtp.remoteDir.getCon() instanceof FilesystemConnection) { con.setLocalPath(JFtp.remoteDir.getPath()); con.handleDownload(path + entry.file); } else if(con instanceof FilesystemConnection && JFtp.remoteDir.getCon() instanceof FilesystemConnection) { con.upload(entry.file); JFtp.remoteDir.actionPerformed(con, "FRESH"); } else if(con instanceof FilesystemConnection) { // local: file, remote: smb,sftp,nfs JFtp.remoteDir.getCon().handleUpload(entry.file); JFtp.remoteDir.actionPerformed(con, "FRESH"); } else { if(entry.isDirectory()) { Log.debug("Directory transfer between remote connections is not supported yet!");
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?