sftp2connection.java
来自「java ftp 操作代码,程序可以直接运行」· Java 代码 · 共 1,076 行 · 第 1/2 页
JAVA
1,076 行
x++; } File fx = new File(out); fx.mkdir(); for(int i = 0; i < tmp.length; i++) { if(tmp[i].equals("./") || tmp[i].equals("../")) { continue; } tmp[i] = tmp[i].replace('\\', '/'); //System.out.println("1: " + dir+tmp[i] + ", " + out +tmp[i]); if(tmp[i].endsWith("/")) { if(!tmp[i].endsWith("/")) { tmp[i] = tmp[i] + "/"; } downloadDir(dir + tmp[i], out + tmp[i]); } else { fileCount++; fireProgressUpdate(baseFile, DataConnection.GETDIR + ":" + fileCount, -1); work(dir + tmp[i], out + tmp[i], false); } } //System.out.println("enddir"); fireProgressUpdate(baseFile, DataConnection.DFINISHED + ":" + fileCount, -1); } catch(Exception ex) { ex.printStackTrace(); System.out.println(dir + ", " + out); Log.debug("Transfer error: " + ex); fireProgressUpdate(baseFile, DataConnection.FAILED + ":" + fileCount, -1); } shortProgress = false; } private void uploadDir(String dir, String out) { try { //System.out.println("uploadDir: " + dir + "," + out); isDirUpload = true; fileCount = 0; shortProgress = true; baseFile = StringUtils.getDir(dir); File f2 = new File(out); String[] tmp = f2.list(); if(tmp == null) { return; } channel.mkdir(dir); //channel.chmod(744, dir); for(int i = 0; i < tmp.length; i++) { if(tmp[i].equals("./") || tmp[i].equals("../")) { continue; } tmp[i] = tmp[i].replace('\\', '/'); //System.out.println("1: " + dir+tmp[i] + ", " + out +tmp[i]); File f3 = new File(out + tmp[i]); if(f3.isDirectory()) { if(!tmp[i].endsWith("/")) { tmp[i] = tmp[i] + "/"; } uploadDir(dir + tmp[i], out + tmp[i]); } else { fileCount++; fireProgressUpdate(baseFile, DataConnection.PUTDIR + ":" + fileCount, -1); work(out + tmp[i], dir + tmp[i], true); } } fireProgressUpdate(baseFile, DataConnection.DFINISHED + ":" + fileCount, -1); } catch(Exception ex) { ex.printStackTrace(); System.out.println(dir + ", " + out); Log.debug("Transfer error: " + ex); fireProgressUpdate(baseFile, DataConnection.FAILED + ":" + fileCount, -1); } isDirUpload = false; shortProgress = true; } private String toSFTP(String f) { String file; if(f.startsWith("/")) { file = f; } else { file = getPWD() + f; } file = file.replace('\\', '/'); //System.out.println("file: "+file); return file; } private String toSFTPDir(String f) { String file; if(f.startsWith("/")) { file = f; } else { file = pwd + f; } file = file.replace('\\', '/'); if(!file.endsWith("/")) { file = file + "/"; } //System.out.println("file: "+file); return file; } private void work(String file, String outfile, boolean up) { BufferedInputStream in = null; BufferedOutputStream out = null; //System.out.println("work"); try { boolean outflag = false; if(up) { in = new BufferedInputStream(new FileInputStream(file)); } else { in = new BufferedInputStream(channel.get(file)); } if(up) { outflag = true; try { channel.rm(outfile); } catch(Exception ex) { } out = new BufferedOutputStream(channel.put(outfile)); } else { out = new BufferedOutputStream(new FileOutputStream(outfile)); } //System.out.println("out: " + outfile + ", in: " + file); byte[] buf = new byte[smbBuffer]; int len = 0; int reallen = 0; //System.out.println(file+":"+getLocalPath()+outfile); while(true) { len = in.read(buf); //System.out.print("."); if(len == StreamTokenizer.TT_EOF) { break; } out.write(buf, 0, len); reallen += len; //System.out.println(file + ":" + StringUtils.getFile(file)); if(outflag) { fireProgressUpdate(StringUtils.getFile(outfile), DataConnection.PUT, reallen); } else { fireProgressUpdate(StringUtils.getFile(file), DataConnection.GET, reallen); } } //if(up) { // channel.chmod(744, outfile); //} fireProgressUpdate(file, DataConnection.FINISHED, -1); } catch(IOException ex) { ex.printStackTrace(); Log.debug("Error with file IO (" + ex + ")!"); fireProgressUpdate(file, DataConnection.FAILED, -1); } catch(SftpException ex) { ex.printStackTrace(); Log.debug("Error with SFTP IO (" + ex + ")!"); fireProgressUpdate(file, DataConnection.FAILED, -1); } finally { try { out.flush(); out.close(); in.close(); } catch(Exception ex) { ex.printStackTrace(); } } } public boolean rename(String oldName, String newName) { try { oldName = toSFTP(oldName); newName = toSFTP(newName); channel.rename(oldName, newName); return true; } catch(Exception ex) { ex.printStackTrace(); Log.debug("Could rename file (" + ex + ")."); return false; } } private void update(String file, String type, int bytes) { if(listeners == null) { return; } else { for(int i = 0; i < listeners.size(); i++) { ConnectionListener listener = (ConnectionListener) listeners.elementAt(i); listener.updateProgress(file, type, bytes); } } } public void addConnectionListener(ConnectionListener l) { listeners.add(l); } public void setConnectionListeners(Vector l) { listeners = l; } /** remote directory has changed */ public void fireDirectoryUpdate() { if(listeners == null) { return; } else { for(int i = 0; i < listeners.size(); i++) { ((ConnectionListener) listeners.elementAt(i)).updateRemoteDirectory(this); } } } public boolean login(String user, String pass) { this.user = user; this.pass = pass; if(!login()) { Log.debug("Login failed."); return false; } else { Log.debug("Authed successfully."); //if(!chdir(getPWD())) chdir("/"); } return true; } /** progress update */ public void fireProgressUpdate(String file, String type, int bytes) { if(listeners == null) { return; } for(int i = 0; i < listeners.size(); i++) { ConnectionListener listener = (ConnectionListener) listeners.elementAt(i); if(shortProgress && Settings.shortProgress) { if(type.startsWith(DataConnection.DFINISHED)) { listener.updateProgress(baseFile, DataConnection.DFINISHED + ":" + fileCount, bytes); } else if(isDirUpload) { listener.updateProgress(baseFile, DataConnection.PUTDIR + ":" + fileCount, bytes); } else { listener.updateProgress(baseFile, DataConnection.GETDIR + ":" + fileCount, bytes); } } else { listener.updateProgress(file, type, bytes); } } } public void fireActionFinished(Sftp2Connection con) { if(listeners == null) { return; } else { for(int i = 0; i < listeners.size(); i++) { ((ConnectionListener) listeners.elementAt(i)).actionFinished(con); } } } public int upload(String file, InputStream i) { BufferedOutputStream out = null; BufferedInputStream in = null; try { file = toSFTP(file); out = new BufferedOutputStream(channel.put(file)); in = new BufferedInputStream(i); //Log.debug(getLocalPath() + ":" + file+ ":"+getPWD()); byte[] buf = new byte[smbBuffer]; int len = 0; int reallen = 0; while(true) { len = in.read(buf); //System.out.print("."); if(len == StreamTokenizer.TT_EOF) { break; } out.write(buf, 0, len); reallen += len; fireProgressUpdate(StringUtils.getFile(file), DataConnection.PUT, reallen); } //channel.chmod(744, file); fireProgressUpdate(file, DataConnection.FINISHED, -1); return 0; } catch(IOException ex) { ex.printStackTrace(); Log.debug("Error with file IO (" + ex + ")!"); fireProgressUpdate(file, DataConnection.FAILED, -1); return -1; } catch(SftpException ex) { ex.printStackTrace(); Log.debug("Error with file SFTP IO (" + ex + ")!"); fireProgressUpdate(file, DataConnection.FAILED, -1); return -1; } finally { try { out.flush(); out.close(); in.close(); } catch(Exception ex) { ex.printStackTrace(); } } } public InputStream getDownloadInputStream(String file) { try { return channel.get(file); } catch(SftpException ex) { ex.printStackTrace(); Log.debug(ex.toString() + " @Sftp2Connection::getDownloadInputStream"); return null; } } public Date[] sortDates() { return null; }}class MyUserInfo implements UserInfo { String password; public MyUserInfo(String pass) { this.password = pass; } public String getPassword(){ return password; } public boolean promptYesNo(String str){ /* Object[] options={ "yes", "no" }; int foo=JOptionPane.showOptionDialog(null, str, "Warning", JOptionPane.DEFAULT_OPTION, JOptionPane.WARNING_MESSAGE, null, options, options[0]); return foo==0; */ return true; } public String getPassphrase(){ return password; } public boolean promptPassphrase(String message){ return true; } public boolean promptPassword(String message){ return true; } public void showMessage(String message){ JOptionPane.showMessageDialog(null, message); }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?