2013-04-03 125 views
0

我有一个使用Sender类的服务器和使用下面的Downloader类的客户端。当客户端连接到服务器,它下载database.db和default.docx完美的,但是当它开始读取.png文件,它抛出这个错误:套接字文件传输失败

java.io.IOException: Stream closed 
at java.io.BufferedInputStream.getBufIfOpen(BufferedInputStream.java:145) 
at java.io.BufferedInputStream.read(BufferedInputStream.java:308) 
at java.io.DataInputStream.readFully(DataInputStream.java:178) 
at java.io.DataInputStream.readLong(DataInputStream.java:399) 
at data.Downloader.<init>(Downloader.java:18) 
at data.Connection.<init>(Connection.java:18) 
at main(Client.java:17) 

这里是我唯一的两种方法:

下载:

public Downloader(Socket socket) { 
    try { 
     BufferedInputStream bis = new BufferedInputStream(socket.getInputStream()); 
     DataInputStream dis = new DataInputStream(bis); 
     int filesCount = dis.readInt(); 
     for (int i = 0; i < filesCount; i++) { 
      long size = dis.readLong(); 
      String fileName = dis.readUTF(); 
      System.out.println(fileName); 
      if (fileName.equals("database.db")) { 
       List<String> data = new ArrayList<String>(); 
       BufferedReader reader = new BufferedReader(new InputStreamReader(bis)); 
       String line; 
       while ((line = reader.readLine()) != null) { 
        if (line.trim().length() > 0) { 
         data.add(line); 
        } 
       } 
       reader.close(); 
       parse(data); 
      } else if (fileName.equals("default.docx")) { 
       ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
       for (int x = 0; x < size; x++) { 
        bos.write(bis.read()); 
       } 
       bos.close(); 
       document = bos.toByteArray(); 
      } else if (fileName.contains(".png")) { 
       ByteArrayOutputStream bos = new ByteArrayOutputStream(); 
       for (int x = 0; x < size; x++) { 
        bos.write(bis.read()); 
       } 
       bos.close(); 
       Signatures.signatures.add(bos.toByteArray()); 
      } 
     } 
     dis.close(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 

发件人:

public Sender(Socket socket) { 
    List<File> files = new ArrayList<File>(); 
    files.add(new File(Directory.getDataPath("default.docx"))); 
    files.add(new File(Directory.getDataPath("database.db"))); 
    for (String signature : Directory.getSignaturePaths()) { 
     files.add(new File(signature)); 
    } 
    try { 
     BufferedOutputStream bos = new BufferedOutputStream(socket.getOutputStream()); 
     DataOutputStream dos = new DataOutputStream(bos); 
     dos.writeInt(files.size()); 
     for (File file : files) { 
      System.out.println(file.getName() + file.length()); 
      dos.writeLong(file.length()); 
      dos.writeUTF(file.getName()); 
      FileInputStream fis = new FileInputStream(file); 
      BufferedInputStream bis = new BufferedInputStream(fis); 
      int theByte = 0; 
      while ((theByte = bis.read()) != -1) { 
       bos.write(theByte); 
      } 
      bis.close(); 
     } 
     dos.close(); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
} 

另外,我查了GE tSignaturePaths()方法,它返回正确的路径并且.png文件在那里。

+0

你确定文件列表中包含图片吗?第二个文件发送后,是否在stdout上打印了某些内容? – skirsch

回答

0

您关闭了流,然后保持使用套接字。关闭套接字的输入流或输出流会关闭另一个流和套接字。

+0

当我使用dos.close()?或者哪一个。我查看了其余的代码,并没有使用socket.close()。 – CBennett

+0

dos.close(),reader.close(),它们都在这个地方。 – EJP