【问题标题】:File transfer through socket - empty file通过套接字传输文件 - 空文件
【发布时间】:2013-10-02 23:17:41
【问题描述】:

我在将文件从服务器传输到客户端时遇到问题。传输完成后,文件已创建,但为空。请注意,当我将它从客户端发送到服务器时,它可以工作。有时我也会在 long fileLength = dis.readLong() 和 String 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);
}

【问题讨论】:

    标签: java sockets file-transfer


    【解决方案1】:

    我认为问题在于没有刷新您的输出流。像这样:

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

    【讨论】:

    • dos.close() 不调用 flush()?
    • 我的错。 DataOutputStream 是 FilterOutputStream 的子类,API 也确实说关闭刷新。对于那个很抱歉。 docs.oracle.com/javase/7/docs/api/java/io/…
    • 您确实在客户端 sendFile 方法中显式刷新它。
    • 我正在试验;p 我没有在那里调用 dos.close() - 无论如何我测试了它但它不起作用:(
    • 当您通过调试器单步执行客户端代码时,您能否确认数据已到达并且写入文件本身没有问题?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-19
    • 1970-01-01
    • 1970-01-01
    • 2020-12-02
    相关资源
    最近更新 更多