欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java ftp操作

程序员文章站 2022-04-08 23:18:33
...
简单的写了个java对ftp的操作

用的是commons-net.jar包

FTPProxy.java
 public class FTPProxy{  
     public FTPProxy()  
     {  
         super();  
         ftpClient = new FTPClient();  
     }  
   
     public FTPProxy(FTPBean entity)  
     {  
         this();  
         this.entity = entity;  
     }  
   
     private FTPBean entity;  
   
     private FTPClient ftpClient;  
   
     public void connection()  
     {  
         try  
         {  
             ftpClient.connect(entity.getServerIP());  
             ftpClient.login(entity.getUser(), entity.getPassword());  
         } catch (SocketException e)  
         {  
             e.printStackTrace();  
         } catch (IOException e)  
         {  
             e.printStackTrace();  
         }  
     }  
   
     public void download()  
     {  
         FileOutputStream fos = null;  
   
         try  
         {  
             String remoteFile = entity.getSrcFile();  
             String remoteFileName = "";  
             if (0 == remoteFile.lastIndexOf("/"))  
             {  
                 remoteFileName = remoteFile.substring(remoteFile  
                         .lastIndexOf("/") + 1);  
             } else if (0 < remoteFile.lastIndexOf("/"))  
             {  
                 remoteFileName = remoteFile.substring(remoteFile  
                         .lastIndexOf("/") + 1);  
                 String path = remoteFile.substring(0, remoteFile  
                         .lastIndexOf("/"));  
                 ftpClient.changeWorkingDirectory(path);  
             } else  
             {  
                 remoteFileName = remoteFile;  
             }  
   
             fos = new FileOutputStream(entity.getTargetFile());  
   
             ftpClient.setBufferSize(1024 * 8);  
   
             ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);  
             ftpClient.retrieveFile(remoteFileName, fos);  
         } catch (IOException e)  
         {  
             e.printStackTrace();  
         } finally  
         {  
             try  
             {  
                 fos.close();  
                 ftpClient.disconnect();  
             } catch (IOException e)  
             {  
                 e.printStackTrace();  
             }  
         }  
     }  
   
     public void upload()  
     {  
         FileInputStream fis = null;  
   
         try  
         {  
             File srcFile = new File(entity.getSrcFile());  
             fis = new FileInputStream(srcFile);  
   
             ftpClient.setBufferSize(1024 * 8);  
             ftpClient.setControlEncoding("GBK");  
   
             String ftpPath = entity.getFtpPath();  
   
             if(null!=ftpPath&&!"".equals(ftpPath))  
             {  
                 ftpClient.makeDirectory(ftpPath);  
                 ftpClient.changeWorkingDirectory(ftpPath);  
             }  
   
             ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);  
             ftpClient.storeFile(entity.getTargetFile(), fis);  
   
         } catch (FileNotFoundException e)  
         {  
             e.printStackTrace();  
         } catch (IOException e)  
         {  
             e.printStackTrace();  
         } finally  
         {  
             try  
             {  
                 fis.close();  
                 ftpClient.disconnect();  
             } catch (IOException e)  
             {  
                 e.printStackTrace();  
             }  
         }  
     }  
   
     public void setEntity(FTPBean entity)  
     {  
         this.entity = entity;  
     }  
   
     public static void main(String[] args)  
     {  
         FTPBean ftpBean = new FTPBean();  
         ftpBean.setUser("admin");  
         ftpBean.setPassword("admin");  
         ftpBean.setServerIP("192.168.211.204");  
         ftpBean.setSrcFile("D:\\loadingPackageList.jsp");  
         ftpBean.setTargetFile("loadingPackageList.jsp");  
         ftpBean.setFtpPath("/admin_Test");  
           
         FTPProxy f = new FTPProxy(ftpBean);  
         f.connection();  
         f.upload();  
     }  
 } 


FTPBean.java

   1. public class FTPBean  
   2. {  
   3.     private String serverIP;  
   4.   
   5.     private String user;  
   6.   
   7.     private String password;  
   8.   
   9.     private String srcFile;  
  10.   
  11.     private String targetFile;  
  12.   
  13.     private String ftpPath;  
  14.   
  15.     public String getServerIP()  
  16.     {  
  17.         return serverIP;  
  18.     }  
  19.   
  20.     public void setServerIP(String serverIP)  
  21.     {  
  22.         this.serverIP = serverIP;  
  23.     }  
  24.   
  25.     public String getUser()  
  26.     {  
  27.         return user;  
  28.     }  
  29.   
  30.     public void setUser(String user)  
  31.     {  
  32.         this.user = user;  
  33.     }  
  34.   
  35.     public String getPassword()  
  36.     {  
  37.         return password;  
  38.     }  
  39.   
  40.     public void setPassword(String password)  
  41.     {  
  42.         this.password = password;  
  43.     }  
  44.   
  45.     public String getSrcFile()  
  46.     {  
  47.         return srcFile;  
  48.     }  
  49.   
  50.     public void setSrcFile(String srcFile)  
  51.     {  
  52.         this.srcFile = srcFile;  
  53.     }  
  54.   
  55.     public String getTargetFile()  
  56.     {  
  57.         return targetFile;  
  58.     }  
  59.   
  60.     public void setTargetFile(String targetFile)  
  61.     {  
  62.         this.targetFile = targetFile;  
  63.     }  
  64.   
  65.     public String getFtpPath()  
  66.     {  
  67.         return ftpPath;  
  68.     }  
  69.   
  70.     public void setFtpPath(String ftpPath)  
  71.     {  
  72.         this.ftpPath = ftpPath;  
  73.     }  
  74. }