Android Socket下载多个文件?

时间:2012-05-14 10:18:46

标签: android sockets http-protocols

我想从服务器下载多张图片。我打开套接字并下载第一个图像,当我想在此套接字中下载第二个图像时,下载不继续,套接字关闭。我的代码如下......

公共类ClientThread实现Runnable {

    public void run() {
        try {
            Log.d("ClientActivity", "C: Connecting...");
            SocketAddress sockaddr = new InetSocketAddress(serverIpAddress, SERVERPORT);
            socket = new Socket();
            socket.setKeepAlive(true);
            socket.connect(sockaddr, 5000);
            if (socket.isConnected()) {
                final DataInputStream input = new DataInputStream(socket.getInputStream());
                final PrintWriter output = new PrintWriter(new OutputStreamWriter(socket.getOutputStream()));

                output.println("GET /way/images/profile/1231 HTTP/1.1");
                output.println("Host: 192.168.1.2");
                output.println("User-Agent: Java");
                output.println("Accept: */*");
                output.println("Connection: Keep-Alive");
                output.println("");
                output.flush();

                String line;
                File file = new File("/sdcard/aaa.png");
                if (!file.exists()) {
                    file.createNewFile();
                }
                FileOutputStream fileOut = new FileOutputStream(file);
                System.out.println("Getting first file");
                while ( (line = input.readLine()) != null ) {
                    System.out.println(line);
                    fileOut.write(line.getBytes());
                }
                System.out.println("First file finished");
                fileOut.flush();
                fileOut.close();

                output.println("GET /way/images/profile/1231 HTTP/1.1");
                output.println("Host: 192.168.1.2");
                output.println("User-Agent: Java");
                output.println("Accept: */*");
                output.println("Connection: Keep-Alive");
                output.println("");
                output.flush();

                file = new File("/sdcard/aaa1.png");
                if (!file.exists()) {
                    file.createNewFile();
                }
                fileOut = new FileOutputStream(file);
                System.out.println("Getting second file");
                while ( (line = input.readLine()) != null ) {
                    System.out.println(line);
                    fileOut.write(line.getBytes());
                }
                System.out.println("Second file finished");

                fileOut.flush();
                fileOut.close();
                input.close();
                output.close();
                socket.close();
            }
            Log.d("ClientActivity", "C: Closed.");
        } catch (Exception e) {
            Log.e("ClientActivity", "C: Error", e);
            connected = false;
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您似乎使用HTTP下载图像文件,在这种情况下,使用HttpURLConnection会更简单,更好。

使用HttpURLConnection下载图片的代码示例可以在以下博客文章中找到:Downloading image in Android

您还可以阅读Android开发者博客中的Android's HTTP Clients