套接字文件传输失败

时间:2013-04-03 04:44:37

标签: java sockets

我有一个使用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();
    }
}

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

1 个答案:

答案 0 :(得分:0)

您关闭了流,然后继续使用套接字。关闭套接字的输入流或输出流将关闭另一个流和套接字。