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

2014-11-24 07:32:03 · 作者: · 浏览: 9
ngth;
370 for (int i = 0; i < size; i++) {
371 FTPFile ftpFile = ftpFiles[i];
372 FfpFileInfo fi = new FfpFileInfo();
373 fi.setName(ftpFile.getName());
374 fi.setSize(ftpFile.getSize());
375 fi.setTimestamp(ftpFile.getTimestamp());
376 fi.setType(ftpFile.isDirectory());
377 fileList.add(fi);
378 }
379
380 return fileList;
381 }
382
383 /**
384 * Get file from ftp server into given output stream
385 *
386 * @param ftpFileName
387 * file name on ftp server
388 * @param out
389 * OutputStream
390 * @throws IOException
391 */
392 public void getFile(String ftpFileName, OutputStream out) throws IOException {
393 try {
394 // Use passive mode to pass firewalls.
395 ftp.enterLocalPassiveMode();
396
397 // Get file info.
398 FTPFile[] fileInfoArray = ftp.listFiles(ftpFileName);
399 if (fileInfoArray == null) {
400 throw new FileNotFoundException("File '" + ftpFileName + "' was not found on FTP server.");
401 }
402
403 // Check file size.
404 FTPFile fileInfo = fileInfoArray[0];
405 long size = fileInfo.getSize();
406 if (size > Integer.MAX_VALUE) {
407 throw new IOException("File '" + ftpFileName + "' is too large.");
408 }
409
410 // Download file.
411 if (!ftp.retrieveFile(ftpFileName, out)) {
412 throw new IOException("Error loading file '" + ftpFileName + "' from FTP server. Check FTP permissions and path.");
413 }
414
415 out.flush();
416
417 } finally {
418 if (out != null) {
419 try {
420 out.close();
421 } catch (IOException ex) {
422 }
423 }
424 }
425 }
426
427 /**
428 * Put file on ftp server from given input stream
429 *
430 * @param ftpFileName
431 * file name on ftp server
432 * @param in
433 * InputStream
434 * @throws IOException
435 */
436 public void putFile(String ftpFileName, InputStream in) throws IOException {
437 try {
438 // Use passive mode to pass firewalls.
439 ftp.enterLocalPassiveMode();
440
441 if (!ftp.storeFile(ftpFileName, in)) {
442 throw new IOException("Can't upload file '" + ftpFileName + "' to FTP server. Check FTP permissions and path.");
443 }
444 } finally {
445 try {
446 in.close();
447 } catch (IOException ex) {
448 }
449 }
450 }