`
hao861002
  • 浏览: 84251 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

利用ftp服务器实现文件上传下载

阅读更多

利用ftp服务器实现文件上传下载

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.richeninfo.anaplatform.gether.task;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import sun.net.TelnetOutputStream;
import sun.net.TelnetInputStream;
import sun.net.ftp.FtpClient;

/**
 *
 * @author xth
 */
public class TerminalFtp {

    FtpClient ftpClient;

// server:服务器名字
// user:用户名
// password:密码
// path:服务器上的路径
    public void connectServer(String ip, int port, String user, String password, String path) {

        try {
            ftpClient = new FtpClient();
            ftpClient.openServer(ip, port);
            ftpClient.login(user, password);
            System.out.println("login success!");
            if (path.length() != 0) {
                ftpClient.cd(path);
            }
            ftpClient.binary();
        } catch (IOException ex) {
            System.out.println("not login");
            System.out.println(ex);
        }
    }

    public void closeConnect() {
        try {
            ftpClient.closeServer();
            System.out.println("disconnect success");
        } catch (IOException ex) {
            System.out.println("not disconnect");
            System.out.println(ex);
        }
    }

    public void upload(String localfilename,String remotefilename) {
//
//        this.localfilename = "D://test2//test.txt";
//        this.remotefilename = "test.txt";
//System.out.println("localfilename :"+localfilename);
//        System.out.println("remotefilename :"+remotefilename);
        try {
            TelnetOutputStream os = ftpClient.put(remotefilename);
            java.io.File file_in = new java.io.File(localfilename);
            FileInputStream is = new FileInputStream(file_in);
            byte[] bytes = new byte[1024];
            int c;
            while ((c = is.read(bytes)) != -1) {
                os.write(bytes, 0, c);
            }
            System.out.println("upload success");
            is.close();
            os.close();
        } catch (IOException ex) {
            System.out.println("not upload");
            System.out.println(ex);
        }
    }

//    public void download() {
//
//        try {
//            TelnetInputStream is = ftpClient.get(this.remotefilename);
//            java.io.File file_in = new java.io.File(this.localfilename);
//            FileOutputStream os = new FileOutputStream(file_in);
//            byte[] bytes = new byte[1024];
//            int c;
//            while ((c = is.read(bytes)) != -1) {
//// System.out.println((char)is.read());
//// System.out.println(file_in);
//                os.write(bytes, 0, c);
//            }
//
//            System.out.println("download success");
//            os.close();
//            is.close();
//        } catch (IOException ex) {
//            System.out.println("not download");
//            System.out.println(ex);
//        }
//    }

    public void download(String remotePath, String remoteFile, String localFile) {

        try {
            if (remotePath.length() != 0) {
                ftpClient.cd(remotePath);
            }
            TelnetInputStream is = ftpClient.get(remoteFile);
            java.io.File file_in = new java.io.File(localFile);
            FileOutputStream os = new FileOutputStream(file_in);
            byte[] bytes = new byte[1024];
            int c;
            while ((c = is.read(bytes)) != -1) {
// System.out.println((char)is.read());
// System.out.println(file_in);
                os.write(bytes, 0, c);
            }

            System.out.println("download success");
            os.close();
            is.close();
        } catch (IOException ex) {
            System.out.println("not download");
            System.out.println(ex);
        }
    }

    public void download(String remoteFile, String localFile) {

        try {
            TelnetInputStream is = ftpClient.get(remoteFile);
            java.io.File file_in = new java.io.File(localFile);
            FileOutputStream os = new FileOutputStream(file_in);
            byte[] bytes = new byte[1024];
            int c;
            while ((c = is.read(bytes)) != -1) {
// System.out.println((char)is.read());
// System.out.println(file_in);
                os.write(bytes, 0, c);
            }

            System.out.println("download success");
            os.close();
            is.close();
        } catch (IOException ex) {
            System.out.println("not download");
            System.out.println(ex);
        }
    }

    public static void main(String agrs[]) {

        String filepath[] = {"/callcenter/index.jsp", "/callcenter/ip.txt",
            "/callcenter/mainframe/image/processing_bar_2.gif",
            "/callcenter/mainframe/image/logo_01.jpg"};
        String localfilepath[] = {"C:\\FTP_Test\\index.jsp",
            "C:\\FTP_Test\\ip.txt", "C:\\FTP_Test\\processing_bar_2.gif",
            "C:\\FTP_Test\\logo_01.jpg"};

        TerminalFtp fu = new TerminalFtp();
        fu.connectServer("172.16.1.66", 22, "web_test", "123456", "/callcenter");
        for (int i = 0; i < filepath.length; i++) {
            fu.download(filepath[i], localfilepath[i]);
        }

//fu.upload();
//fu.download();
        fu.closeConnect();

    }
}

分享到:
评论

相关推荐

    MFC实现FTP上传下载文件的客户端和服务器

    MFC实现的利用ftp上传和下载文件的完整程序代码,包括客户端和服务器端,希望能够给需要的人以参考!

    基于strurs2文件上传,利用FTP将文件直接上传至指定的FTP服务器

    基于strurs2文件上传,利用FTP将文件直接上传至指定的FTP服务器

    ios中利用ios sdk实现上传文件到ftp服务器

    ios中利用ios sdk实现上传文件到ftp服务器

    FTP文件传输的实现课程设计报告

    设计一个ftp客户端,能与ftp服务器实现数据交换,了解ftp行命令。 ftp客户端基本功能:下载远程文件(或文件夹),上传本地文件(或文件夹),删除本地或远程文件(或文件夹)

    ios中利用libcurl上传文件到ftp服务器

    ios中利用libcurl上传文件到ftp服务器

    windows下定时利用bat脚本实现ftp上传下载

    主要为大家详细介绍了windows下定时利用bat脚本实现ftp上传和下载,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

    FTP服务器的配置与管理.doc

    FTP是互联网上的另一项主要服务,该项服务的名字 是由该服务使用的协议引申而来的,各类文件存放于FTP服务器,可以通过FTP客户程序 连接FTP服务器,然后利用FTP协议进行文件的"下载"或"上传"。 2)FTP的工作原理 FTP...

    FTP上传文件

    利用FTP将本地的资源文件上传文件到所需的服务器上。

    FTP服务器小工具

     用户可以利用它连接到FTP服务器上传、下载、查看、编辑、删除,移动文件,具有不会因闲置过久而被服务端踢出;拥有多线传输及文件续传的功能,让你可以稳定且大量的传输文件;友好的界面可以管理多个ftp站点,使用...

    Java Ftp上传下载

    近来做的一个项目因需要大量上传大文件且要求速度快,因此选择了FTP上传,在网上搜索了很多例子后并经自己对比整理后决定采用apache包中的FTP实现上传下载,在使用过程需要注意的是对中文名进行字符转码,FTP服务器...

    android代码ftp协议上传下载

    android终端利用ftp协议从ftp服务器上下载和上传文件的实现!

    FTP客户端上传,下载文件或内存

    利用MFC库实现FTP客户端部分功能:上传文件或内存数据至服务器上,从服务器上下载文件至本地文件或内存中。

    用kettle设计ftp上传操作手册

    利用kettle数据抽取工具,编写了一个输出文本数据兵上传到ftp服务器的操作手册

    FTP文件管理管理模块 java实现

    实现遍历FTP服务器目录 实现获取本地文件图标 实现利用多线程实现FTP 文件上传,下载 本地文件与FTP文件的维护操作

    Python Socketserver实现FTP文件上传下载代码实例

    1、socketserver实现ftp文件上传下载,可以同时多用户登录、上传、下载  效果图:   二、上面只演示了下载,上传也是一样的,来不及演示了,上代码 1、客户端 import socket,hashlib,os,json,sys,time class ...

    FTP客户端和服务器,ftp客户端和服务器有什么不同,Visual C++

    该程序利用socket编程实现ftp文件的上传、下载。

    VC FTP文件上传下载程序.rar

    VC FTP文件上传下载程序,利用FTP协议上传文件、下载文件的一个程序实例,主窗口采用左右框架的模式,左侧显示本地文件、右侧显示远程文件,同时可操作本地视图中的上一层、下一层目录,可选中指定文件上传下载,也...

    FTP上传下载工具

    1、用户可以利用它连接到FTP服务器上传、下载、查看、编辑、删除,移动文件,具有不会因闲置过久而被服务端踢出; 2、8UFTP拥有多线传输及文件续传的功能,让你可以稳定且大量的传输文件; 3、友好的界面可以管理...

    ftp服务器ppt.pptx

    随后,系统管理员会去检查这些文件,他会将这些文件移至另一个公共下载目录中,供其它用户下载,利用这种方式,远程主机的用户得到了保护,避免了有人上载有问题的文件。 FTP协议 ftp服务器ppt全文共38页,当前为第6...

Global site tag (gtag.js) - Google Analytics