java ftp操作

本文提供了一个使用 Java 的 Apache Commons Net 库实现 FTP 文件上传的示例代码。该示例展示了如何连接到 FTP 服务器、更改工作目录、设置文件类型,并完成文件的上传过程。
  1. public boolean sendFile(final String hostname, final String username, final String password, final String remotePath,  
  2.             final File file, final String destinationFileName)  
  3.     {  
  4.         boolean allDone = false;  
  5.         FTPClient ftpClient = new FTPClient();  
  6.         try  
  7.         {  
  8.             int reply;  
  9.             ftpClient.connect(hostname);  
  10.             reply = ftpClient.getReplyCode();  
  11.               
  12.             if(!FTPReply.isPositiveCompletion(reply))  
  13.             {  
  14.                 throw new RuntimeException("Unable to connect to ftp server");  
  15.             }  
  16.               
  17.             if(!ftpClient.login(username, password))  
  18.             {  
  19.                 throw new RuntimeException("Unable to login to ftp server");  
  20.             }  
  21.               
  22.             if(!ftpClient.changeWorkingDirectory(remotePath))  
  23.             {  
  24.                 throw new RuntimeException("Unable to change working directory");  
  25.             }  
  26.   
  27.             if(!ftpClient.setFileType(FTP.ASCII_FILE_TYPE))  
  28.             {  
  29.                 throw new RuntimeException("Unable to set file mode ");  
  30.             }  
  31.   
  32.             String tempFileName = destinationFileName + FILEEXT_TEMP;  
  33.   
  34.             FileInputStream fis = new FileInputStream(file);  
  35.             try  
  36.             {  
  37.                 ftpClient.storeFile(tempFileName, fis);  
  38.                 fis.close();  
  39.             }   
  40.             finally  
  41.             {  
  42.                 fis.close();  
  43.             }  
  44.   
  45.             String finalFileName = destinationFileName;  
  46.             if(!ftpClient.rename(tempFileName, finalFileName))  
  47.             {  
  48.                 if(!ftpClient.deleteFile(tempFileName))  
  49.                 {  
  50.                     LOG.warn("Unable to delete temp file");  
  51.                 }  
  52.                 throw new RuntimeException("Unable to rename file");  
  53.             }  
  54.             ftpClient.logout();  
  55.             allDone = true;  
  56.         }   
  57.         catch (Exception e)  
  58.         {  
  59.             LOG.fatal("Unable to FTP file", e);  
  60.         }  
  61.         finally  
  62.         {  
  63.             if(ftpClient.isConnected())  
  64.             {  
  65.                 try  
  66.                 {  
  67.                     ftpClient.disconnect();  
  68.                 }  
  69.                 catch (IOException e)  
  70.                 {  
  71.                     LOG.warn("Erro while disconnecting from FTP", e);  
  72.                 }  
  73.             }  
  74.         }     
  75.           
  76.         return allDone;  
  77.     } 



/--------------------------------------------------------------------------------------------------------------------------/

  1. import java.io.IOException;   
  2. import java.io.OutputStream;   
  3.   
  4. import org.apache.commons.io.FileUtils;   
  5. import org.apache.commons.io.IOUtils;   
  6. import org.apache.commons.net.ftp.FTPClient;   
  7. import org.apache.commons.net.ftp.FTPFile;   
  8.   
  9. /**  
  10.  * 使用commons的net包进行ftp链接.  
  11.  * 相关包:commons-net-1.4.1.jar ; commons-io-1.2.jar;jakarta-oro-2.0.8.jar测试通过.可以列出ftp上的文件 
  12.  * 通过把ftp服务器上的文件流连接到outSteam及可以把文件下载到本机的目录..限制如果目录为中文则需要处理.最好使用英文文件名 
  13.  * @author xzgf  
  14.  * email: 
  15.  * 
     
  16.  * @create 2007-2-11  
  17.  *  
  18.  */  
  19. public class ListFtpFile {   
  20.   
  21.     private FTPClient ftpClient = new FTPClient();   
  22.        
  23.     private OutputStream outSteam = null;   
  24.        
  25.     /**  
  26.      * ftp服务器地址  
  27.      */  
  28.     private String hostName = "127.0.0.1";   
  29.        
  30.     /**  
  31.      * 登录名  
  32.      */  
  33.     private String userName = "1";   
  34.        
  35.     /**  
  36.      * 登录密码  
  37.      */  
  38.     private String password = "1";   
  39.        
  40.     /**  
  41.      * 需要访问的远程目录  
  42.      */  
  43.     private String remoteDir = "/ClientTest";   
  44.        
  45.     /**  
  46.      * 登录方法  
  47.      *  
  48.      */  
  49.     private void login() {   
  50.         try {   
  51.             //链接到ftp服务器   
  52.             ftpClient.connect(hostName);   
  53.             System.out.println("连接到ftp服务器:" + hostName + " 成功..开始登录");   
  54.             //登录.用户名 密码   
  55.             ftpClient.login(userName, password);   
  56.             System.out.println("登录成功.");   
  57.                
  58.             FTPFile[] remoteFiles = ftpClient.listFiles(remoteDir);   
  59.             System.out.println("目录" + remoteDir + "下的文件:");   
  60.             if(remoteFiles != null) {   
  61.                 for(int i=0;i
  62.                     String name = remoteFiles[i].getName();   
  63.                     long length = remoteFiles[i].getSize();   
  64.                     String readableLength = FileUtils.byteCountToDisplaySize(length);   
  65.                     System.out.println(name + ":\t\t" + readableLength);   
  66.                 }   
  67.             }   
  68.                
  69.         } catch (Exception e) {   
  70.             e.printStackTrace();   
  71.         } finally {   
  72.             //使用IO包关闭流   
  73.             IOUtils.closeQuietly(outSteam);   
  74.             try {   
  75.                 ftpClient.disconnect();   
  76.             } catch (IOException ioe) {   
  77.                 ioe.printStackTrace();   
  78.             }   
  79.         }   
  80.     }   
  81.        
  82.     public static void main(String[] args) {   
  83.         ListFtpFile listFtpfiles = new ListFtpFile();   
  84.         listFtpfiles.login();   
  85.     }   
  86. }   

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值