和其他 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()
}
}