关于java ftp程序

关于java ftp程序,第1张

第一;中的ip,
cgetPassword()),是否可以正常连接
第二getServerIp(),先确定你所要连接的ftp服务器是否可用,
cgetUserName()connect(c,可以用ftp命令在命令行或者用ftp软件测试,在确定help,如果ftp服务器正常可用这个问题很简单呀,name

sunnetftpFtpClient,该类库主要提供了用于建立FTP连接的类。利用这些类的方法,编程人员可以远程登录到FTP服务器,列举该服务器上的目录,设置传输协议,以及传送文件。FtpClient类涵盖了几乎所有FTP的功能,FtpClient的实例变量保存了有关建立"代理"的各种信息。下面给出了这些实例变量:
public static boolean useFtpProxy
这个变量用于表明FTP传输过程中是否使用了一个代理,因此,它实际上是一个标记,此标记若为TRUE,表明使用了一个代理主机。
public static String ftpProxyHost
此变量只有在变量useFtpProxy为TRUE时才有效,用于保存代理主机名。
public static int ftpProxyPort此变量只有在变量useFtpProxy为TRUE时才有效,用于保存代理主机的端口地址。
FtpClient有三种不同形式的构造函数,如下所示:
1、public FtpClient(String hostname,int port)
 此构造函数利用给出的主机名和端口号建立一条FTP连接。
2、public FtpClient(String hostname)
此构造函数利用给出的主机名建立一条FTP连接,使用默认端口号。
3、FtpClient()
此构造函数将创建一FtpClient类,但不建立FTP连接。这时,FTP连接可以用openServer方法建立。
一旦建立了类FtpClient,就可以用这个类的方法来打开与FTP服务器的连接。类ftpClient提供了如下两个可用于打开与FTP服务器之间的连接的方法。
public void openServer(String hostname)
这个方法用于建立一条与指定主机上的FTP服务器的连接,使用默认端口号。
public void openServer(String host,int port)
这个方法用于建立一条与指定主机、指定端口上的FTP服务器的连接。
打开连接之后,接下来的工作是注册到FTP服务器。这时需要利用下面的方法。
public void login(String username,String password)
此方法利用参数username和password登录到FTP服务器。使用过Intemet的用户应该知道,匿名FTP服务器的登录用户名为anonymous,密码一般用自己的电子邮件地址。
下面是FtpClient类所提供的一些控制命令。
public void cd(String remoteDirectory):该命令用于把远程系统上的目录切换到参数remoteDirectory所指定的目录。
public void cdUp():该命令用于把远程系统上的目录切换到上一级目录。
public String pwd():该命令可显示远程系统上的目录状态。
public void binary():该命令可把传输格式设置为二进制格式。
public void ascii():该命令可把传输协议设置为ASCII码格式。
public void rename(String string,String string1):该命令可对远程系统上的目录或者文件进行重命名 *** 作。
除了上述方法外,类FtpClient还提供了可用于传递并检索目录清单和文件的若干方法。这些方法返回的是可供读或写的输入、输出流。下面是其中一些主要的方法。
public TelnetInputStream list()
返回与远程机器上当前目录相对应的输入流。
public TelnetInputStream get(String filename)
获取远程机器上的文件filename,借助TelnetInputStream把该文件传送到本地。
public TelnetOutputStream put(String filename)
以写方式打开一输出流,通过这一输出流把文件filename传送到远程计算机
package myUtil;
import javaioDataInputStream;
import javaioFile;
import javaioFileInputStream;
import javaioFileOutputStream;
import javaioIOException;
import javaioOutputStream;
import javautilArrayList;
import javautilList;
import javautilStringTokenizer;
import sunnetTelnetInputStream;
import sunnetTelnetOutputStream;
import sunnetftpFtpClient;
/
ftp上传,下载

@author why 2009-07-30

/
public class FtpUtil {
private String ip = "";
private String username = "";
private String password = "";
private int port = -1;
private String path = "";
FtpClient ftpClient = null;
OutputStream os = null;
FileInputStream is = null;
public FtpUtil(String serverIP, String username, String password) {
thisip = serverIP;
thisusername = username;
thispassword = password;
}
public FtpUtil(String serverIP, int port, String username, String password) {
thisip = serverIP;
thisusername = username;
thispassword = password;
thisport = port;
}
/
连接ftp服务器

@throws IOException
/
public boolean connectServer() {
ftpClient = new FtpClient();
try {
if (thisport != -1) {
ftpClientopenServer(thisip, thisport);
} else {
ftpClientopenServer(thisip);
}
ftpClientlogin(thisusername, thispassword);
if (thispathlength() != 0) {
ftpClientcd(thispath);// path是ftp服务下主目录的子目录
}
ftpClientbinary();// 用2进制上传、下载
Systemoutprintln("已登录到\"" + ftpClientpwd() + "\"目录");
return true;
} catch (IOException e) {
eprintStackTrace();
return false;
}
}
/
断开与ftp服务器连接

@throws IOException
/
public boolean closeServer() {
try {
if (is != null) {
isclose();
}
if (os != null) {
osclose();
}
if (ftpClient != null) {
ftpClientcloseServer();
}
Systemoutprintln("已从服务器断开");
return true;
} catch (IOException e) {
eprintStackTrace();
return false;
}
}
/
检查文件夹在当前目录下是否存在

@param dir
@return
/
private boolean isDirExist(String dir) {
String pwd = "";
try {
pwd = ftpClientpwd();
ftpClientcd(dir);
ftpClientcd(pwd);
} catch (Exception e) {
return false;
}
return true;
}
/
在当前目录下创建文件夹

@param dir
@return
@throws Exception
/
private boolean createDir(String dir) {
try {
ftpClientascii();
StringTokenizer s = new StringTokenizer(dir, "/"); // sign
scountTokens();
String pathName = ftpClientpwd();
while (shasMoreElements()) {
pathName = pathName + "/" + (String) snextElement();
try {
ftpClientsendServer("MKD " + pathName + "\r\n");
} catch (Exception e) {
e = null;
return false;
}
ftpClientreadServerResponse();
}
ftpClientbinary();
return true;
} catch (IOException e1) {
e1printStackTrace();
return false;
}
}
/
ftp上传 如果服务器段已存在名为filename的文件夹,该文件夹中与要上传的文件夹中同名的文件将被替换

@param filename
要上传的文件(或文件夹)名
@return
@throws Exception
/
public boolean upload(String filename) {
String newname = "";
if (filenameindexOf("/") > -1) {
newname = filenamesubstring(filenamelastIndexOf("/") + 1);
} else {
newname = filename;
}
return upload(filename, newname);
}
/
ftp上传 如果服务器段已存在名为newName的文件夹,该文件夹中与要上传的文件夹中同名的文件将被替换

@param fileName
要上传的文件(或文件夹)名
@param newName
服务器段要生成的文件(或文件夹)名
@return
/
public boolean upload(String fileName, String newName) {
try {
String savefilename = new String(fileNamegetBytes("GBK"),
"GBK");
File file_in = new File(savefilename);// 打开本地待长传的文件
if (!file_inexists()) {
throw new Exception("此文件或文件夹[" + file_ingetName() + "]有误或不存在!");
}
if (file_inisDirectory()) {
upload(file_ingetPath(), newName, ftpClientpwd());
} else {
uploadFile(file_ingetPath(), newName);
}
if (is != null) {
isclose();
}
if (os != null) {
osclose();
}
return true;
} catch (Exception e) {
eprintStackTrace();
Systemerrprintln("Exception e in Ftp upload(): " + etoString());
return false;
} finally {
try {
if (is != null) {
isclose();
}
if (os != null) {
osclose();
}
} catch (IOException e) {
eprintStackTrace();
}
}
}
/
真正用于上传的方法

@param fileName
@param newName
@param path
@throws Exception
/
private void upload(String fileName, String newName, String path)
throws Exception {
String savefilename = new String(fileNamegetBytes("ISO-8859-1"), "GBK");
File file_in = new File(savefilename);// 打开本地待长传的文件
if (!file_inexists()) {
throw new Exception("此文件或文件夹[" + file_ingetName() + "]有误或不存在!");
}
if (file_inisDirectory()) {
if (!isDirExist(newName)) {
createDir(newName);
}
ftpClientcd(newName);
File sourceFile[] = file_inlistFiles();
for (int i = 0; i < sourceFilelength; i++) {
if (!sourceFile[i]exists()) {
continue;
}
if (sourceFile[i]isDirectory()) {
thisupload(sourceFile[i]getPath(), sourceFile[i]
getName(), path + "/" + newName);
} else {
thisuploadFile(sourceFile[i]getPath(), sourceFile[i]
getName());
}
}
} else {
uploadFile(file_ingetPath(), newName);
}
ftpClientcd(path);
}
/
upload 上传文件

@param filename
要上传的文件名
@param newname
上传后的新文件名
@return -1 文件不存在 >=0 成功上传,返回文件的大小
@throws Exception
/
public long uploadFile(String filename, String newname) throws Exception {
long result = 0;
TelnetOutputStream os = null;
FileInputStream is = null;
try {
javaioFile file_in = new javaioFile(filename);
if (!file_inexists())
return -1;
os = ftpClientput(newname);
result = file_inlength();
is = new FileInputStream(file_in);
byte[] bytes = new byte[1024];
int c;
while ((c = isread(bytes)) != -1) {
oswrite(bytes, 0, c);
}
} finally {
if (is != null) {
isclose();
}
if (os != null) {
osclose();
}
}
return result;
}
/
从ftp下载文件到本地

@param filename
服务器上的文件名
@param newfilename
本地生成的文件名
@return
@throws Exception
/
public long downloadFile(String filename, String newfilename) {
long result = 0;
TelnetInputStream is = null;
FileOutputStream os = null;
try {
is = ftpClientget(filename);
javaioFile outfile = new javaioFile(newfilename);
os = new FileOutputStream(outfile);
byte[] bytes = new byte[1024];
int c;
while ((c = isread(bytes)) != -1) {
oswrite(bytes, 0, c);
result = result + c;
}
} catch (IOException e) {
eprintStackTrace();
} finally {
try {
if (is != null) {
isclose();
}
if (os != null) {
osclose();
}
} catch (IOException e) {
eprintStackTrace();
}
}
return result;
}
/
取得相对于当前连接目录的某个目录下所有文件列表

@param path
@return
/
public List getFileList(String path) {
List list = new ArrayList();
DataInputStream dis;
try {
dis = new DataInputStream(ftpClientnameList(thispath + path));
String filename = "";
while ((filename = disreadLine()) != null) {
listadd(filename);
}
} catch (IOException e) {
eprintStackTrace();
}
return list;
}
public static void main(String[] args) {
FtpUtil ftp = new FtpUtil("1921681111", "111", "1111");
ftpconnectServer();
boolean result = ftpupload("C:/Documents and Settings/ipanel/桌面/java/Hibernate_HQLdocx", "amuse/audioTest/music/Hibernate_HQLdocx");
Systemoutprintln(result "上传成功!" : "上传失败!");
ftpcloseServer();
/
FTP远程命令列表 USER PORT RETR ALLO DELE SITE XMKD CDUP FEAT PASS PASV STOR
REST CWD STAT RMD XCUP OPTS ACCT TYPE APPE RNFR XCWD HELP XRMD STOU
AUTH REIN STRU SMNT RNTO LIST NOOP PWD SIZE PBSZ QUIT MODE SYST ABOR
NLST MKD XPWD MDTM PROT
在服务器上执行命令,如果用sendServer来执行远程命令(不能执行本地FTP命令)的话,所有FTP命令都要加上\r\n
ftpclientsendServer("XMKD /test/bb\r\n"); //执行服务器上的FTP命令
ftpclientreadServerResponse一定要在sendServer后调用
nameList("/test")获取指目录下的文件列表 XMKD建立目录,当目录存在的情况下再次创建目录时报错 XRMD删除目录
DELE删除文件
/
}
}


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zz/10301010.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-07
下一篇 2023-05-07

发表评论

登录后才能评论

评论列表(0条)

保存