java - 通过socket传输文件-空文件

标签 java sockets file-transfer

我在将文件从服务器传输到客户端时遇到问题。传输完成后,文件被创建,但它是空的。请注意,当我将其从客户端发送到服务器时,它可以工作。有时我也会在 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);
}

最佳答案

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

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

    dos.flush();

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

关于java - 通过socket传输文件-空文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19139366/

相关文章:

java - 树小部件仅显示列表中的一个值 (GWT)

java - 嵌套类的接口(interface)静态方法

javascript - 使用文件传输的 Cordova 多图像上传

java - 使用 Java JSch 进行 SFTP 文件传输

ios - WCSession 文件传输问题

java - 在 Activity 中启动前台服务

java - 在 MS SQL Server 2012 中通过 Java 动态创建存储过程

sockets - UDP 套接字上的套接字错误 10052

django - 无法使用Cpanel/WHM连接到Centos7中的WSGI守护程序进程mod_wsgi

java - 通过java中的套接字发送对象