运行的程序为什么是之前的?加了输出为什么不能继续?

lsjlovebm 2009-11-23 05:16:09
package jftp;

public class FtpConnection extends Thread //继承线程
{
static public String root = null; /** 主目录 */
private String currentDir = "/"; // 当前目录
private Socket socket; //客户套接字
private BufferedReader reader = null; //读入的字符缓冲区
private BufferedWriter writer = null; //写出的字符缓冲区
private String clientIP = null; //客户端IP地址
private Socket tempSocket = null; //tempSocket用于传送文件
private ServerSocket pasvSocket = null; //用于被动模式
private String host = null;
private int port = (-1);

public FtpConnection(Socket socket)
{
this.socket = socket;
this.clientIP = socket.getInetAddress().getHostAddress();//获取客户端IP地址
}

public void run()
{
String command;
try
{
System.out.println(clientIP + " 已连接 ");
socket.setSoTimeout(60000); //ftp超时设定
reader = new BufferedReader(new InputStreamReader(socket.getInputStream()));//获取输入输出流
writer = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
response("220-欢迎消息......"); //*****自定义

for(;;) //循环检测命令
{

command = reader.readLine(); //读入内容
System.out.println("调试"+command);
if(command == null)
break;
System.out.println("命令来自 " + clientIP + " 命令为: " + command);
parseCommand(command); //*****自定义
if(command.equals("QUIT")) //收到QUIT命令
break;
}
}
catch(Exception e) { e.printStackTrace(); }
finally
{ try
{
if(reader!=null) reader.close(); //关闭输入流
}
catch(Exception e) {}
try
{
if(writer!=null) writer.close(); //关闭输出流
}
catch(Exception e) {}
try
{
if(this.pasvSocket!=null) pasvSocket.close(); //关闭被动模式
}
catch(Exception e) {}
try
{
if(this.tempSocket!=null) tempSocket.close(); //关闭空套接字
}
catch(Exception e) {}
try
{
if(this.socket!=null) socket.close(); //关闭当前套接字
}
catch(Exception e) {}
}
System.out.println(clientIP + " 断开连接......");
}
private void response(String s) throws Exception //自定义:向客户发送消息
{
// System.out.println(" [RESPONSE] "+s);
writer.write(s);
writer.newLine();
writer.flush(); // 注意要flush否则响应仍在缓冲区
}
private static String pad(int length) //自定义:生成一个空字符串
{
StringBuffer buf = new StringBuffer();
for (int i = 0; i < length; i++)
buf.append((char)' ');
return buf.toString();//转换为String
}
private String getParam(String cmd, String start) //自定义:获取参数
{
String s = cmd.substring(start.length(), cmd.length());
return s.trim();
}
private String translatePath(String path) //自定义:获取路径
{
if(path==null) return root;
if(path.equals("")) return root;//为空,返回主目录
path = path.replace('/', '\\');//替换
return root + path;
}

// 获取文件长度,注意是一个字符串,并定义为标准的12个字符
private String getFileLength(long length)
{
String s = Long.toString(length);
int spaces = 12 - s.length();
for(int i=0; i<spaces; i++)
s = " " + s;
return s;
}

//接下来便是处理用户命令,这个方法有点长,需要重构一下,我只是把LIST命令单独挪了出来:

private void parseCommand(String s) throws Exception
{
if(s==null || s.equals(""))
return;
if(s.startsWith("USER ")) {
response("331 请输入密码");
}
else if(s.startsWith("PASS ")) {
response("230 欢迎您的到来!");
}
else if(s.equals("QUIT")) {
response("221 欢迎再来!");
}
else if(s.equals("TYPE A")) {
response("200 TYPE set to A.");
}
else if(s.equals("TYPE I")) {
response("200 TYPE set to I.");
}
else if(s.equals("NOOP")) {
response("200 NOOP OK.");
}
else if(s.startsWith("CWD")) { // 设置当前目录,注意没有检查目录是否有效
this.currentDir = getParam(s, "CWD ");//取CWD后的字符,即当前目录
response("250 CWD 命令执行成功.");
}
else if(s.startsWith("CDUP")) {
int n = this.currentDir.lastIndexOf("/");
this.currentDir = this.currentDir.substring(0,n);
response("250 CWD command succesful");
}
else if(s.equals("PWD")) { // 打印当前目录
response("257 \"" + this.currentDir + "\" 是当前目录.");
}
/*??*/ else if(s.startsWith("PORT ")) {



只能显示部分内容,长度不够,在上面加了红色的内容为什么就不能往下运行了?很奇怪!!而且再做修改 再运行也是之前的程序 这到底是为什么呀。。。
...全文
69 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
lsjlovebm 2009-11-23
  • 打赏
  • 举报
回复
好像是command = reader.readLine(); 没有读出东西 一直在读 所以下面那行显示不出来
tyj2788540 2009-11-23
  • 打赏
  • 举报
回复
楼主说的:
而且再做修改 再运行也是之前的程序 这到底是为什么呀。。。
是不是工具不编译了!重新编译一下!
tyj2788540 2009-11-23
  • 打赏
  • 举报
回复
改后的就不是这个问题了。
bayougeng 2009-11-23
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 tyj2788540 的回复:]
command 没有初始化,比如:String commond = "" ;
[/Quote]

好像是。
楼主,你到底想问什么呢?把现象和问题描述清楚好不?
tyj2788540 2009-11-23
  • 打赏
  • 举报
回复
command 没有初始化,比如:String commond = "" ;

51,408

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧