1 16 package net.sf.jftp.net; 17 18 import java.io.IOException ; 19 import java.io.InputStream ; 20 import java.io.OutputStream ; 21 import java.net.URL ; 22 import java.net.URLConnection ; 23 24 import net.sf.jftp.system.logging.Log; 25 26 27 30 public class FtpURLConnection extends URLConnection 31 { 32 private FtpConnection connection = null; 33 private String username = "ftp"; 34 private String password = "none@no.no"; 35 private int loginFlag = 0; 36 37 public FtpURLConnection(URL u) 38 { 39 super(u); 40 41 connection = new FtpConnection(u.getHost()); 43 44 String userInfo = u.getUserInfo(); 45 46 if(userInfo != null) 47 { 48 int index = userInfo.indexOf(":"); 49 50 if(index != -1) 51 { 52 username = userInfo.substring(0, index); 53 password = userInfo.substring(index + 1); 54 } 55 } 56 57 Log.debug("URL: " + u.toString()); 62 } 63 64 public void connect() throws IOException 65 { 66 loginFlag = connection.login(username, password); 67 68 if(loginFlag != FtpConnection.LOGIN_OK) 69 { 70 return; 71 } 72 73 connection.chdir(url.getPath()); 75 } 76 77 public FtpConnection getFtpConnection() 78 { 79 return connection; 80 } 81 82 public InputStream getInputStream() throws IOException 83 { 84 return null; 85 } 86 87 public OutputStream getOutputStream() throws IOException 88 { 89 return null; 90 } 91 92 public String getUser() 93 { 94 return username; 95 } 96 97 public String getPass() 98 { 99 return password; 100 } 101 102 public String getHost() 103 { 104 return url.getHost(); 105 } 106 107 public int getPort() 108 { 109 int ret = url.getPort(); 110 111 if(ret <= 0) 112 { 113 return 21; 114 } 115 else 116 { 117 return ret; 118 } 119 } 120 121 public int getLoginResponse() 122 { 123 return loginFlag; 124 } 125 126 public boolean loginSucceeded() 127 { 128 if(loginFlag == FtpConnection.LOGIN_OK) 129 { 130 return true; 131 } 132 133 return false; 134 } 135 136 public static void main(String [] args) 137 { 138 try 139 { 140 URLConnection uc = new FtpURLConnection(new URL ("ftp://ftp:pass@localhost/pub")); 141 uc.connect(); 142 } 143 catch(IOException ioe) 144 { 145 } 146 } 147 } 148 | Popular Tags |