用户注册



邮箱:

密码:

用户登录


邮箱:

密码:
记住登录一个月忘记密码?

发表随想


还能输入:200字
云代码 - java代码库

java ftp服务器

2013-05-11 作者: 神马举报

[java]代码库

/*
  * To change this template, choose Tools | Templates
  * and open the template in the editor.
  */
package test;

import com.enterprisedt.net.ftp.FTPClient;
import com.enterprisedt.net.ftp.FTPConnectMode;
import com.enterprisedt.net.ftp.FTPMessageCollector;
import com.enterprisedt.net.ftp.FTPTransferType;
import com.enterprisedt.util.debug.Logger;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.net.URL;

/**
 *
 * @author Icer
 */
public class FtpClient {

    private FTPClient ftp = null;

    public FTPClient connFtpServer(String host, int port, String username, String password) {
        ftp = new FTPClient();
        try {

            ftp.setRemoteHost(host);
            ftp.setRemotePort(port);
            FTPMessageCollector listener = new FTPMessageCollector();
            ftp.setMessageListener(listener);

            System.out.println("Connecting");
            ftp.connect();

            System.out.println("Logging in");
            ftp.login(username, password);

            System.out.println("Setting up passive,ASCII transfers");
            ftp.setConnectMode(FTPConnectMode.PASV);
            ftp.setType(FTPTransferType.ASCII);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return ftp;
    }

    public void upload(String localfile, URL url) {
        String host = url.getHost();
        int port = url.getPort();
        File filePath = new File(url.getPath());
        String directory = filePath.getParent().substring(1);
        String filename = filePath.getName();
        try {
            ftp.chdir(directory);
            ftp.put(new FileInputStream(localfile), filename);
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("upload failed");
        }
    }


    public void upload(String localpath, String filename) {
        try {
            if(ftp.connected()) {

                ftp.put(localpath, filename);
                ftp.dir();

                System.out.println("hello");
            } else {
                System.out.println("wrong");
            }

        } catch (Exception e) {
            System.out.println("upload failed");
            e.printStackTrace();
        }
    }

    public void DownloadFile(String localpath, String filename) {
        try {
            ftp.chdir("");
            String[] files = ftp.dir("", true);

            for (int i = 0; i < files.length; i++) {

                System.out.println(files[i]);

                ftp.get(new FileOutputStream(localpath + filename), filename);

            }
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("upload failed");
        }
    }

    public void disConn() {
        try {
            ftp.quit();
        } catch (Exception e) {
            System.out.println("disconnection failed");
            e.printStackTrace();
        }
    }


    public static void main(String[] args) throws Exception {

    }
}


网友评论    (发表评论)


发表评论:

评论须知:

  • 1、评论每次加2分,每天上限为30;
  • 2、请文明用语,共同创建干净的技术交流环境;
  • 3、若被发现提交非法信息,评论将会被删除,并且给予扣分处理,严重者给予封号处理;
  • 4、请勿发布广告信息或其他无关评论,否则将会删除评论并扣分,严重者给予封号处理。


扫码下载

加载中,请稍后...

输入口令后可复制整站源码

加载中,请稍后...