📄 webdavconnection.java
字号:
/* * 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.net;import net.sf.jftp.config.Settings;import net.sf.jftp.util.Log;import net.sf.jftp.util.StringUtils;import org.apache.commons.httpclient.*;import org.apache.webdav.lib.*;import java.io.*;import java.net.*;import java.util.*;public class WebdavConnection implements BasicConnection{ public static int webdavBuffer = 32000; private String path = ""; private String pwd = ""; private Vector listeners = new Vector(); private String[] files; private String[] size = new String[0]; private int[] perms = null; private String baseFile; private int fileCount; private boolean shortProgress = false; private String user; private String pass; public WebdavConnection(String path, String user, String pass, ConnectionListener l) { this.user = user; this.pass = pass; listeners.add(l); chdir(path); } public int removeFileOrDir(String file) { Log.debug("Feature is not implemented yet"); if(true) { return -1; } try { if((file == null) || file.equals("")) { return -1; } String tmp = file; if(StringUtils.isRelative(file)) { tmp = getPWD() + file; } WebdavFile f = new WebdavFile(getURL(tmp)); if(!f.getAbsolutePath().equals(f.getCanonicalPath())) { //Log.debug("WARNING: Symlink removed");//Skipping symlink, remove failed."); //Log.debug("This is necessary to prevent possible data loss when removing those symlinks."); //return -1; if(!f.delete()) { return -1; } } if(f.exists() && f.isDirectory()) { cleanLocalDir(tmp); } //System.out.println(tmp); if(!f.delete()) { Log.debug("Removal failed."); return -1; } } catch(Exception ex) { Log.debug("Error: " + ex.toString()); ex.printStackTrace(); } return -1; } private void cleanLocalDir(String dir) { try { dir = dir.replace('\\', '/'); if(!dir.endsWith("/")) { dir = dir + "/"; } //String remoteDir = StringUtils.removeStart(dir,path); //System.out.println(">>> " + dir); WebdavFile f2 = new WebdavFile(getURL(dir)); String[] tmp = f2.list(); if(tmp == null) { return; } for(int i = 0; i < tmp.length; i++) { WebdavFile f3 = new WebdavFile(getURL(dir + tmp[i])); if(!f3.getAbsolutePath().equals(f3.getCanonicalPath())) { //Log.debug("WARNING: Symlink remove");//Skipping symlink, remove may fail."); f3.delete(); //Log.debug("This is necessary to prevent possible data loss when removing those symlinks."); //continue; } if(f3.isDirectory()) { //System.out.println(dir); cleanLocalDir(dir + tmp[i]); f3.delete(); } else { //System.out.println(dir+tmp[i]); f3.delete(); } } } catch(Exception ex) { Log.debug("Error: " + ex.toString()); ex.printStackTrace(); } } public void sendRawCommand(String cmd) { } public void disconnect() { } public boolean isConnected() { return true; } public String getPWD() { return pwd; } public boolean cdup() { return chdir(pwd.substring(0, pwd.lastIndexOf("/") + 1)); } public boolean mkdir(String dirName) { Log.debug("Feature is not implemented yet"); if(true) { return false; } try { if(StringUtils.isRelative(dirName)) { dirName = getPWD() + dirName; } WebdavFile f = new WebdavFile(getURL(dirName)); boolean x = f.mkdir(); fireDirectoryUpdate(); return x; } catch(Exception ex) { Log.debug("Error: " + ex.toString()); ex.printStackTrace(); return false; } } public void list(String outfile) throws IOException { } public boolean chdir(String p) { try { String p2 = processPath(p); if(p2 == null) { return false; } //WebdavFile f = new WebdavFile(getURL(p2)); WebdavResource f = getResource(p2); if(!f.exists() || !f.isCollection()) //!f.isDirectory() || !f.canRead()) { Log.debug("Access denied."); return false; } pwd = p2; Log.out("PWD: " + pwd); fireDirectoryUpdate(); return true; } catch(Exception ex) { Log.debug("Error: " + ex.toString()); ex.printStackTrace(); return false; } } public boolean chdirNoRefresh(String p) { String p2 = processPath(p); if(p2 == null) { return false; } //System.out.println(p2); pwd = p2; return true; } public String getLocalPath() { //System.out.println("local: " + path); return path; } public String processPath(String p) { try { if(!p.startsWith("http://")) { p = pwd + p; } if(!p.endsWith("/")) { p = p + "/"; } while(p.endsWith("/../")) { p = p.substring(0, p.lastIndexOf("/../") - 1); p = p.substring(0, p.lastIndexOf("/")); } while(p.endsWith("/./")) { p = p.substring(0, p.lastIndexOf("/./") - 1); p = p.substring(0, p.lastIndexOf("/")); } //System.out.println(", processPath 2: "+p); //WebdavFile f = new WebdavFile(getURL(p)); Log.out("\n\n\nprocessPath URL: " + p); WebdavResource f = getResource(p); String p2 = p; if(f.exists() && f.isCollection()) { try { //p2 = f.toURL(); //CanonicalPath(); if(!p2.endsWith("/")) { p2 = p2 + "/"; } //Log.out("processPath parsed URL: " + p); //Log.out("processPath URL2: " + f.getPath()); return p2; } catch(Exception ex) { Log.debug("Error: can not get pathname (processPath)!"); return null; } } else { Log.debug("(processpPath) No such path: \"" + p + "\""); return null; } } catch(Exception ex) { Log.debug("Error: " + ex.toString()); ex.printStackTrace(); return null; } } public boolean setLocalPath(String p) { try { p = p.replace('\\', '/'); //System.out.print("local 1:" + p); if(StringUtils.isRelative(p)) { p = path + p; } p = p.replace('\\', '/'); //System.out.println(", local 2:" + p); File f = new File(p); if(f.exists()) { try { path = f.getCanonicalPath(); path = path.replace('\\', '/'); if(!path.endsWith("/")) { path = path + "/"; } //System.out.println("localPath: "+path); } catch(Exception ex) { Log.debug("Error: can not get pathname (local)!"); return false; } } else { Log.debug("(local) No such path: \"" + p + "\""); return false; } return true; } catch(Exception ex) { Log.debug("Error: " + ex.toString()); ex.printStackTrace(); return false; } } public String[] sortLs(String file) { try { Log.out("sortLs PWD: " + pwd); //WebdavFile fl = new WebdavFile(new URL(pwd), user, pass); WebdavResource fp = getResource(pwd); //new WebdavResource(getURL(pwd)); //files = f.list(); //File[] f = fl.listFiles(); WebdavResource[] f = fp.listWebdavResources(); //if(files == null) return new String[0]; files = new String[f.length]; size = new String[f.length]; perms = new int[f.length]; int accessible = 0; for(int i = 0; i < f.length; i++) { files[i] = f[i].getName(); /* while(files[i].indexOf("%20") >= 0) { int x = files[i].indexOf("%20"); String tmp = files[i].substring(0,x)+" "+ files[i].substring(files[i].indexOf("%20")+3); files[i] = tmp; } */ /* try { f[i].exists(); }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -