2 u012173090 u012173090 于 2013.09.18 18:15 提问

简陋的ftp下载代码,小弟求助!

服务器代码:
package day21.xl;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;

public class FtpServer {

public static void main(String[] args)throws IOException {
    FtpServer server=new FtpServer();
    server.listen(8080);
}
public void listen(int port)throws IOException{
    ServerSocket ss=new ServerSocket(port);
    //System.out.println("欢迎使用FTP Demo软件!");
    while(true){
        System.out.println("等待客户连接...");
        Socket socket=ss.accept();
        System.out.println("连接成功!");
        new ClientAgent(socket).start();
    }
}
class ClientAgent extends Thread{
    Socket socket;
    InputStream in;
    OutputStream out;
    public ClientAgent(Socket socket) throws IOException{
        this.socket=socket;
        in=socket.getInputStream();
        out=socket.getOutputStream();
    }
    @Override
    public void run() {
        BufferedReader in=new BufferedReader(new InputStreamReader(this.in));
        PrintWriter out=new PrintWriter(this.out, true);
        out.println("text,1");
        out.println("欢迎使用《Ftp》传输软件!");
        while(true){
            try {
                String cmd=in.readLine();
                if("?".trim().equals(cmd)){
                    out.println("text,1");
                    out.println("支持命令:ls,get,?,bye");
                }else if("ls".trim().equals(cmd)){
                    listDir(out);
                }else if(cmd.matches("^get\\s+.+")){
                    sendFile(cmd, out, this.out);
                }else{
                    out.println("text,1");
                    out.println("不知可否!");
                }
            } catch (IOException e) {
                e.printStackTrace();
            }

        }
    }
    private void sendFile(String cmd, PrintWriter out, OutputStream os) throws IOException {
        String name=cmd.split("\\s+")[1];
        File file=new File(name);
        if(! file.exists()){
            out.println("text,1");
            out.println("没有该文件!"+name);
            return;
        }
        out.println("file,"+file.length()+","+name);
        InputStream in=new BufferedInputStream(new FileInputStream(file));
        int b;
        while((b=in.read())!=-1){
            os.write(b);
        }
        os.flush();
        out.flush();
        in.close();
    }
    private void listDir(PrintWriter out) {
        File dir=new File(".");
        File[] files=dir.listFiles(new FileFilter(){
            @Override
            public boolean accept(File pathname) {
                return pathname.isFile();
            }
        });
        out.println("text,"+files.length+1);
        out.println("在目录"+dir+"中,有文件"+files.length);
        for(File file : files){
            out.println(file.getName());
        }
        out.flush();
    }
}

}

客户端代码:
package day21.xl;

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.Socket;

public class FtpClient {
Socket socket;
InputStream in;
OutputStream out;
public static void main(String[] args)throws IOException {
FtpClient server=new FtpClient();
server.open("localhost",8080);
}
public void open(String host,int port)throws IOException{
socket=new Socket(host,port);
in=socket.getInputStream();
out=socket.getOutputStream();
new RequestSender(out).start();
new ResponseReceiver(in).start();
}
class RequestSender extends Thread{
OutputStream out;
public RequestSender(OutputStream out){
this.out=out;
}
@Override
public void run() {
PrintWriter out=new PrintWriter(this.out, true);
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
String str;
try {
while((str=in.readLine())!=null){
out.println(str);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}

class ResponseReceiver extends Thread{
    InputStream in;
    public ResponseReceiver(InputStream in){
        this.in=in;
    }
    @Override
    public void run() {
        BufferedReader in=new BufferedReader(new InputStreamReader(this.in));
        String str;
        try {
            while((str=in.readLine())!=null){
                if(str.startsWith("text")){
                    String num=str.substring(str.indexOf(",")+1);
                    printText(in,Integer.parseInt(num));
                }else if(str.startsWith("file")){
                    saveFile(this.in, str);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private void saveFile(InputStream in, String head)throws IOException {
        String[] data=head.split(",");
        int length=Integer.parseInt(data[1]);
        String name=data[2];
        OutputStream out=new BufferedOutputStream(new FileOutputStream("ftp-"+name));
        for(int i=0;i<length;i++){
            int b=in.read();
            out.write(b);
        }
        out.flush();
        out.close();
        System.out.println(name+"下载成功!");
    }
    private void printText(BufferedReader in, int num) throws IOException {
        for(int i=0;i<num;i++){
            System.out.println(in.readLine());
        }
    }
}

}

当我运行get 下载功能时,测试结果为
第一次输入get text.txt结果显示:
get text.txt
file,19,text.txt
第二次输入get text.txt结果为:
get text.txt

A中接口bc中中试试吧file,19,text.txt

我想要的结果是,输入get text.txt命令之后再本地磁盘上会出现一个 ftp-text.txt这个文件。但是输入结果是file,19,text.txt;
我想应该是没有清空缓存的结果吧?但是我吧所有的流都.flush()了一遍。结果还是这样。请大神们指点!小弟已经研究一天了,找不出哪里错了。小弟在此感谢所有进来看的大神们。

1个回答

huangying123111
huangying123111   2013.09.19 12:59

把所有的导包删除,按下CTRL +SHIFT + O来导包

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!