打开从套接字读取的文件出错

时间:2014-08-07 16:11:28

标签: java sockets io

我有一对使用TCP协议的简单客户端/服务器FTP类。我能够传输小文本文件,但是当我尝试更大的文件如this时,文件显示已损坏(编辑器被锁定试图打开它,更少被卡住跳到最后。)我感觉我没有正确关闭它,或者正确地从插座中读取并保存它。以下是来自客户端和服务器的get()函数。希望有人能抓住我做错的事情。

服务器:

public void get(){

    // get file path from client
    try {
        this.filePath = controlIn.readLine();
    } catch (IOException e) {
        controlOut.println(ERROR);
        dataOut.println("Server Error: Unable to read file path");
        System.out.println("error reading filepath");
        return;
    }   

    // if client sends 0, abort GET (file exists in client directory)
    if(filePath == "-1"){
        return;
    }       

    // check if file exists
    File f = new File(filePath);
    if(!f.exists()){
        controlOut.println(ERROR);
        dataOut.println("Server Error: File does not exist: " + filePath);
        return;
    }

    // send file size as success msg
    int fileSize = (int)f.length();
    controlOut.println(fileSize);

    //read file to buffer, write buffer to socket
    byte [] buf  = new byte [fileSize];

    try {
        FileInputStream fis = new FileInputStream(f);
        BufferedInputStream bis = new BufferedInputStream(fis);
        bis.read(buf,0,buf.length);
        OutputStream os = dataSocket.getOutputStream();
        os.write(buf,0,fileSize);
        fis.close();
        bis.close();
        os.close();
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }   
}

客户端:

public void get(String filePath){

    // check if file exists. If not, send ERROR to server
    File f = new File(filePath);
    if(f.exists()){
        controlOut.println(ERROR);
        System.out.println("Client Error: File: " + filePath + " already exists");
        return;
    }

    // send file path to server
    controlOut.println(filePath);

    // read status from server. Status > 0 = file size
    int status = readStatus();  
    if(status == ERROR){
        System.out.println(readError());
        return;
    }

    int fileSize = status;
    int bytesRead;
    int totalBytesRead = 0;

    byte [] buf  = new byte [fileSize*2];
    try {
        InputStream is = dataSocket.getInputStream();
        FileOutputStream fos = new FileOutputStream(f);
        BufferedOutputStream bos = new BufferedOutputStream(fos);
        do{
             bytesRead = is.read(buf,0,fileSize);
        } while(bytesRead != -1);
        bos.write(buf, 0 , fileSize);
        is.close();
        bos.close();
        fos.close();        
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

还有一件事。 get方法返回后,数据套接字在服务器端关闭,而fileSize似乎在客户端正确。

1 个答案:

答案 0 :(得分:-1)

整数的文件大小

老实说:只需把它变成像

那样的循环
byte[] buffer = new byte[256];
int len;
while ((len = in.read(buffer)) != -1) {
   //do something with the buffer, eg:
   StringBuilder.append(new String(buffer, 0, len));