ftpconnection.java
来自「java ftp 操作代码,程序可以直接运行」· Java 代码 · 共 2,382 行 · 第 1/5 页
JAVA
2,382 行
if(tmp[i].trim().equals("../") || tmp[i].trim().equals("./")) { Log.debug("Skipping " + tmp[i].trim()); } else { if(!work) { return TRANSFER_STOPPED; } if(downloadDir(tmp[i]) < 0) { ; // return TRANSFER_FAILED; } } } else { //System.out.println( "file: " + getLocalPath() + tmp[i] + "\n\n"); if(!work) { return TRANSFER_STOPPED; } fileCount++; if(rawDownload(getLocalPath() + tmp[i]) < 0) { ; // return TRANSFER_FAILED; } } } chdirNoRefresh(oldPwd); setLocalPath(oldDir); return TRANSFER_SUCCESSFUL; } /** Upload a file or directory. * Uses multithreading if enabled and does not block. * * @param file The file to upload * @return An int-statuscode, NEW_TRANSFER_SPAWNED,TRANSFER_FAILED or TRANSFER_SUCCESSFUL */ public int handleUpload(String file) { return handleUpload(file, null); } /** Upload a file or directory. * Uses multithreading if enabled and does not block. * * @param file The file to upload * @param realName The file to rename the uploaded file to * @return An int-statuscode, NEW_TRANSFER_SPAWNED,TRANSFER_FAILED or TRANSFER_SUCCESSFUL */ public int handleUpload(String file, String realName) { if(Settings.getEnableMultiThreading() && (!Settings.getNoUploadMultiThreading())) { Log.out("spawning new thread for this upload."); FtpTransfer t; if(realName != null) { t = new FtpTransfer(host, port, getLocalPath(), getCachedPWD(), file, username, password, Transfer.UPLOAD, handler, listeners, realName, crlf); } else { t = new FtpTransfer(host, port, getLocalPath(), getCachedPWD(), file, username, password, Transfer.UPLOAD, handler, listeners, crlf); } lastTransfer = t; return NEW_TRANSFER_SPAWNED; } else { if(Settings.getNoUploadMultiThreading()) { Log.out("upload multithreading is disabled."); } else { Log.out("multithreading is completely disabled."); } return (realName == null) ? upload(file) : upload(file, realName); } } /** * Upload a file or directory, block until finished. * * @param file The file to upload * @return An int returncode */ public int upload(String file) { return upload(file, file); } /** * Upload and a file or directory under a given name, block until finished. * Note that setting realName does not affect directory transfers * * @param file The file to upload * @param realName The file to rename the uploaded file to * @return An int responsecode */ public int upload(String file, String realName) { return upload(file, realName, null); } /** * Upload from an InputStream, block until finished. * * @param file The file to upload * @param in InputStream to read from * @return An int responsecode */ public int upload(String file, InputStream in) { return upload(file, file, in); } /** * Upload and a file or directory under a given name, block until finished. * Note that setting realName does not affect directory transfers * * @param file The file to upload * @param realName The file to rename the uploaded file to * @param in InputStream to read from * @return An int responsecode */ public int upload(String file, String realName, InputStream in) { hasUploaded = true; Log.out("ftp upload started: " + this); int stat; if((in == null) && new File(file).isDirectory()) { shortProgress = true; fileCount = 0; baseFile = file; dataType = DataConnection.PUTDIR; isDirUpload = true; stat = uploadDir(file); shortProgress = false; //System.out.println(fileCount + ":" + baseFile); fireProgressUpdate(baseFile, DataConnection.DFINISHED + ":" + fileCount, -1); fireActionFinished(this); fireDirectoryUpdate(this); } else { dataType = DataConnection.PUT; stat = rawUpload(file, realName, in); try { Thread.sleep(100); } catch(Exception ex) { } fireActionFinished(this); fireDirectoryUpdate(this); } try { Thread.sleep(500); } catch(Exception ex) { } return stat; } private int rawUpload(String file) { return rawUpload(file, file); } private int rawUpload(String file, String realName) { return rawUpload(file, realName, null); } /** uploads a file */ private int rawUpload(String file, String realName, InputStream in) { if(!file.equals(realName)) { Log.debug("File: " + file + ", stored as " + realName); } else { Log.debug("File: " + file); realName = null; } file = parse(file); String path = file; try { int p = 0; if(StringUtils.isRelative(file)) { path = getLocalPath() + file; } file = StringUtils.getFile(file); //BufferedReader in = jcon.getReader(); boolean resume = false; String size = "0"; if(Settings.enableUploadResuming && (in == null)) { list(); String[] ls = sortLs(); String[] sizes = sortSize(); if((ls == null) || (sizes == null)) { Log.out(">>> ls out of sync (skipping resume check)"); } else { for(int i = 0; i < ls.length; i++) { //Log.out(ls[i] + ":" + sizes[i]); if(realName != null) { if(ls[i].equals(realName)) { resume = true; size = sizes[i]; break; } } else { if(ls[i].equals(file)) { resume = true; size = sizes[i]; } } } File f = new File(path); if(f.exists() && (f.length() <= Integer.parseInt(size))) { Log.out("skipping resuming, file is <= filelength"); resume = false; } else if(f.exists() && Integer.parseInt(size) > 0) { if(!Settings.noUploadResumingQuestion) { if(JOptionPane.showConfirmDialog(new JLabel(), "A file smaller than the one to be uploaded already exists on the server,\n do you want to resume the upload?", "Resume upload?", JOptionPane.YES_NO_OPTION) != JOptionPane.OK_OPTION) { resume = false; } } Log.out("resume: " + resume + ", size: " + size); } else { Log.out("resume: " + resume + ", size: " + size); } } } modeStream(); //binary(); p = negotiatePort(); if(resume && Settings.enableUploadResuming) { jcon.send(REST + " " + size); if(getLine(PROCEED) == null) { resume = false; } } dcon = new DataConnection(this, p, host, path, dataType, resume, Integer.parseInt(size), in); //, new Updater()); while(!dcon.isThere()) { pause(10); } //System.out.println(path + " : " + file); if(realName != null) { jcon.send(STOR + " " + realName); } else { jcon.send(STOR + " " + file); } String tmp = getLine(POSITIVE); if(!tmp.startsWith(POSITIVE) && !tmp.startsWith(PROCEED)) { return TRANSFER_FAILED; } Log.debug(tmp); // we need to block since some ftp-servers do not want the // refresh command that dirpanel sends otherwise while(!dcon.finished) { pause(10); } } catch(Exception ex) { ex.printStackTrace(); Log.debug(ex.toString() + " @FtpConnection::upload"); return TRANSFER_FAILED; } return TRANSFER_SUCCESSFUL; } private int uploadDir(String dir) { //System.out.println("up"); if(dir.endsWith("\\")) { System.out.println("Something's wrong with the selected directory - please report this bug!"); } if(!dir.endsWith("/")) { dir = dir + "/"; } if(StringUtils.isRelative(dir)) { dir = getLocalPath() + dir; } String single = StringUtils.getDir(dir); String path = dir.substring(0, dir.indexOf(single)); String remoteDir = getCachedPWD() + single; //StringUtils.removeStart(dir,path); String oldDir = getCachedPWD(); if(Settings.safeMode) { noop(); } boolean successful = mkdir(remoteDir); if(!successful) { return MKDIR_FAILED; } if(Settings.safeMode) { noop(); } chdirNoRefresh(remoteDir); File f2 = new File(dir); String[] tmp = f2.list(); for(int i = 0; i < tmp.length; i++) { String res = dir + tmp[i]; File f3 = new File(res); if(f3.isDirectory()) { if(!work) { return TRANSFER_STOPPED; } uploadDir(res); } else { //System.out.println(">>>\nlp: "+path+single + "\nrp: "); //System.out.println(remoteDir +"\nres: "+res); if(!work) { return TRANSFER_STOPPED; } fileCount++; if(rawUpload(res) < 0) { ; //return TRANSFER_STOPPED; } } } chdirNoRefresh(oldDir); return TRANSFER_SUCCESSFUL; } private String parse(String file) { file = parseSymlink(file); return file; } /** recursive delete remote directory */ private int cleanDir(String dir, String path) { if(dir.equals("")) { return 0; } if(!dir.endsWith("/")) { dir = dir + "/"; } String remoteDir = StringUtils.removeStart(dir, path); String oldDir = pwd; chdirNoRefresh(pwd + remoteDir); try { list(); } catch(IOException ex) { // probably we don't have permission to ls here return PERMISSION_DENIED; }
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?