简体   繁体   English

套接字文件传输失败

[英]Socket file transfer failing

I have a server which uses the Sender class and a client which uses the Downloader class below. 我有一个使用Sender类的服务器和一个使用下面的Downloader类的客户端。 When the client connects to the server, it downloads the database.db and the default.docx perfectly, but when It starts to read the .png files, It throws this error: 当客户端连接到服务器时,它会完美地下载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)

Here are my only two methods: 这是我仅有的两种方法:

Downloader: 下载器:

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();
    }
}

Sender: 发件人:

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();
    }
}

Also, I checked the getSignaturePaths() method and It returns the correct paths and the .png files are there. 另外,我检查了getSignaturePaths()方法,它返回正确的路径,.png文件在那里。

You closed the stream and then kept using the socket. 您关闭了流,然后继续使用套接字。 Closing either the input stream or the output stream of a socket closes the other stream and the socket. 关闭套接字的输入流或输出流会关闭另一个流和套接字。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM