当前位置:web 服务器 > Apache >>

Apache common FTP

  import java.io.File;

    import java.io.FileOutputStream;

    import java.io.IOException;

    import java.io.InputStream;

    import java.io.OutputStream;

    import java.io.RandomAccessFile;

    import java.net.SocketException;

    import java.util.ArrayList;

    import java.util.List;

    import org.apache.commons.logging.Log;

    import org.apache.commons.logging.LogFactory;

    import org.apache.commons.net.ftp.FTP;

    import org.apache.commons.net.ftp.FTPClient;

    import org.apache.commons.net.ftp.FTPFile;

    import org.apache.commons.net.ftp.FTPReply;

    

    /**

     * FTP工具类

     * @author WangXianfeng 1:16:50 PM Jul 16, 2011

    

     */

    

    public class FtpUtil {

           private FTPClient ftpClient;

           private static Log log = LogFactory.getLog(FtpUtil.class);

           /**

    

            * 根路径为"/",如果需要链接服务器之后跳转到路径,则在path中定义

    

            * @param ftpConfig

    

            * @throws SocketException

    

            * @throws IOException

    

            */

    

           public boolean connectServer(FtpConfig ftpConfig) throws SocketException,

                         IOException {

                  String server = ftpConfig.getServer();

                  int port = ftpConfig.getPort();

                  String user = ftpConfig.getUsername();

                  String password = ftpConfig.getPassword();

                  String path = ftpConfig.getPath();

                  return connectServer(server, port, user, password, path);

           }

    

           /**

    

            * 连接ftp服务器

    

            * @param server 服务器ip

    

            * @param port 端口,通常为21

    

            * @param user 用户名

    

            * @param password 密码

    

            * @param path 进入服务器之后的默认路径

    

            * @return 连接成功返回true,否则返回false

    

            * @throws SocketException

    

            * @throws IOException

    

            */

    

           public boolean connectServer(String server, int port, String user,

                         String password, String path) throws SocketException, IOException {

                  ftpClient = new FTPClient();

                  ftpClient.connect(server, port);

                  ftpClient.setControlEncoding("GBK");

                  log.info("Connected to " + server + ".");

                  log.info("FTP server reply code:" + ftpClient.getReplyCode());

                  if (FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) {

                         if (ftpClient.login(user, password)) {

                                // Path is the sub-path of the FTP path

                                if (path.length() != 0) {

                                       ftpClient.changeWorkingDirectory(path);

                                }

                                return true;

    

                         }

    

 

补充:软件开发 , Java ,
Apache
IIS
Nginx
Tomcat
如果你遇到web 服务器难题:
访问www.zzzyk.com 试试
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,