2013-10-02 129 views
1

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

回答

0

我认为问题出在不冲水的输出流。像这样:

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

    dos.flush(); 

    dos.close(); 
    bis.close(); 
+0

does not dos.close()call flush()? – keysersoze

+0

我的不好。 DataOutputStream是FilterOutputStream的一个子类,API也会说关闭刷新。对于那个很抱歉。 http://docs.oracle.com/javase/7/docs/api/java/io/FilterOutputStream.html#close() –

+0

但是,您可以在客户端sendFile方法中显式清除它。 –