| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 909 人关注过本帖
标题:在FTP客户端出现的问题,各位帮忙看看嘛
只看楼主 加入收藏
Xuan
Rank: 1
等 级:新手上路
帖 子:79
专家分:0
注 册:2007-4-1
收藏
 问题点数:0 回复次数:3 
在FTP客户端出现的问题,各位帮忙看看嘛

import java.io.*;
import java.net.*;
import java.util.*;

public class FClient {
private static FClient f;

private Socket Sock;

private String strip;

private int Port;

private PrintStream out;

private BufferedReader in;

private PrintWriter dataout;

public FClient(String str, int port) throws IOException {
strip = str;
Port = port;
Sock = new Socket(strip, Port);
}

public static void main(String args[]) throws IOException {

String strSendCmd;
String strRec;

try {
String strip = "192.168.0.33";
FClient ftpClient = new FClient(strip,21);
f = ftpClient;
//初始化 控制通道
ftpClient.InitSockStream(ftpClient.Sock);
//获得控制通道的信息
System.out.println(ftpClient.in.readLine());
//登陆到ftp
ftpClient.BuildConnection(ftpClient);
//获取服务器端的文件目录
ArrayList fileList = ftpClient.GetFileList(ftpClient);
//处理用户的指令
ftpClient.ftpCmd();

} catch (IOException E) {
System.out.println(E.toString());
}
}

// 初始化控制通道
public void InitSockStream(Socket Sock) throws IOException {
out = new PrintStream(Sock.getOutputStream());
in = new BufferedReader(new InputStreamReader(Sock.getInputStream()));
}

//登陆到ftp
public void BuildConnection(FClient ftpClient) throws IOException {
String strSendCmd;
//输入用户名
strSendCmd = "USER zealear";
ftpClient.SendCommand(strSendCmd);
//输入密码
strSendCmd = "PASS sysu";
ftpClient.SendCommand(strSendCmd);
//输入格式控制模式
//strSendCmd = "TYPE A";
//ftpClient.SendCommand(strSendCmd);
}

//获得服务器上的目录或者文件列表
public ArrayList GetFileList(FClient ftpClient) throws IOException {
//设置模式PASV or PORT
ArrayList FileList = new ArrayList();
String strSendCmd = "PASV";
String strRec = ftpClient.SendCommand(strSendCmd);
Socket Client = ftpClient.CreateDataSocket(ftpClient.GetPort(strRec));//////////////////////////
strSendCmd = "NLST";
ftpClient.SendCommand(strSendCmd);
PrintStream outData;
BufferedReader inData;
//获得数据通道传出的数据
outData = new PrintStream(Client.getOutputStream());
inData = new BufferedReader(new InputStreamReader(Client
.getInputStream()));
String s;
while ((s = inData.readLine()) != null) {
System.out.println(s);
FileList.add(s);
}
ftpClient.SockClose(Client);//关闭数据通道
System.out.println(ftpClient.in.readLine()); //获得控制通道的信息
return FileList;
}

//在PORT或者PASV模式的返回值中获取端口
public String[] GetPort(String ss) {
int iPort = ss.indexOf("(");
int ePort = ss.lastIndexOf(")");
String tt = ss.substring(iPort + 1, ePort);////////////////////////////////////////////////////
String[] kk = null;
kk = tt.split(",");
return kk;
}

//创建数据连接socket
public Socket CreateDataSocket(String[] strArgu) throws IOException {

int Port = Integer.parseInt(strArgu[4]) * 256
+ Integer.parseInt(strArgu[5]);
String StrIP = strArgu[0] + "." + strArgu[1] + "." + strArgu[2] + "."
+ strArgu[3];
Socket Client = new Socket(StrIP, Port);
return Client;

}

//处理用户输入
public String userIn() throws IOException {
String strCmd;
InputStreamReader ir;
BufferedReader in;
ir = new InputStreamReader(System.in);
in = new BufferedReader(ir);
System.out.println("Enter:");
strCmd = in.readLine();
return strCmd;
}

//向控制通道发送命令
public String SendCommand(String strcmd) throws IOException {
out.println(strcmd);//发送控制命令
out.flush();
String strRec = in.readLine();
System.out.println(strRec);//输出控制器返回的消息
return strRec;
}

//用户的输入指令,包括:下载、上传文件
public void ftpCmd() throws IOException {
while (true) {
String userIn = f.userIn();
String[] ss = userIn.split(" ");
if (userIn.equalsIgnoreCase("close")) {
//退出ftp
String strRec = f.SendCommand("QUIT");
f.SockClose(f.Sock);//关闭控制通道
break;
} else if (ss[0].equalsIgnoreCase("get") && ss.length == 2) {
//下载文件到本地

} else if (ss[0].equalsIgnoreCase("put") && ss.length == 2) {
// 上传文件到服务器
f.up(f);
} else {
System.out.println("请输入合法的字符");
}
}
}

// 下载文件
public void down(FClient f) throws IOException{
String strRec = f.SendCommand("PASV");
Socket Client = f.CreateDataSocket(f.GetPort(strRec));
//.....
}

//上传文件
public void up(FClient f) throws IOException{
f.SendCommand("TYPE A");
String strRec = f.SendCommand("PASV");
Socket Client = f.CreateDataSocket(f.GetPort(strRec));


f.SendCommand("STOR haha.txt");
RandomAccessFile infile=new RandomAccessFile("D:/haha.txt","r");
PrintWriter dataout = new PrintWriter(Client.getOutputStream(),true);
String ss=infile.readLine();
while(ss!=null){
dataout.println(ss);
dataout.flush();
System.out.println(ss);
ss=infile.readLine();
}

}


//关闭socket
public void SockClose(Socket sock) throws IOException {
if (sock != null) {
sock.close();
sock = null;
}
}
}

上面的IP地址、用户名和密码、以及要上传的文件名和它的路径。红色的字体可以改成你想要的
我在测试的时候,为什么那个haha.txt文件很小(〈40K)左右的时候就无法传出去,只有文件很大的时候才能传,而且传出去的文件也是不完整的。。。。如果有中文字体还出现乱码。。。大家一起想想办法吧。。。

搜索更多相关主题的帖子: 客户端 FTP 
2007-04-03 13:11
千里冰封
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:灌水之王
等 级:版主
威 望:155
帖 子:28477
专家分:59
注 册:2006-2-26
收藏
得分:0 
你用readline很不好,因为传文本的时候,还可能成功,传别的,你根本读不出line来,即使读出,编码也变了

可惜不是你,陪我到最后
2007-04-03 13:33
Xuan
Rank: 1
等 级:新手上路
帖 子:79
专家分:0
注 册:2007-4-1
收藏
得分:0 
是啊。。
还有别的什么更好的办法实现吗?

首页:WWW. 蘑菇手机娱乐论坛,欢迎你的到来
2007-04-03 16:06
千里冰封
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:灌水之王
等 级:版主
威 望:155
帖 子:28477
专家分:59
注 册:2006-2-26
收藏
得分:0 
很多开源包,你找找看吧

可惜不是你,陪我到最后
2007-04-03 16:06
快速回复:在FTP客户端出现的问题,各位帮忙看看嘛
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015400 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved