1 2 package de.jwi.ftp; 3 24 import java.io.File ; 25 import java.io.FileInputStream ; 26 import java.io.IOException ; 27 import java.net.URL ; 28 import java.util.ArrayList ; 29 import java.util.Arrays ; 30 import java.util.Iterator ; 31 import java.util.List ; 32 33 import org.apache.commons.net.ftp.FTP; 34 import org.apache.commons.net.ftp.FTPClient; 35 import org.apache.commons.net.ftp.FTPConnectionClosedException; 36 import org.apache.commons.net.ftp.FTPReply; 37 38 42 public class FTPUploader 43 { 44 45 public static String upload(URL url, List files) 46 { 47 String rcs = ""; 48 49 if (!"ftp".equals(url.getProtocol())) { return "not ftp protocol"; } 50 51 String host = url.getHost(); 52 String userInfo = url.getUserInfo(); 53 String path = url.getPath(); 54 String user = null; 55 String pass = null; 56 57 int p; 58 if ((userInfo != null) && ((p = userInfo.indexOf(':')) > -1)) 59 { 60 user = userInfo.substring(0, p); 61 pass = userInfo.substring(p + 1); 62 } 63 else 64 { 65 user = userInfo; 66 } 67 68 FTPClient ftp = new FTPClient(); 69 70 try 71 { 72 int reply; 73 ftp.connect(host); 74 75 reply = ftp.getReplyCode(); 78 79 if (!FTPReply.isPositiveCompletion(reply)) 80 { 81 ftp.disconnect(); 82 return "connection refused"; 83 } 84 } 85 catch (IOException e) 86 { 87 if (ftp.isConnected()) 88 { 89 try 90 { 91 ftp.disconnect(); 92 } 93 catch (IOException f) 94 { 95 } 97 } 98 return "could not connect to " + host; 99 } 100 101 try 102 { 103 if (!ftp.login(user, pass)) 104 { 105 ftp.logout(); 106 return "failed to login"; 107 } 108 109 ftp.setFileType(FTP.BINARY_FILE_TYPE); 110 111 ftp.enterLocalPassiveMode(); 114 115 rcs = uploadFiles(ftp, path, files); 116 117 ftp.logout(); 118 } 119 catch (FTPConnectionClosedException e) 120 { 121 return "connection closed"; 122 } 123 catch (IOException e) 124 { 125 return e.getMessage(); 126 } 127 finally 128 { 129 if (ftp.isConnected()) 130 { 131 try 132 { 133 ftp.disconnect(); 134 } 135 catch (IOException f) 136 { 137 } 139 } 140 } 141 142 return rcs; 143 } 144 145 private static String uploadFiles(FTPClient ftp, String ftpServerPath, 146 List files) throws IOException 147 { 148 boolean rc = false; 149 String rcs = "error"; 150 List l = null; 151 152 rc = ftp.makeDirectory(ftpServerPath); 153 boolean rc1 = ftp.changeWorkingDirectory(ftpServerPath); 154 System.out.println("cd: "+ftpServerPath); 155 156 if (rc1) 157 { 158 Iterator it = files.iterator(); 159 while (it.hasNext()) 160 { 161 File f = (File ) it.next(); 162 163 if (f.isDirectory()) 164 { 165 String name = f.getName(); 166 167 String newPath = ftpServerPath + "/" + name; 168 169 l = Arrays.asList(f.listFiles()); 170 171 rcs = uploadFiles(ftp, newPath, l); 172 173 rc1 = ftp.changeWorkingDirectory(ftpServerPath); 174 175 if (!rc1) 176 { 177 return "failed to chdir to " + ftpServerPath; 178 } 179 } 180 else 181 { 182 rcs = uploadFile(ftp, ftpServerPath, f); 183 } 184 } 185 } 186 else 187 { 188 rcs = "failed to chdir to " + ftpServerPath; 189 } 190 return rcs; 191 } 192 193 private static String uploadFile(FTPClient ftp, String ftpServerPath, File f) 194 throws IOException 195 { 196 String name = f.getName(); 197 String rcs = "error"; 198 199 FileInputStream fi = new FileInputStream (f); 200 201 boolean rc = false; 202 try 203 { 204 System.out.println(f); 205 System.out.println(ftpServerPath + " " + name); 206 rc = ftp.storeFile(name, fi); 207 rcs = rc ? "" : rcs; 208 } 209 finally 210 { 211 fi.close(); 212 } 213 return rcs; 214 } 215 216 public static void main(String [] args) throws Exception 217 { 218 URL url = new URL ("ftp://ftp:none@localhost/tmp"); 219 String protocol = url.getProtocol(); 220 221 String host = url.getHost(); 222 String userInfo = url.getUserInfo(); 223 String path = url.getPath(); 224 String file = url.getFile(); 225 226 File f = new File ("D:/temp/out"); 227 List l = new ArrayList (); 228 l.add(f); 229 String s = upload(url, l); 230 System.out.println(s); 231 int x = 5; 232 } 233 } | Popular Tags |