KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > de > jwi > ftp > FTPUploader


1
2 package de.jwi.ftp;
3 /*
4  * jFM - Java Web File Manager
5  *
6  * Copyright (C) 2004 Juergen Weber
7  *
8  * This file is part of jFM.
9  *
10  * jFM is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * jFM is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with jFM; if not, write to the Free Software
22  * Foundation, Inc., 59 Temple Place, Suite 330, Boston
23  */

24 import java.io.File JavaDoc;
25 import java.io.FileInputStream JavaDoc;
26 import java.io.IOException JavaDoc;
27 import java.net.URL JavaDoc;
28 import java.util.ArrayList JavaDoc;
29 import java.util.Arrays JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.List JavaDoc;
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 /**
39  * @author Jürgen Weber
40  * Source file created on 27.05.2004
41  */

42 public class FTPUploader
43 {
44
45     public static String JavaDoc upload(URL JavaDoc url, List JavaDoc files)
46     {
47         String JavaDoc rcs = "";
48
49         if (!"ftp".equals(url.getProtocol())) { return "not ftp protocol"; }
50
51         String JavaDoc host = url.getHost();
52         String JavaDoc userInfo = url.getUserInfo();
53         String JavaDoc path = url.getPath();
54         String JavaDoc user = null;
55         String JavaDoc 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             // After connection attempt, you should check the reply code to verify
76
// success.
77
reply = ftp.getReplyCode();
78
79             if (!FTPReply.isPositiveCompletion(reply))
80             {
81                 ftp.disconnect();
82                 return "connection refused";
83             }
84         }
85         catch (IOException JavaDoc e)
86         {
87             if (ftp.isConnected())
88             {
89                 try
90                 {
91                     ftp.disconnect();
92                 }
93                 catch (IOException JavaDoc f)
94                 {
95                     // do nothing
96
}
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             // Use passive mode as default because most of us are
112
// behind firewalls these days.
113
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 JavaDoc 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 JavaDoc f)
136                 {
137                     // do nothing
138
}
139             }
140         }
141
142         return rcs;
143     }
144
145     private static String JavaDoc uploadFiles(FTPClient ftp, String JavaDoc ftpServerPath,
146             List JavaDoc files) throws IOException JavaDoc
147     {
148         boolean rc = false;
149         String JavaDoc rcs = "error";
150         List JavaDoc 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 JavaDoc it = files.iterator();
159             while (it.hasNext())
160             {
161                 File JavaDoc f = (File JavaDoc) it.next();
162
163                 if (f.isDirectory())
164                 {
165                     String JavaDoc name = f.getName();
166
167                     String JavaDoc 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 JavaDoc uploadFile(FTPClient ftp, String JavaDoc ftpServerPath, File JavaDoc f)
194             throws IOException JavaDoc
195     {
196         String JavaDoc name = f.getName();
197         String JavaDoc rcs = "error";
198
199         FileInputStream JavaDoc fi = new FileInputStream JavaDoc(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 JavaDoc[] args) throws Exception JavaDoc
217     {
218         URL JavaDoc url = new URL JavaDoc("ftp://ftp:none@localhost/tmp");
219         String JavaDoc protocol = url.getProtocol();
220
221         String JavaDoc host = url.getHost();
222         String JavaDoc userInfo = url.getUserInfo();
223         String JavaDoc path = url.getPath();
224         String JavaDoc file = url.getFile();
225
226         File JavaDoc f = new File JavaDoc("D:/temp/out");
227         List JavaDoc l = new ArrayList JavaDoc();
228         l.add(f);
229         String JavaDoc s = upload(url, l);
230         System.out.println(s);
231         int x = 5;
232     }
233 }
Popular Tags