Java 利用FTP上传,下载文件,遍历文件目录

Java实现FTP上传下载文件的工具包有很多,这里我采用Java自带的API,实现FTP上传下载文件。另外JDK1.7以前的版本与其之后版本的API有了较大的改变了。

例如:

JDK1.7之前 JDK1.7
ftpClient = new FtpClinet() ftpClient = FtpClient.create(ip)
ftpclient.login(user,password) ftpclient.login(user,null,password)
ftpclient.binary() ftpClient.setBinaryType();

一. 连接FTP服务器

   1: public class FTPUtil {
   2:     //FTP服务器IP地址
   3:     public final static String FTP_HOST = "10.103.240.255";
   4:     
   5:     //FTP服务器端口
   6:     public final static int FTP_PORT = 21;
   7:     
   8:     //FTP服务器用户名
   9:     public final static String FTP_USER = "bloodHunter";
  10:     
  11:     //密码
  12:     public final static String FTP_PASSWORD = "wbljy";
  13:     
  14:     
  15:     public static FtpClient getConnect()
  16:     {
  17:         try {
  18:             FtpClient ftpClient = FtpClient.create(FTP_HOST);
  19:             ftpClient.login(FTP_USER, FTP_PASSWORD.toCharArray());
  20:             return ftpClient;
  21:         } catch (FtpProtocolException e) {
  22:             // TODO Auto-generated catch block
  23:             e.printStackTrace();
  24:             System.out.println("Connect to FTP Server fail!");
  25:             return null;
  26:         } catch (IOException e) {
  27:             // TODO Auto-generated catch block
  28:             e.printStackTrace();
  29:             System.out.println("Connect to FTP Server fail!");
  30:             return null;
  31:         }
  32:         
  33:     }
  34: }

二. 上传文件

   1: /*
   2:      * ftp file upload
   3:      * @param path 上传文件的路径
   4:      * @param fileName 上传文件名称
   5:      * @return 上传成功返回true,否则返回false
   6:      * */
   7:     
   8:     public static boolean FtpUpload(String path,String fileName)
   9:     {
  10:         TelnetOutputStream os = null;
  11:         FileInputStream is = null;
  12:         FtpClient ftpClient = getConnect();
  13:         try {
  14:             ftpClient.setBinaryType();
  15:             os = (TelnetOutputStream) ftpClient.putFileStream(fileName, true);
  16:             is = new FileInputStream(new File(path));
  17:             byte[] buffer = new byte[1024];
  18:             int c;
  19:             while((c = is.read(buffer)) != -1)
  20:             {
  21:                 os.write(buffer,0,c);
  22:             }
  23:             System.out.println("Upload Success!");
  24:             return true;
  25:         } catch (Exception e) {
  26:             // TODO: handle exception
  27:             e.printStackTrace();
  28:             System.out.println("Upload fail!");
  29:             return false;
  30:         }finally{
  31:             try {
  32:                 ftpClient.close();
  33:                 is.close();
  34:                 os.close();
  35:             } catch (IOException e) {
  36:                 // TODO Auto-generated catch block
  37:                 e.printStackTrace();
  38:             }
  39:         }
  40:     }

三. 下载文件

   1: /*
   2:      * ftp file download
   3:      * @param path 下载文件的保存路径
   4:      * @param fileName 下载文件名称
   5:      * @return 下载成功返回true,否则返回false
   6:      * */
   7:     public static boolean FtpDownload(String path,String fileName)
   8:     {
   9:         FileInputStream is = null;
  10:         FileOutputStream os = null;
  11:         FtpClient ftpClient = getConnect();
  12:         try {
  13:             is =  (FileInputStream) ftpClient.getFileStream(fileName);
  14:             os = new FileOutputStream(new File(path));
  15:             byte[] buffer = new byte[1024];
  16:             int c;
  17:             while((c = is.read(buffer)) != -1)
  18:             {
  19:                 os.write(buffer,0,c);
  20:             }
  21:             System.out.println("Download Success!");
  22:             return true;
  23:         } catch (FtpProtocolException e) {
  24:             // TODO Auto-generated catch block
  25:             e.printStackTrace();
  26:             System.out.println("Download fail!");
  27:             return false;
  28:         } catch (IOException e) {
  29:             // TODO Auto-generated catch block
  30:             e.printStackTrace();
  31:             System.out.println("Download fail");
  32:             return false;
  33:         }catch (Exception e) {
  34:             // TODO: handle exception
  35:             e.printStackTrace();
  36:             return false;
  37:         }
  38:         finally{
  39:             try {
  40:                 is.close();
  41:                 os.close();
  42:                 ftpClient.close();
  43:             } catch (IOException e) {
  44:                 // TODO Auto-generated catch block
  45:                 e.printStackTrace();
  46:             }
  47:         }
  48:     }

四. 遍历FTP目录文件

   1: /*
   2:      * FTP getFileList
   3:      * @param filenames       保存遍历的文件名
   4:      * @param path    遍历目录的路径
   5:      * */
   6:     public static void getFtpFileList(List<String> filenames,String path){
   7:         //DataInputStream ds = null;
   8:         BufferedReader ds = null;
   9:         FtpClient ftpClient = getConnect();
  10:         try {
  11:             ds = new BufferedReader(new InputStreamReader(ftpClient.nameList(path),"ISO-8859-1"));
  12:             String line = "";
  13:             while((line = ds.readLine())!=null){
  14:                 line = new String(line.getBytes("ISO-8859-1"),"GBK");
  15:                 String name[] = line.split("/");
  16:                 filenames.add(name[name.length - 1]);
  17:             }
  18:         } catch (FtpProtocolException e) {
  19:             // TODO Auto-generated catch block
  20:             e.printStackTrace();
  21:         } catch (IOException e) {
  22:             // TODO Auto-generated catch block
  23:             e.printStackTrace();
  24:         }finally{
  25:             try {
  26:                 ds.close();
  27:                 ftpClient.close();
  28:             } catch (IOException e) {
  29:                 // TODO Auto-generated catch block
  30:                 e.printStackTrace();
  31:             }
  32:         }
  33:     }

猜你喜欢

转载自zfei.iteye.com/blog/2379115