java操作FTP,实现文件上传下载删除操作

来源:互联网 发布:java测试工程师面试题 编辑:程序博客网 时间:2024/04/27 05:07

上传文件到FTP服务器:

Java代码  收藏代码
  1. /** 
  2.      * Description: 向FTP服务器上传文件 
  3.      * @param url FTP服务器hostname 
  4.      * @param port FTP服务器端口,如果默认端口请写-1 
  5.      * @param username FTP登录账号 
  6.      * @param password FTP登录密码 
  7.      * @param path FTP服务器保存目录 
  8.      * @param filename 上传到FTP服务器上的文件名 
  9.      * @param input 输入流 
  10.      * @return 成功返回true,否则返回false 
  11.      */  
  12.     public static boolean uploadFile(String url, int port, String username, String password, String path,  
  13.         String filename, InputStream input)  
  14.     {  
  15.         boolean success = false;  
  16.         FTPClient ftp = new FTPClient();  
  17.         try  
  18.         {  
  19.             int reply;  
  20.               
  21.             // 连接FTP服务器  
  22.             if (port > -1)  
  23.             {  
  24.                 ftp.connect(url, port);  
  25.             }  
  26.             else  
  27.             {  
  28.                 ftp.connect(url);  
  29.             }  
  30.               
  31.             // 登录FTP  
  32.             ftp.login(username, password);  
  33.             reply = ftp.getReplyCode();  
  34.             if (!FTPReply.isPositiveCompletion(reply))  
  35.             {  
  36.                 ftp.disconnect();  
  37.                 return success;  
  38.             }  
  39.             ftp.changeWorkingDirectory(path);  
  40.             ftp.storeFile(filename, input);  
  41.               
  42.             input.close();  
  43.             ftp.logout();  
  44.             success = true;  
  45.         }  
  46.         catch (IOException e)  
  47.         {  
  48.             success = false;  
  49.             logger.error(EXCEPTION_NAME, e);  
  50.         }  
  51.         finally  
  52.         {  
  53.             if (ftp.isConnected())  
  54.             {  
  55.                 try  
  56.                 {  
  57.                     ftp.disconnect();  
  58.                 }  
  59.                 catch (IOException e)  
  60.                 {  
  61.                     logger.error(EXCEPTION_NAME, e);  
  62.                 }  
  63.             }  
  64.         }  
  65.         return success;  
  66.     }  

从FTP服务器下载文件:

Java代码  收藏代码
  1. /** 
  2.      * Description: 从FTP服务器下载文件 
  3.      * @Version1.0 Jul 27, 2008 5:32:36 PM by 崔红保(cuihongbao@d-heaven.com)创建 
  4.      * @param url FTP服务器hostname 
  5.      * @param port FTP服务器端口 
  6.      * @param username FTP登录账号 
  7.      * @param password FTP登录密码 
  8.      * @param remotePath FTP服务器上的相对路径 
  9.      * @param fileName 要下载的文件名 
  10.      * @param localPath 下载后保存到本地的路径 
  11.      * @return 
  12.      */  
  13.     public static boolean downloadFile(String url, int port, String username, String password, String remotePath,  
  14.         String fileName, String localPath)  
  15.     {  
  16.         boolean success = false;  
  17.         FTPClient ftp = new FTPClient();  
  18.         try  
  19.         {  
  20.             int reply;  
  21.               
  22.             // 连接FTP服务器  
  23.             if (port > -1)  
  24.             {  
  25.                 ftp.connect(url, port);  
  26.             }  
  27.             else  
  28.             {  
  29.                 ftp.connect(url);  
  30.             }  
  31.               
  32.             ftp.login(username, password);//登录  
  33.             reply = ftp.getReplyCode();  
  34.             if (!FTPReply.isPositiveCompletion(reply))  
  35.             {  
  36.                 ftp.disconnect();  
  37.                 return success;  
  38.             }  
  39.             ftp.changeWorkingDirectory(remotePath);//转移到FTP服务器目录  
  40.             FTPFile[] fs = ftp.listFiles();  
  41.             for (FTPFile ff : fs)  
  42.             {  
  43.                 if (ff.getName().equals(fileName))  
  44.                 {  
  45.                     File localFile = new File(localPath + "/" + ff.getName());  
  46.                       
  47.                     OutputStream is = new FileOutputStream(localFile);  
  48.                     ftp.retrieveFile(ff.getName(), is);  
  49.                     is.close();  
  50.                 }  
  51.             }  
  52.               
  53.             ftp.logout();  
  54.             success = true;  
  55.         }  
  56.         catch (IOException e)  
  57.         {  
  58.             logger.error(EXCEPTION_NAME, e);  
  59.         }  
  60.         finally  
  61.         {  
  62.             if (ftp.isConnected())  
  63.             {  
  64.                 try  
  65.                 {  
  66.                     ftp.disconnect();  
  67.                 }  
  68.                 catch (IOException e)  
  69.                 {  
  70.                     logger.error(EXCEPTION_NAME, e);  
  71.                 }  
  72.             }  
  73.         }  
  74.         return success;  
  75.     }  
 

删除FTP服务器上的文件:

Java代码  收藏代码
  1. /** 
  2.      * <删除FTP上的文件> 
  3.      * <远程删除FTP服务器上的录音文件> 
  4.      * @param url FTP服务器IP地址 
  5.      * @param port FTP服务器端口 
  6.      * @param username FTP服务器登录名 
  7.      * @param password FTP服务器密码 
  8.      * @param remotePath 远程文件路径 
  9.      * @param fileName 待删除的文件名 
  10.      * @return 
  11.      * @see [类、类#方法、类#成员] 
  12.      */  
  13.     public static boolean deleteFtpFile(String url, int port, String username, String password, String remotePath,  
  14.         String fileName)  
  15.     {  
  16.         boolean success = false;  
  17.         FTPClient ftp = new FTPClient();  
  18.         try  
  19.         {  
  20.             int reply;  
  21.               
  22.             // 连接FTP服务器  
  23.             if (port > -1)  
  24.             {  
  25.                 ftp.connect(url, port);  
  26.             }  
  27.             else  
  28.             {  
  29.                 ftp.connect(url);  
  30.             }  
  31.               
  32.             // 登录  
  33.             ftp.login(username, password);  
  34.             reply = ftp.getReplyCode();  
  35.             if (!FTPReply.isPositiveCompletion(reply))  
  36.             {  
  37.                 ftp.disconnect();  
  38.                 return success;  
  39.             }  
  40.               
  41.             // 转移到FTP服务器目录  
  42.             ftp.changeWorkingDirectory(remotePath);  
  43.             success = ftp.deleteFile(remotePath + "/" + fileName);  
  44.             ftp.logout();  
  45.         }  
  46.         catch (IOException e)  
  47.         {  
  48.             logger.error(EXCEPTION_NAME, e);  
  49.             success = false;  
  50.         }  
  51.         finally  
  52.         {  
  53.             if (ftp.isConnected())  
  54.             {  
  55.                 try  
  56.                 {  
  57.                     ftp.disconnect();  
  58.                 }  
  59.                 catch (IOException e)  
  60.                 {  
  61.                     logger.error(EXCEPTION_NAME, e);  
  62.                 }  
  63.             }  
  64.         }  
  65.         return success;  
  66.     }  
0 0
原创粉丝点击