线程java服务器只发送一个文件

时间:2012-10-04 18:58:13

标签: java multithreading sockets

我有一个将文件发送到Android客户端的java服务器。由于某种原因,服务器似乎只在一次通过后关闭套接字,我希望它发送所有文件然后关闭套接字,这是我的服务器代码。提前致谢

public void run() {
   DataOutputStream dos = null;
    try {
        String path = "/home/test";
        int count = 0;
        dos = new DataOutputStream(
                new BufferedOutputStream(socket.getOutputStream()));
        DataInputStream dis = new DataInputStream(new BufferedInputStream(
                socket.getInputStream()));

        File[] files = new File(path).listFiles();
        for (File file : files) {
            String filename = file.getName();
            String extension = filename.substring(
                    filename.lastIndexOf(".") + 1, filename.length());
            if ("png".equals(extension)) {
                count += 1;
            }

        }
        System.out.println("Sending " + count + " files");
        dos.writeInt(count);
        dos.flush();
        byte[] temp = new byte[1024];
        int n = 0;
        for (File file : files) {
            String filename = file.getName();
            String extension = filename.substring(
                    filename.lastIndexOf(".") + 1, filename.length());
            if ("png".equals(extension)) {
                FileInputStream fis = new FileInputStream(file);
                BufferedInputStream bis = new BufferedInputStream(fis);
                byte fileContent[] = new byte[(int) file.length()];
                bis.read(fileContent);
                int dataLength = fileContent.length;
                dos.writeInt(dataLength);
                dos.flush();
                int open = dis.readInt();
                System.out.println("still open if open = 5: open = "+open);
                System.out.println(filename + " is " + dataLength
                        + " bytes long");
                while ((dataLength > 0)
                        && (n = bis.read(temp, 0,
                                (int) Math.min(temp.length, dataLength))) != -1) {
                    dos.write(temp, 0, n);
                    dos.flush();
                    dataLength -= n;

                }
                System.out.println("Sent file "+filename);
                fis.close();
                bis.close();

            }
        }

    } catch (IOException e) {
        e.printStackTrace();
    }
    finally
    {   

        try{
        if (dos != null) {dos.close();}
        } catch (Exception e) {

            e.printStackTrace();
        }
    }
}

}

1 个答案:

答案 0 :(得分:2)

您的Android应用程序是否有机会旋转?您的代码中存在逻辑错误。它将整个文件读入fileContent,然后进入while循环,等待文件完全读取。

bis.read(fileContent);
...
while ((dataLength > 0)
    && (n = bis.read(temp, 0, (int) Math.min(temp.length, dataLength))) != -1) {

您已经从bis读取了整个文件,因此当您阅读更多内容时,您将获得n == 0,因此它会永远循环播放。

如果您想进行缓冲,则应删除bis.read(fileContent);。如果您想在开始时阅读该文件,则需要删除while循环。

int dataLength = (int) file.length();
byte fileContent[] = new byte[dataLength];
bis.read(fileContent);
dos.writeInt(dataLength);
dos.write(fileContent);
dos.flush();
dataLength -= n;