`

java实现ftp上传,下载功能(转)

阅读更多
 本文来自:http://cd0281.iteye.com/blog/682103
package download;

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

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

public class Download {
	String localfilename;

	String remotefilename;

	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() {

		this.localfilename = "D://test2//test.txt";
		this.remotefilename = "test.txt";

		try {
			TelnetOutputStream os = ftpClient.put(this.remotefilename);
			java.io.File file_in = new java.io.File(this.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" };

		Download fu = new Download();
		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();

	}
}

 

另一篇:http://vipjustin.blog.163.com/blog/static/19966227200732734834722/

分享到:
评论

相关推荐

    java实现ftp上传和下载文件功能.

    java实现ftp上传和下载文件功能 文档里有详细的代码

    java中实现ftp下载功能

    java中实现ftp下载功能,支持断点下载

    Java实现FTP的上传下载功能

    Java实现FTP的上传下载功能

    java实现ftp功能

    java实现ftp功能 上传功能

    用Java实现FTP批量大文件上传下载

    用Java实现FTP批量大文件上传下载(一) 本文介绍了在Java中,如何使用Java现有的可用的库来编写FTP客户端代码,并开发成Applet控件,做成基于Web的批量、大文件的上传下载控件。文章在比较了一系列FTP客户库的基础上...

    山东大学计算机网络课程设计Java实现FTP的类似的功能

    山东大学计算机网络课程设计,利用Java实现类似于FTP的相关功能,用户登录,用户上传文件,下载文件,删除文件。使用Javasocket的作为传输底层。

    使用java实现的linux和ftp服务器文件上传下载工具

    这是我使用java实现的linux和ftp服务器文件上传下载工具,需要电脑安装jdk8, 启动命令,java -jar linuxAndFtp.jar 启动成功后,浏览器访问:http://localhost:9999 服务器的账号密码通过服务器列表页面管理,添加的...

    Java实现ftp上传

    使用java实现的操作ftp等一系列功能,比较完整,全面。放心下载

    java实现文件上传到FTP和从FTP下载到本地功能

    java实现文件上传到FTP和从FTP下载到本地功能,里面没有jar架包,但有说明要哪些架包

    如何在Java程序中实现FTP的上传下载功能.txt

    如何在Java程序中实现FTP的上传下载功能.txt FtpList部分是用来显示FTP服务器上的文件; GetButton部分为从FTP服务器下传一个文件; PutButton部分为向FTP服务器上传一个文件 ………………………………...

    java中FTP上传

    利用java进行ftp上传文件,通过java代码实现ftp上传文件的功能。

    如何在JAVA程序中实现FTP的功能

    在JAVA的编程中,您也许会遇到FTP方面的编程,本文就来演示如何实现它。本程序是由JBUILDER2.0来开发的, FtpList 部分是用来显示FTP服务器上的文件(附图略)。GetButton部分为从FTP服务器下传一个文件。PutButton...

    JAVA程序中实现FTP的功能

    在JAVA的编程中,也许会遇到FTP方面的编程,本文就来演示如何实现它。本程序是由JBUILDER2.0来开发的,为了节约篇幅我只列出主要的三个部份。FtpList 部分是用来显示FTP服务器上的文件(附图略)。GetButton部分为从...

    java ftp上传和下载

    看了很多别人写的java连接ftp服务器的上传和下载,都没有能够实现的需要的功能。自己写了一个连接ftp服务器,并实现定时上传和下载。已经实现,正在使用,很好用的。

    java实现FTP批量大文件下载

    本文介绍了在Java中,如何使用Java现有的可用的库来编写FTP客户端代码,并开发成Applet控件,做成基于Web的批量、大文件的上传下载控件。文章在比较了一系列FTP客户库的基础上,就其中一个比较通用且功能较强的j-ftp...

    java ftp上传 下载,获取文件集合

    java 连接linux系统上的ftp服务器,通过java代码上传,下载文件 通过链接ftp,获取文件列表,上传。下载、删除文件功能实现

    javaFTP软件

    一款JAVA手机上连接FTP的软件;很适合手机党建站的需求;此程序具有很多功能;能上传和在线编辑源码文件

    java Ftp上传下载工具类

    使用Apache commons net组件实现ftp上传与下载功能

Global site tag (gtag.js) - Google Analytics