2024年6月2日发(作者:)
FTP客户端下载程序文档
一.开发环境描述及其相关配置:
由于java语言的平台无关性,所以我选取在windows操作系统上做的开发。
操作系统:Microsoft Windows XP
JAVA开发包:JDK1.6
开发工具:MyEclipse6.5
二.程序的模块功能描述
本程序主要是一个一个FTP客户端下载程序,主要以实现通过用户名密码与
FTP服务器进行连接,断开与服务器的连接,文件的上传与下载等功能。。。
这程序由三个部分组成,程序的主窗体和一下可视化的功能,
对FTP的连接,上传文件,下载文件的方法进行了封装。
类对窗体的设置和布局。主意用到了集合类,流对
文件的操作和文件的上传下载,..*f对FTP的操作。
三.主要程序代码:
FTP的连接:
public String connect(String dir, String hostname, int port, String username,String
passwd)
{
String msg = "";
try
{
ftpClient = new FtpClient(hostname, port);
(username, passwd);
();
msg = "Success";
} catch (FtpLoginException e)
{
msg = "登录主机失败,可能是用户名密码错误!";
ftpClient=null;
}
catch (IOException e)
{
msg = "登录主机失败,请检验端品是否正确!";
ftpClient=null;
}
catch (SecurityException e)
{
msg = "无权连接主机,主确认是否有权限连接主机!";
ftpClient=null;
}
连接ftp的程序需要五个参数分别,当前目录,服务器命,端口号,用户名,密
码进行连接 如果连接成功则设置Success否则就失败。
文件上传:public boolean uploadFile(String localFile, String targetFileName)
{
boolean result = false;
if (ent == null)
{
return false;
}
TelnetOutputStream tos = null;
RandomAccessFile sendFile = null;
DataOutputStream dos = null;
try
{
File file = new File(localFile);
sendFile = new RandomAccessFile(file, "r");
(0);
tos = (targetFileName);
dos = new DataOutputStream(tos);
int ch = 0;
while (ePointer() < ())
{
ch = ();
(ch);
}
result = true;
}
文件下载:public boolean downloadFile(String srcFileName, String targetFileName)
{
if (ent == null)
{
return false;
}
TelnetInputStream tis = null;
RandomAccessFile getFile = null;
boolean result = true;
try
{
File file = new File(targetFileName);
getFile = new RandomAccessFile(file, "rw");
(0);
tis = (srcFileName);
DataInputStream dis = new DataInputStream(tis);
int ch = 0;
while (true)
{
ch = ();
if (ch < 0)
{
break;
}
(ch);
}
();
}
文件的上传和下载利用IO六来上传读写,需要原路径和目标路径。
心得体会:自己写
发布评论