繁体   English   中英

通过套接字传输文件-空文件

[英]File transfer through socket - empty file

我将文件从服务器传输到客户端时遇到问题。 传输完成后,将创建文件,但文件为空。 请注意,当我将其从客户端发送到服务器时,它可以工作。 有时,在长fileLength = dis.readLong()和字符串fileName = dis.readUTF()时,也会出现EOF异常。

客户:

private void sendFile(String path) throws IOException {
    BufferedOutputStream bos = new BufferedOutputStream(socket.getOutputStream());
    DataOutputStream dos = new DataOutputStream(bos);

    File file = new File(path);

    long length = file.length();
    dos.writeLong(length);

    String name = file.getName();
    dos.writeUTF(name);

    FileInputStream fis = new FileInputStream(file);
    BufferedInputStream bis = new BufferedInputStream(fis);

    int theByte = 0;
    while((theByte = bis.read()) != -1) 
        bos.write(theByte);

    dos.close();
    bis.close();   

    displayMessage(MESSAGE_SENT);
}

private void getFile() throws IOException {     
    BufferedInputStream bis = new BufferedInputStream(socket.getInputStream());
    DataInputStream dis = new DataInputStream(bis);

        long fileLength = dis.readLong();
    String fileName = dis.readUTF();

    File file = new File(System.getProperty("user.dir") + "/" + fileName);

    FileOutputStream fos = new FileOutputStream(file);
    BufferedOutputStream bos = new BufferedOutputStream(fos);

    int theByte = 0;
    while((theByte = bis.read()) != -1) 
        bos.write(theByte);

    bos.close();
    dis.close();
    displayMessage(MESSAGE_DOWNLOADED);
}

服务器:

private void sendFile(String path) throws IOException {     
    BufferedOutputStream bos = new BufferedOutputStream(socket.getOutputStream());
    DataOutputStream dos = new DataOutputStream(bos);

    File file = new File(path);

    long length = file.length();
    dos.writeLong(length);

    String name = file.getName();
    dos.writeUTF(name);

    FileInputStream fis = new FileInputStream(file);
    BufferedInputStream bis = new BufferedInputStream(fis);

    int theByte = 0;
    while((theByte = bis.read()) != -1) 
        bos.write(theByte);

    dos.close();
    bis.close();

    displayMessage(MESSAGE_SENT);
}

private void getFile() throws IOException {     
    BufferedInputStream bis = new BufferedInputStream(socket.getInputStream());
    DataInputStream dis = new DataInputStream(bis);

    long fileLength = dis.readLong();
    String fileName = dis.readUTF();

    File file = new File(System.getProperty("user.dir") + "/" + fileName);

    FileOutputStream fos = new FileOutputStream(file);
    BufferedOutputStream bos = new BufferedOutputStream(fos);

    for(int j = 0; j < fileLength; j++) 
        bos.write(bis.read());

    bos.close();
    dis.close();
    displayMessage(MESSAGE_DOWNLOADED);
}

我认为问题出在不刷新输出流。 像这样:

 int theByte = 0;
 while((theByte = bis.read()) != -1) 
       bos.write(theByte);

    dos.flush();

    dos.close();
    bis.close();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM