`
langzhiwang888
  • 浏览: 176829 次
  • 性别: Icon_minigender_1
  • 来自: 青岛
社区版块
存档分类
最新评论

ftp

 
阅读更多

最近因工作需要,数据库中的数据需要从FTP服务中抽取数据文件然后校检再抽取到数据中。因为第一步需要从FTP服务中抽取数据文件。第二步采用JDBC批量数据更新。

1。采用Apache.FTPClient:

Java代码  收藏代码
  1. /** 
  2.  * Apache.FTPClient FTP操作共公类 
  3.  *  
  4.  * @author 张明学 
  5.  *  
  6.  */  
  7. public class FTPCommon {  
  8.   
  9.     private FTPClient ftpClient;  
  10.   
  11.     private FTPModel ftpModel;  
  12.   
  13.     public FTPCommon(FTPModel ftp) {  
  14.         super();  
  15.         // 从配置文件中读取初始化信息  
  16.         this.ftpClient = new FTPClient();  
  17.         this.ftpModel = ftp;  
  18.     }  
  19.   
  20.     /** 
  21.      * 连接并登录FTP服务器 
  22.      *  
  23.      */  
  24.     public boolean ftpLogin() {  
  25.         boolean isLogin = false;  
  26.         FTPClientConfig ftpClientConfig = new FTPClientConfig(  
  27.                 FTPClientConfig.SYST_NT);  
  28.         ftpClientConfig.setServerTimeZoneId(TimeZone.getDefault().getID());  
  29.         this.ftpClient.setControlEncoding("GBK");  
  30.         this.ftpClient.configure(ftpClientConfig);  
  31.         try {  
  32.             if (this.ftpModel.getPort() > 0) {  
  33.                 this.ftpClient.connect(ftpModel.getUrl(), ftpModel.getPort());  
  34.             } else {  
  35.                 this.ftpClient.connect(ftpModel.getUrl());  
  36.             }  
  37.             // FTP服务器连接回答  
  38.             int reply = this.ftpClient.getReplyCode();  
  39.             if (!FTPReply.isPositiveCompletion(reply)) {  
  40.                 this.ftpClient.disconnect();  
  41.                 return isLogin;  
  42.             }  
  43.             this.ftpClient.login(this.ftpModel.getUsername(), this.ftpModel  
  44.                     .getPassword());  
  45.             this.ftpClient.changeWorkingDirectory(this.ftpModel.getRemoteDir());  
  46.             this.ftpClient.setFileType(FTPClient.FILE_STRUCTURE);  
  47.             LogUtil.infoOutPut("成功登陆FTP服务器:" + this.ftpModel.getUrl() + " 端口号:"  
  48.                     + this.getFtpModel().getPort() + " 目录:"  
  49.                     + this.ftpModel.getRemoteDir());  
  50.             isLogin = true;  
  51.         } catch (SocketException e) {  
  52.             e.printStackTrace();  
  53.             LogUtil.logPrint("连接FTP服务失败!", Constants.LOG_EXCEPTION);  
  54.             LogUtil.logPrint(e.getMessage(), Constants.LOG_EXCEPTION);  
  55.         } catch (IOException e) {  
  56.             e.printStackTrace();  
  57.             LogUtil.logPrint("登录FTP服务失败!", Constants.LOG_EXCEPTION);  
  58.             LogUtil.logPrint(e.getMessage(), Constants.LOG_EXCEPTION);  
  59.         }  
  60.         System.out.println(this.ftpClient.getBufferSize());  
  61.         this.ftpClient.setBufferSize(1024 * 2);  
  62.         this.ftpClient.setDataTimeout(2000);  
  63.         return isLogin;  
  64.     }  
  65.   
  66.     /** 
  67.      * 退出并关闭FTP连接 
  68.      *  
  69.      */  
  70.     public void close() {  
  71.         if (null != this.ftpClient && this.ftpClient.isConnected()) {  
  72.             try {  
  73.                 boolean reuslt = this.ftpClient.logout();// 退出FTP服务器  
  74.                 if (reuslt) {  
  75.                     LogUtil.info("退出并关闭FTP服务器的连接");  
  76.                 }  
  77.             } catch (IOException e) {  
  78.                 e.printStackTrace();  
  79.                 LogUtil.exception("退出FTP服务器异常!");  
  80.                 LogUtil.exception(e.getMessage());  
  81.             } finally {  
  82.                 try {  
  83.                     this.ftpClient.disconnect();// 关闭FTP服务器的连接  
  84.                 } catch (IOException e) {  
  85.                     e.printStackTrace();  
  86.                     LogUtil.exception("关闭FTP服务器的连接异常!");  
  87.                     LogUtil.exception(e.getMessage());  
  88.                 }  
  89.             }  
  90.         }  
  91.     }  
  92.   
  93.     /** 
  94.      * 检查FTP服务器是否关闭 ,如果关闭接则连接登录FTP 
  95.      *  
  96.      * @return 
  97.      */  
  98.     public boolean isOpenFTPConnection() {  
  99.         boolean isOpen = false;  
  100.         if (null == this.ftpClient) {  
  101.             return false;  
  102.         }  
  103.         try {  
  104.             // 没有连接  
  105.             if (!this.ftpClient.isConnected()) {  
  106.                 isOpen = this.ftpLogin();  
  107.             }  
  108.         } catch (Exception e) {  
  109.             e.printStackTrace();  
  110.             LogUtil.exception("FTP服务器连接登录异常!");  
  111.             LogUtil.exception(e.getMessage());  
  112.             isOpen = false;  
  113.         }  
  114.         return isOpen;  
  115.     }  
  116.   
  117.     /** 
  118.      * 设置传输文件的类型[文本文件或者二进制文件] 
  119.      *  
  120.      * @param fileType--FTPClient.BINARY_FILE_TYPE,FTPClient.ASCII_FILE_TYPE 
  121.      */  
  122.     public void setFileType(int fileType) {  
  123.         try {  
  124.             this.ftpClient.setFileType(fileType);  
  125.         } catch (IOException e) {  
  126.             e.printStackTrace();  
  127.             LogUtil.exception("设置传输文件的类型异常!");  
  128.             LogUtil.exception(e.getMessage());  
  129.         }  
  130.     }  
  131.   
  132.     /** 
  133.      * 下载文件 
  134.      *  
  135.      * @param localFilePath 
  136.      *            本地文件名及路径 
  137.      * @param remoteFileName 
  138.      *            远程文件名称 
  139.      * @return 
  140.      */  
  141.     public boolean downloadFile(String localFilePath, String remoteFileName) {  
  142.         BufferedOutputStream outStream = null;  
  143.         boolean success = false;  
  144.         try {  
  145.             outStream = new BufferedOutputStream(new FileOutputStream(  
  146.                     localFilePath));  
  147.             success = this.ftpClient.retrieveFile(remoteFileName, outStream);  
  148.         } catch (FileNotFoundException e) {  
  149.             e.printStackTrace();  
  150.         } catch (IOException e) {  
  151.             e.printStackTrace();  
  152.         } finally {  
  153.             if (outStream != null) {  
  154.                 try {  
  155.                     outStream.flush();  
  156.                     outStream.close();  
  157.                 } catch (IOException e) {  
  158.                     e.printStackTrace();  
  159.                 }  
  160.             }  
  161.         }  
  162.         return success;  
  163.     }  
  164.   
  165.     /** 
  166.      * 下载文件 
  167.      *  
  168.      * @param localFilePath 
  169.      *            本地文件 
  170.      * @param remoteFileName 
  171.      *            远程文件名称 
  172.      * @return 
  173.      */  
  174.     public boolean downloadFile(File localFile, String remoteFileName) {  
  175.         BufferedOutputStream outStream = null;  
  176.         FileOutputStream outStr = null;  
  177.         boolean success = false;  
  178.         try {  
  179.             outStr = new FileOutputStream(localFile);  
  180.             outStream = new BufferedOutputStream(outStr);  
  181.             success = this.ftpClient.retrieveFile(remoteFileName, outStream);  
  182.         } catch (FileNotFoundException e) {  
  183.             e.printStackTrace();  
  184.         } catch (IOException e) {  
  185.             e.printStackTrace();  
  186.         } finally {  
  187.             try {  
  188.                 if (null != outStream) {  
  189.                     try {  
  190.                         outStream.flush();  
  191.                         outStream.close();  
  192.                     } catch (IOException e) {  
  193.                         e.printStackTrace();  
  194.                     }  
  195.                 }  
  196.             } catch (Exception e) {  
  197.                 e.printStackTrace();  
  198.             } finally {  
  199.                 if (null != outStr) {  
  200.                     try {  
  201.                         outStr.flush();  
  202.                         outStr.close();  
  203.                     } catch (IOException e) {  
  204.                         e.printStackTrace();  
  205.                     }  
  206.   
  207.                 }  
  208.             }  
  209.         }  
  210.         return success;  
  211.     }  
  212.   
  213.     /** 
  214.      * 上传文件 
  215.      *  
  216.      * @param localFilePath 
  217.      *            本地文件路径及名称 
  218.      * @param remoteFileName 
  219.      *            FTP 服务器文件名称 
  220.      * @return 
  221.      */  
  222.     public boolean uploadFile(String localFilePath, String remoteFileName) {  
  223.         BufferedInputStream inStream = null;  
  224.         boolean success = false;  
  225.         try {  
  226.             inStream = new BufferedInputStream(new FileInputStream(  
  227.                     localFilePath));  
  228.             success = this.ftpClient.storeFile(remoteFileName, inStream);  
  229.         } catch (FileNotFoundException e) {  
  230.             e.printStackTrace();  
  231.         } catch (IOException e) {  
  232.             e.printStackTrace();  
  233.         } finally {  
  234.             if (inStream != null) {  
  235.                 try {  
  236.                     inStream.close();  
  237.                 } catch (IOException e) {  
  238.                     e.printStackTrace();  
  239.                 }  
  240.             }  
  241.         }  
  242.         return success;  
  243.     }  
  244.   
  245.     /** 
  246.      * 上传文件 
  247.      *  
  248.      * @param localFilePath 
  249.      *            本地文件 
  250.      * @param remoteFileName 
  251.      *            FTP 服务器文件名称 
  252.      * @return 
  253.      */  
  254.     public boolean uploadFile(File localFile, String remoteFileName) {  
  255.         BufferedInputStream inStream = null;  
  256.         boolean success = false;  
  257.         try {  
  258.             inStream = new BufferedInputStream(new FileInputStream(localFile));  
  259.             success = this.ftpClient.storeFile(remoteFileName, inStream);  
  260.         } catch (FileNotFoundException e) {  
  261.             e.printStackTrace();  
  262.         } catch (IOException e) {  
  263.             e.printStackTrace();  
  264.         } finally {  
  265.             if (inStream != null) {  
  266.                 try {  
  267.                     inStream.close();  
  268.                 } catch (IOException e) {  
  269.                     e.printStackTrace();  
  270.                 }  
  271.             }  
  272.         }  
  273.         return success;  
  274.     }  
  275.   
  276.     /** 
  277.      * 变更工作目录 
  278.      *  
  279.      * @param remoteDir--目录路径 
  280.      */  
  281.     public void changeDir(String remoteDir) {  
  282.         try {  
  283.             this.ftpClient.changeWorkingDirectory(remoteDir);  
  284.             LogUtil.info("变更工作目录为:" + remoteDir);  
  285.         } catch (IOException e) {  
  286.             e.printStackTrace();  
  287.             LogUtil.exception("变更工作目录为:" + remoteDir + "时出错!");  
  288.             LogUtil.exception(e.getMessage());  
  289.         }  
  290.   
  291.     }  
  292.   
  293.     /** 
  294.      * 变更工作目录 
  295.      *  
  296.      * @param remoteDir--目录路径 
  297.      */  
  298.     public void changeDir(String[] remoteDirs) {  
  299.         String dir = "";  
  300.         try {  
  301.             for (int i = 0; i < remoteDirs.length; i++) {  
  302.                 this.ftpClient.changeWorkingDirectory(remoteDirs[i]);  
  303.                 dir = dir + remoteDirs[i] + "/";  
  304.             }  
  305.             LogUtil.info("变更工作目录为:" + dir);  
  306.         } catch (IOException e) {  
  307.             e.printStackTrace();  
  308.             LogUtil.exception("变更工作目录为:" + dir + "时出错!");  
  309.             LogUtil.exception(e.getMessage());  
  310.         }  
  311.   
  312.     }  
  313.   
  314.     /** 
  315.      * 返回上级目录 
  316.      *  
  317.      */  
  318.     public void toParentDir(String[] remoteDirs) {  
  319.         try {  
  320.             for (int i = 0; i < remoteDirs.length; i++) {  
  321.                 this.ftpClient.changeToParentDirectory();  
  322.             }  
  323.             LogUtil.info("返回上级目录");  
  324.         } catch (IOException e) {  
  325.             e.printStackTrace();  
  326.             LogUtil.exception("返回上级目录时出错!");  
  327.             LogUtil.exception(e.getMessage());  
  328.         }  
  329.     }  
  330.   
  331.     /** 
  332.      * 返回上级目录 
  333.      *  
  334.      */  
  335.     public void toParentDir() {  
  336.         try {  
  337.             this.ftpClient.changeToParentDirectory();  
  338.             LogUtil.info("返回上级目录");  
  339.         } catch (IOException e) {  
  340.             e.printStackTrace();  
  341.             LogUtil.exception("返回上级目录时出错!");  
  342.             LogUtil.exception(e.getMessage());  
  343.         }  
  344.     }  
  345.   
  346.     /** 
  347.      * 获得FTP 服务器下所有的文件名列表 
  348.      *  
  349.      * @param regex 
  350.      * @return 
  351.      */  
  352.     public String[] getListFiels() {  
  353.         String files[] = null;  
  354.         try {  
  355.             files = this.ftpClient.listNames();  
  356.         } catch (IOException e) {  
  357.             e.printStackTrace();  
  358.         }  
  359.         return files;  
  360.     }  
  361.   
  362.     public FTPClient getFtpClient() {  
  363.         return ftpClient;  
  364.     }  
  365.   
  366.     public FTPModel getFtpModel() {  
  367.         return ftpModel;  
  368.     }  
  369.   
  370.     public void setFtpModel(FTPModel ftpModel) {  
  371.         this.ftpModel = ftpModel;  
  372.     }  
  373.   
  374. }  

 2。采用FTP4J:

Java代码  收藏代码
  1. /** 
  2.  * ftp4j FTP操作共公类 
  3.  *  
  4.  * @author 张明学 
  5.  *  
  6.  */  
  7. public class FTP4JCommon {  
  8.     FTPClient ftpClient = null;  
  9.   
  10.     FTPModel ftpModel = null;  
  11.   
  12.     public FTP4JCommon() {  
  13.   
  14.     }  
  15.   
  16.     public FTP4JCommon(FTPModel ftpModel) {  
  17.         this.ftpModel = ftpModel;  
  18.     }  
  19.   
  20.     /** 
  21.      * 连接并登录FTP服务器 
  22.      *  
  23.      */  
  24.     public boolean ftpLogin() {  
  25.         ftpClient = new FTPClient();  
  26.         try {  
  27.             // 建立连接  
  28.             ftpClient.connect(ftpModel.getUrl());  
  29.             ftpClient.setType(FTPClient.TYPE_AUTO);  
  30.             ftpClient.setCharset("GBK");  
  31.         } catch (Exception e) {  
  32.             e.printStackTrace();  
  33.             LogUtil.infoOutPut("与FTP服务器建立连接失败!");  
  34.             LogUtil.exception(e.getMessage());  
  35.         }  
  36.         try {  
  37.             ftpClient.login(ftpModel.getUsername(), ftpModel.getPassword());  
  38.         } catch (Exception e) {  
  39.             e.printStackTrace();  
  40.             LogUtil.infoOutPut("登录FTP服务器失败!");  
  41.             LogUtil.exception(e.getMessage());  
  42.         }  
  43.         return true;  
  44.     }  
  45.   
  46.     /** 
  47.      * 退出并关闭FTP连接 
  48.      *  
  49.      */  
  50.     public void close() {  
  51.         if (null != ftpClient) {  
  52.             try {  
  53.                 ftpClient.disconnect(true);  
  54.             } catch (Exception e) {  
  55.                 e.printStackTrace();  
  56.                 LogUtil.infoOutPut("安全退出FTP服务时异常!");  
  57.                 LogUtil.exception(e.getMessage());  
  58.             }  
  59.         }  
  60.     }  
  61.   
  62.     /** 
  63.      * 下载文件 
  64.      *  
  65.      * @param localFilePath 
  66.      *            本地文件名及路径 
  67.      * @param remoteFileName 
  68.      *            远程文件名称 
  69.      * @return 
  70.      * @throws Exception 
  71.      */  
  72.     public void downloadFile(String localFilePath, String remoteFileName)  
  73.             throws Exception {  
  74.         File localFile = new File(localFilePath);  
  75.         try {  
  76.             ftpClient.download(remoteFileName, localFile);  
  77.         } catch (Exception e) {  
  78.             e.printStackTrace();  
  79.             LogUtil.infoOutPut("下载" + remoteFileName + "时出现异常!");  
  80.             LogUtil.exception(e.getMessage());  
  81.             throw e;  
  82.         }  
  83.     }  
  84.   
  85.     /** 
  86.      * 下载文件 
  87.      *  
  88.      * @param localFilePath 
  89.      *            本地文件名及路径 
  90.      * @param remoteFileName 
  91.      *            远程文件名称 
  92.      * @return 
  93.      * @throws Exception 
  94.      */  
  95.     public void downloadFile(File localFile, String remoteFileName)  
  96.             throws Exception {  
  97.         try {  
  98.             ftpClient.download(remoteFileName, localFile);  
  99.         } catch (Exception e) {  
  100.             e.printStackTrace();  
  101.             LogUtil.infoOutPut("下载" + remoteFileName + "时出现异常!");  
  102.             LogUtil.exception(e.getMessage());  
  103.             throw e;  
  104.         }  
  105.     }  
  106.   
  107.     /** 
  108.      * 获得FTP 服务器下所有的文件名列表 
  109.      *  
  110.      * @param regex 
  111.      * @return 
  112.      */  
  113.     public String[] getListFiels() {  
  114.         String fileNames[] = null;  
  115.         try {  
  116.             fileNames = this.ftpClient.listNames();  
  117.         } catch (Exception e) {  
  118.             e.printStackTrace();  
  119.             LogUtil.infoOutPut("获取文件名列表时出现异常!");  
  120.             LogUtil.exception(e.getMessage());  
  121.         }  
  122.         return fileNames;  
  123.     }  
  124.   
  125.     public FTPModel getFtpModel() {  
  126.         return ftpModel;  
  127.     }  
  128.   
  129.     public void setFtpModel(FTPModel ftpModel) {  
  130.         this.ftpModel = ftpModel;  
  131.     }  
  132.   
  133.     public FTPClient getFtpClient() {  
  134.         return ftpClient;  
  135.     }  
  136.   
  137. }  

 3。采用:jftp下载

Java代码  收藏代码
  1. /** 
  2.  * jftp FTP下载操作 
  3.  *  
  4.  * @author 张明学 
  5.  *  
  6.  */  
  7. public class JFTPDownloadCommon implements ConnectionListener {  
  8.   
  9.     private boolean isThere = false;  
  10.   
  11.     public static long time = 0;  
  12.   
  13.     private FTPModel ftpModel = null;  
  14.   
  15.     private ConnectionHandler handler = new ConnectionHandler();  
  16.   
  17.     private FtpConnection ftpcon = null;  
  18.   
  19.     public JFTPDownloadCommon(FTPModel ftpModel) {  
  20.         this.ftpModel = ftpModel;  
  21.         // 登录FTP  
  22.         this.ftpLogin();  
  23.     }  
  24.   
  25.     /** 
  26.      * 连接并登录FTP服务器 
  27.      *  
  28.      */  
  29.     public boolean ftpLogin() {  
  30.         ftpcon = new FtpConnection(this.ftpModel.getUrl());  
  31.         ftpcon.addConnectionListener(this);  
  32.         ftpcon.setConnectionHandler(handler);  
  33.         // 登录  
  34.         ftpcon.login(ftpModel.getUsername(), ftpModel.getPassword());  
  35.         while (!isThere) {  
  36.             try {  
  37.                 Thread.sleep(10);  
  38.             } catch (Exception ex) {  
  39.                 ex.printStackTrace();  
  40.             }  
  41.         }  
  42.         LogUtil.infoOutPut("是否登录成功:" + isThere);  
  43.         return isThere;  
  44.     }  
  45.   
  46.     /** 
  47.      * 关闭与FTP服务器的连接 
  48.      *  
  49.      */  
  50.     public void close() {  
  51.         ftpcon.disconnect();  
  52.         LogUtil.infoOutPut("关闭与FTP的连接");  
  53.     }  
  54.   
  55.     /** 
  56.      * 获得FTP 服务器下所有的文件名列表 
  57.      *  
  58.      * @param regex 
  59.      * @return 
  60.      */  
  61.     public String[] getListFiels() {  
  62.         ftpcon.exists("");  
  63.         Vector fileNameVector = ftpcon.currentFiles;  
  64.         String[] fileNames = new String[fileNameVector.size()];  
  65.         int i = 0;  
  66.         for (Iterator iter = fileNameVector.iterator(); iter.hasNext();) {  
  67.             String name = (String) iter.next();  
  68.             fileNames[i] = name;  
  69.             i++;  
  70.         }  
  71.         return fileNames;  
  72.     }  
  73.   
  74.     /** 
  75.      * 将FTP服务器上的file下载为bype型数据 
  76.      *  
  77.      * @param remoteFileName 
  78.      *            文件名 
  79.      * @return 
  80.      */  
  81.     public byte[] downloadToBinary(String remoteFileName) {  
  82.         Settings.bufferSize = 16384;  
  83.         long current = System.currentTimeMillis();  
  84.         byte[] bytes = null;  
  85.         try {  
  86.             InputStream is = ftpcon.getDownloadInputStream(remoteFileName);  
  87.             ByteArrayOutputStream bais = new ByteArrayOutputStream();  
  88.             int bit = 0;  
  89.             while ((bit = is.read()) != -1) {  
  90.                 bais.write(bit);  
  91.             }  
  92.             bytes = bais.toByteArray();  
  93.         } catch (Exception e) {  
  94.         }  
  95.         time = (System.currentTimeMillis() - current);  
  96.         System.out.println("下载花费时间:" + time + "ms.");  
  97.         return bytes;  
  98.     }  
  99.   
  100.     /** 
  101.      * 下载FTP服务器文件 
  102.      *  
  103.      * @param remoteFileName 
  104.      *            FTP服务器文件名 
  105.      * @param localFile 
  106.      *            本地文件名 
  107.      * @return 
  108.      * @throws Exception 
  109.      */  
  110.     public void downloadToBinary(String remoteFileName, File localFile)  
  111.             throws Exception {  
  112.         Settings.bufferSize = 16384;  
  113.         byte[] bytes = null;  
  114.         InputStream is = ftpcon.getDownloadInputStream(remoteFileName);  
  115.         ByteArrayOutputStream bais = new ByteArrayOutputStream();  
  116.         int bit = 0;  
  117.         while ((bit = is.read()) != -1) {  
  118.             bais.write(bit);  
  119.         }  
  120.         bytes = bais.toByteArray();  
  121.         CopyByteDataToLoacal(localFile, bytes);  
  122.   
  123.     }  
  124.   
  125.     /** 
  126.      * 将二进制文件下载到本地 
  127.      *  
  128.      * @param localFile 
  129.      *            目标文件名 
  130.      * @param fileDatas 
  131.      *            文件数据 
  132.      * @throws IOException 
  133.      */  
  134.     public void CopyByteDataToLoacal(File localFile, byte[] fileDatas)  
  135.             throws IOException {  
  136.         FileOutputStream fileOutStream = null;  
  137.         BufferedOutputStream bufferOutStream = null;  
  138.         try {  
  139.             if (localFile.exists()) {  
  140.                 localFile.delete();  
  141.             }  
  142.             fileOutStream = new FileOutputStream(localFile);  
  143.             bufferOutStream = new BufferedOutputStream(fileOutStream);  
  144.             bufferOutStream.write(fileDatas);  
  145.         } catch (FileNotFoundException e) {  
  146.             LogUtil.exception(e.getMessage());  
  147.             throw e;  
  148.         } catch (IOException e) {  
  149.             LogUtil.exception(e.getMessage());  
  150.             throw e;  
  151.         } finally {  
  152.             try {  
  153.                 if (null != bufferOutStream) {  
  154.                     bufferOutStream.flush();  
  155.                     bufferOutStream.close();  
  156.                 }  
  157.             } catch (IOException e) {  
  158.                 e.printStackTrace();  
  159.             } finally {  
  160.                 if (null != fileOutStream) {  
  161.                     try {  
  162.                         fileOutStream.flush();  
  163.                         fileOutStream.close();  
  164.                     } catch (IOException e) {  
  165.                         e.printStackTrace();  
  166.                     }  
  167.                 }  
  168.             }  
  169.         }  
  170.     }  
  171.   
  172.     public void connectionFailed(BasicConnection arg0, String arg1) {  
  173.         LogUtil.infoOutPut("与FTP服务器连接失败!");  
  174.     }  
  175.   
  176.     public void connectionInitialized(BasicConnection arg0) {  
  177.         isThere = true;  
  178.     }  
  179.   
  180.     public void updateRemoteDirectory(BasicConnection arg0) {  
  181.     }  
  182.   
  183.     public void updateProgress(String arg0, String arg1, long arg2) {  
  184.     }  
  185.   
  186.     public void actionFinished(BasicConnection arg0) {  
  187.     }  
  188.   
  189. }  

上面都用到了一个ftpModel如下(get,set方法省了):

Java代码  收藏代码
  1. /** 
  2.  * FTP实体对象 
  3.  *  
  4.  * @author 张明学 
  5.  *  
  6.  */  
  7. public class FTPModel {  
  8.   
  9.     private String ftpId;  
  10.   
  11.     private String username;  
  12.   
  13.     private String password;  
  14.   
  15.     private String url;  
  16.   
  17.     private int port;  
  18.   
  19.     private String remoteDir;  
  20.   
  21.     public FTPModel() {  
  22.   
  23.     }  
  24.   
  25.     public FTPModel(String username, String password, String url, int port,  
  26.             String remoteDir) {  
  27.         this.username = username;  
  28.         this.password = password;  
  29.         this.url = url;  
  30.         this.port = port;  
  31.         this.remoteDir = remoteDir;  
  32.     }  
  33. }  
 

上述仅仅列出了一点常用的操作,更多的操作需要参看它的们API文件。

 

还有,我采用的Serv-U作为FTP服务器,按装之后新建用户不法登录。后来找到了原因是:要把IIS服务中的FTP服务关掉。

分享到:
评论

相关推荐

    详解ftp创建文件权限问题

    详解ftp创建文件权限问题 一、问题 有一个这样的需求,admin为一个Linux为其FTP应用创建的一个有权限限制的用户,通过admin用户可以进行登录FTP服务,登录FTP服务后,创建文件夹,该文件夹的用户和用户组都是admin,...

    一个FTP客户端

    如果用本FTP客户端登录某个FTP服务器,则先必须申请一个FTP空间,申请FTP空间时,它会给你一个FTP主机地址、用户名称、用户密码。 测试步骤: 1、项目编译并运行。 2、申请一个FTP空间(这里不讨论)。 3、输入FTP...

    FTP服务器FTP服务器

    FTP服务器FTP服务器FTP服务器FTP服务器FTP服务器FTP服务器FTP服务器FTP服务器

    iis ftp 多用户隔离实现方法(根目录)

    首先需要取消“站点属性/允许匿名连接”,如图1。  接着进入“计算机管理/本地用户和组...回到IIS管理器,选择一个FTP站点“新建/虚拟目录/下一步”在别名处输入FTP01,选择“下一步”,路径选择刚刚创建的FTP01目

    windows下FTP匿名登录或弱口令漏洞及服务加固

    FTP 弱口令或匿名登录漏洞,一般指使用 FTP 的用户启用了匿名登录功能,或系统口令的长度太短、复杂度不够、仅包含数字、或仅包含字母等,容易被黑客攻击,发生恶意文件上传或更严重的入侵行为。 漏洞危害 黑客利用...

    Linux下安装卸载ftp的方法

    ftp是liunx下常用的软件之一。 1.将用户切换到root:su - ; 2.检查机器是否已经安装了ftp:rpm -qa|grep vsftpd;  如果没有返回任何结果,表示没有安装;如果返回文件包名,这表示已经安装了该服务; 3.开始安装:...

    密探ftp,多线程ftp.zip

    密探ftp,多线程ftp.zip密探ftp,多线程ftp.zip密探ftp,多线程ftp.zip密探ftp,多线程ftp.zip密探ftp,多线程ftp.zip密探ftp,多线程ftp.zip密探ftp,多线程ftp.zip密探ftp,多线程ftp.zip密探ftp,多线程ftp.zip...

    ftp4j-一个开源的支持代理的FTP组件

    ftp4j是个很年轻的开源项目,但是试用后发现很好很强大,如果你找一个纯java的FTP库,要支持socks4,socks4a,socks5,http代理,就是他了! 比apache的FTPClient(不支持代理)、半商业的edtFTPj(PRO支持代理,...

    TYPSoft FTP ServerV1.11 简体中文版

    市面上比较常见的FTP搭建工具都过于专业,例如IIS的FTP功能,SERV-U软件等。虽然使用这些软件建立的FTP在功能上非常强大,但却存在两个明显不足,第一是FTP服务器初始工作比较繁琐,需要具备一定的计算机水平才能...

    FTP服务器源码(C#版web端和后台)纯代码

    目前网络上有很多专业的FTP服务器软件,但是基于C#开发的版本很少,功能很全的源码就更少了,为此我专门将该资源分享出来,希望能给真正需要的读者提供微薄的帮助。 本软件简介: 1.具备FTP服务器的基本功能,如文件...

    (Java)FTP多线程下载源代码

    采用apache commons开发包,实现了FTP多... 另外要注意的是,输入的FTP目录和本地目录要正确,因为程序中没有处理这类异常(呵呵呵,毕竟这不是主要目的),范例 FTP目录:\FTP\hello.txt(\代表FTP根目录) 本地目录:C:\

    webftp.zip ftp源码

    webftp.zip ftp源码 webftp.zip ftp源码 webftp.zip ftp源码 webftp.zip ftp源码 webftp.zip ftp源码

    最新FTP 服务器 最新FTP 服务器

    Serv-U是一种被广泛运用的FTP服务器端软件,支持3x/9x/ME/NT/2K等全Windows系列。可以设定多个FTP服务器、限定登录用户的权限、登录主目录及空间大小等,功能非常完备。 它具有非常完备的安全特性,支持SSl FTP传输...

    计算机网络课程设计:简单FTP客户端软件开发

    而文件传送协议FTP是因特网上使用得最广泛的文件传送协议。FTP使用客户服务器方式。 设计要求: 1) 以命令行形式运行 2) 该FTP客户端程序具有以下基本功能:能完成FTP链接的打开和关闭操作;能执行文件的上传和...

    ftp检测\ftp扫描

    ftp检测\ftp扫描

    vs2010写的一个简单的FTP客户端

    FtpC是用vs2010写的一个简单的FTP客户端,说明CFtpConnection的基本用法,大家可参照它写出长期自动获取远程FTP服务器上数据的应用。如果你用vc6.0,只要把这个工程中的原理性代码复制过去就行了。 测试时FTP服务器...

    自己收集的多个Java FTP断点续传的例子源码

    ftp上传下载 java FTPClient - - ITeye技术网站 (2012年5月21日) FTP操作类:FTPClient - 石川 - 博客园 (2012年5月21日) JAVA中使用FTPClient上传下载 - hbcui1984的专栏 - 博客频道 - CSDN.NET (2012年5月21日) ...

    除非Microsoft FTP 服务(FTPSVC)正在运行,否则无法启动FTP站点。服务目前已停止。

    当出现:除非Microsoft FTP 服务(FTPSVC)正在运行,否则无法启动FTP站点。服务目前已停止。(针对win10系统,亲测成功。其他系统应该差不多) 即如下提示时: 原因是:在自己电脑上搭建的ftp服务器,每次断电开机...

    java编写的ftp文件实时监控下载上传

    用java语言编写的ftp小工具,可以按指定时间监控ftp服务器,把服务器指定目录内新产生的文件或者文件夹下载到本地指定文件夹,下载后删除数据。 也可以监控本地文件夹,把文件夹内新产生的文件或者文件夹整体上传到...

    FTP服务器单文件绿色版FTPServer

    FTP服务器单文件绿色版FTPServer

Global site tag (gtag.js) - Google Analytics