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

Python021

如何在Java程序中实现FTP的上传下载功能,第1张

以下是这三部分的JAVA源程序: (1)显示FTP服务器上的文件 void ftpList_actionPerformed(ActionEvent e) {String server=serverEdit.getText()//输入的FTP服务器的IP地址 String user=userEdit.getText()//登录FTP服务器的用户名 String password=passwordEdit.getText()//登录FTP服务器的用户名的口令 String path=pathEdit.getText()//FTP服务器上的路径 try {FtpClient ftpClient=new FtpClient()//创建FtpClient对象 ftpClient.openServer(server)//连接FTP服务器 ftpClient.login(user, password)//登录FTP服务器 if (path.length()!=0) ftpClient.cd(path)TelnetInputStream is=ftpClient.list()int cwhile ((c=is.read())!=-1) { System.out.print((char) c)} is.close()ftpClient.closeServer()//退出FTP服务器 } catch (IOException ex) {} } (2)从FTP服务器上下传一个文件 void getButton_actionPerformed(ActionEvent e) { String server=serverEdit.getText()String user=userEdit.getText()String password=passwordEdit.getText()String path=pathEdit.getText()String filename=filenameEdit.getText()try { FtpClient ftpClient=new FtpClient()ftpClient.openServer(server)ftpClient.login(user, password)if (path.length()!=0) ftpClient.cd(path)ftpClient.binary()TelnetInputStream is=ftpClient.get(filename)File file_out=new File(filename)FileOutputStream os=new FileOutputStream(file_out)byte[] bytes=new byte[1024]int cwhile ((c=is.read(bytes))!=-1) { os.write(bytes,0,c)} is.close()os.close()ftpClient.closeServer()} catch (IOException ex) {} } (3)向FTP服务器上上传一个文件 void putButton_actionPerformed(ActionEvent e) { String server=serverEdit.getText()String user=userEdit.getText()String password=passwordEdit.getText()String path=pathEdit.getText()String filename=filenameEdit.getText()try { FtpClient ftpClient=new FtpClient()ftpClient.openServer(server)ftpClient.login(user, password)if (path.length()!=0) ftpClient.cd(path)ftpClient.binary()TelnetOutputStream os=ftpClient.put(filename)File file_in=new File(filename)FileInputStream is=new FileInputStream(file_in)byte[] bytes=new byte[1024]int cwhile ((c=is.read(bytes))!=-1){ os.write(bytes,0,c)} is.close()os.close()ftpClient.closeServer()} catch (IOException ex) {} } }

FTP(File Transfer Protocol)是 Internet 上用来传送文件的协议(文件传输协议)。它是为了我们能够在 Internet 上互相传送文件而制定的的文件传送标准,规定了 Internet 上文件如何传送。也就是说,通过 FTP 协议,我们就可以跟 Internet 上的 FTP 服务器进行文件的上传(Upload)或下载(Download)等动作。

和其他 Internet 应用一样,FTP 也是依赖于客户程序/服务器关系的概念。在 Internet 上有一些网站,它们依照 FTP 协议提供服务,让网友们进行文件的存取,这些网站就是 FTP 服务器。网上的用户要连上 FTP 服务器,就要用到 FPT 的客户端软件,通常 Windows 都有“ftp”命令,这实际就是一个命令行的 FTP 客户程序,另外常用的 FTP 客户程序还有 CuteFTP、Ws_FTP、FTP Explorer等。

要连上 FTP 服务器(即“登陆”),必须要有该 FTP 服务器的帐号。如果是该服务器主机的注册客户,你将会有一个 FTP 登陆帐号和密码,就凭这个帐号密码连上该服务器。但 Internet 上有很大一部分 FTP 服务器被称为“匿名”(Anonymous)FTP 服务器。这类服务器的目的是向公众提供文件拷贝服务,因此,不要求用户事先在该服务器进行登记注册。

Anonymous(匿名文件传输)能够使用户与远程主机建立连接并以匿名身份从远程主机上拷贝文件,而不必是该远程主机的注册用户。用户使用特殊的用户名“anonymous”和“guest”就可有限制地访问远程主机上公开的文件。现在许多系统要求用户将Emai1地址作为口令,以便更好地对访问进行跟综。出于安全的目的,大部分匿名FTP主机一般只允许远程用户下载(download)文件,而不允许上载(upload)文件。也就是说,用户只能从匿名FTP主机拷贝需要的文件而不能把文件拷贝到匿名FTP主机。另外,匿名FTP主机还采用了其他一些保护措施以保护自己的文件不至于被用户修改和删除,并防止计算机病毒的侵入。在具有图形用户界面的 WorldWild Web环境于1995年开始普及以前,匿名FTP一直是Internet上获取信息资源的最主要方式,在Internet成千上万的匿名PTP主机中存储着无以计数的文件,这些文件包含了各种各样的信息,数据和软件。 人们只要知道特定信息资源的主机地址, 就可以用匿名FTP登录获取所需的信息资料。虽然目前使用WWW环境已取代匿名FTP成为最主要的信息查询方式,但是匿名FTP仍是 Internet上传输分发软件的一种基本方法

上传下载的代码

import java.io.BufferedReader

import java.io.FileInputStream

import java.io.FileOutputStream

import java.io.IOException

import java.io.InputStreamReader

import sun.net.TelnetOutputStream

import sun.net.TelnetInputStream

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=0i<filepath.lengthi++){

fu.download(filepath[i],localfilepath[i])

}

//fu.upload()

//fu.download()

fu.closeConnect()

}

}