
测试服务器防火墙阻止了发起的数据端口的连接因而FTPClientlistFiles(remote)或者FTPClientretrieveFile(remote)方法时获取不了数据,就停止在那里什么反应都没有,出现假死状态。
在调用这两个方法之前,调用FTPCliententerLocalPassiveMode,这个方法的意思就是每次数据连接之前,ftp client告诉ftp server。
数据连接的端口号已经告诉你了,你只需被动接受数据连接的请求就行。
java测试连接ftp是否连通可以使用判断是否有异常来决定,实例如下:
/connectServer
连接ftp服务器
@throws javaioIOException
@param path 文件夹,空代表根目录
@param password 密码
@param user 登陆用户
@param server 服务器地址
/
public void connectServer(String server, String user, String password, String path)
throws IOException
{
// server:FTP服务器的IP地址;user:登录FTP服务器的用户名
// password:登录FTP服务器的用户名的口令;path:FTP服务器上的路径
ftpClient = new FtpClient();
ftpClientopenServer(server);
ftpClientlogin(user, password);
//path是ftp服务下主目录的子目录
if (pathlength() != 0) ftpClientcd(path);
//用2进制上传、下载
ftpClientbinary();
}
/
upload
上传文件
@throws javalangException
@return -1 文件不存在
-2 文件内容为空
>0 成功上传,返回文件的大小
@param newname 上传后的新文件名
@param filename 上传的文件
/
public long upload(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;
if (file_inlength()==0) return -2;
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;
}
/
upload
@throws javalangException
@return
@param filename
/
public long upload(String filename)
throws Exception
{
String newname = "";
if (filenameindexOf("/")>-1)
{
newname = filenamesubstring(filenamelastIndexOf("/")+1);
}else
{
newname = filename;
}
return upload(filename,newname);
}
/
download
从ftp下载文件到本地
@throws javalangException
@return
@param newfilename 本地生成的文件名
@param filename 服务器上的文件名
/
public long download(String filename,String newfilename)
throws Exception
{
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 {
if (is != null) {
isclose();
}
if (os != null) {
osclose();
}
}
return result;
}
/
取得某个目录下的所有文件列表
/
public List getFileList(String path)
{
List list = new ArrayList();
try
{
DataInputStream dis = new DataInputStream(ftpClientnameList(path));
String filename = "";
while((filename=disreadLine())!=null)
{
listadd(filename);
}
} catch (Exception e)
{
eprintStackTrace();
}
return list;
}
/
closeServer
断开与ftp服务器的链接
@throws javaioIOException
/
public void closeServer()
throws IOException
{
try
{
if (ftpClient != null)
{
ftpClientcloseServer();
}
} catch (IOException e) {
eprintStackTrace();
}
}
public static void main(String [] args) throws Exception
{
FtpUtil ftp = new FtpUtil();
try {
//连接ftp服务器
ftpconnectServer("10163715", "cxl", "1", "info2");
/ 上传文件到 info2 文件夹下 /
Systemoutprintln("filesize:"+ftpupload("f:/download/Installexe")+"字节");
/ 取得info2文件夹下的所有文件列表,并下载到 E盘下 /
List list = ftpgetFileList("");
for (int i=0;i<listsize();i++)
{
String filename = (String)listget(i);
Systemoutprintln(filename);
ftpdownload(filename,"E:/"+filename);
}
} catch (Exception e) {
///
}finally
{
ftpcloseServer();
}
}
}
boolean success = false;
FTPClient ftp = new FTPClient();
try {
int reply;
ftpconnect(url, port);
//如果采用默认端口,可以使用ftpconnect(url)的方式直接连接FTP服务器
ftplogin(username, password);//登录
reply = ftpgetReplyCode();
if (!FTPReplyisPositiveCompletion(reply)) {
ftpdisconnect();
return success;
}
ftpchangeWorkingDirectory(remotePath);//转移到FTP服务器目录
FTPFile[] fs = ftplistFiles();
以上就是关于FTP别人传数据给我 系统提取不到数据全部的内容,包括:FTP别人传数据给我 系统提取不到数据、java如何测试连接ftp是否通、java遍历ftp文件夹时,在FTPFile ff[] = ftpClient.listFiles()处一直提示空指针异常错误,是怎么回事。等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)