[JAVA]Apache FTPClient操作“卡死”问题的分析和解决(四)

2014-11-24 07:32:03 · 作者: · 浏览: 10
_connected = false;
244 } catch (IOException ex) {
245 }
246 }
247 }
248
249 /**
250 * Makes the full name of the file on the FTP server by joining its path and
251 * the local file name.
252 *
253 * @param ftpPath
254 * file path on the server
255 * @param localFile
256 * local file
257 * @return full name of the file on the FTP server
258 */
259 public String makeFTPFileName(String ftpPath, File localFile) {
260 if (ftpPath == "") {
261 return localFile.getName();
262 } else {
263 String path = ftpPath.trim();
264 if (path.charAt(path.length() - 1) != '/') {
265 path = path + "/";
266 }
267
268 return path + localFile.getName();
269 }
270 }
271
272 /**
273 * Test coonection to ftp server
274 *
275 * @return true, if connected
276 */
277 public boolean isConnected() {
278 return is_connected;
279 }
280
281 /**
282 * Get current directory on ftp server
283 *
284 * @return current directory
285 */
286 public String getWorkingDirectory() {
287 if (!is_connected) {
288 return "";
289 }
290
291 try {
292 return ftp.printWorkingDirectory();
293 } catch (IOException e) {
294 }
295
296 return "";
297 }
298
299 /**
300 * Set working directory on ftp server
301 *
302 * @param dir
303 * new working directory
304 * @return true, if working directory changed
305 */
306 public boolean setWorkingDirectory(String dir) {
307 if (!is_connected) {
308 return false;
309 }
310
311 try {
312 return ftp.changeWorkingDirectory(dir);
313 } catch (IOException e) {
314 }
315
316 return false;
317 }
318
319 /**
320 * Change working directory on ftp server to parent directory
321 *
322 * @return true, if working directory changed
323 */
324 public boolean setParentDirectory() {
325 if (!is_connected) {
326 return false;
327 }
328
329 try {
330 return ftp.changeToParentDirectory();
331 } catch (IOException e) {
332 }
333
334 return false;
335 }
336
337 /**
338 * Get parent directory name on ftp server
339 *
340 * @return parent directory
341 */
342 public String getParentDirectory() {
343 if (!is_connected) {
344 return "";
345 }
346
347 String w = getWorkingDirectory();
348 setParentDirectory();
349 String p = getWorkingDirectory();
350 setWorkingDirectory(w);
351
352 return p;
353 }
354
355 /**
356 * Get directory contents on ftp server
357 *
358 * @param filePath
359 * directory
360 * @return list of FTPFileInfo structures
361 * @throws IOException
362 */
363 public List listFiles(String filePath) throws IOException {
364 List fileList = new ArrayList();
365
366 // Use passive mode to pass firewalls.
367 ftp.enterLocalPassiveMode();
368 FTPFile[] ftpFiles = ftp.listFiles(filePath);
369 int size = (ftpFiles == null) 0 : ftpFiles.le