简体   繁体   中英

Java http request on binary file - can't read correct content-length

I'm sending a http request to get binary files (here i'm trying an image)

public class Main {

    public static void main(String[] args) throws UnknownHostException,
            IOException {
        new Main(args);

    }

    public Main(String[] args) throws UnknownHostException, IOException {
        lance(args);
    }

    private void lance(String[] args) throws UnknownHostException, IOException {
        Lanceur lan = new Lanceur("www.cril.univ-artois.fr", "/IMG/arton451.jpg");
        lan.requete();
    }

}

public class Lanceur {

    Socket s;
    InputStream readStream;
    OutputStream writeStream;
    String host;
    String ressource;

    public Lanceur(String host, String ressource) throws UnknownHostException,
            IOException {
        s = new Socket(InetAddress.getByName(host), 80);
        readStream = s.getInputStream();
        writeStream = s.getOutputStream();
        this.host = host;
        this.ressource = ressource;
    }

    public void requete() throws IOException {
        // String[] length = null;
        writeStream.write(new String("GET " + ressource + " HTTP/1.1\r\n"
                + "Host: www.google.com\r\n" + "\r\n").getBytes());
        writeStream.flush();
        AnswerReader as = new AnswerReader(readStream);
        as.read();
        as.writeFile(this.ressource);
        s.close();
    }
}

public class AnswerReader {
    BufferedReader br;
    DataInputStream dis;
    String status;
    Map<String, String> attrs;
    byte[] content;

    public AnswerReader(InputStream is) {
        br = new BufferedReader(new InputStreamReader(is));
        dis = new DataInputStream(new BufferedInputStream(is));
    }

    public void read() throws NumberFormatException {
        readStatus();
        try {
            readAttrs();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        String contentL = attrs.get("Content-Length");

        readContent(Integer.valueOf(contentL));

    }

    public void readStatus() {
        try {
            status = br.readLine();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    public void readAttrs() throws IOException {
        attrs = new HashMap<String, String>();
        String line;

        for (line = br.readLine(); line.length() > 0; line = br.readLine()) {
            int index = line.indexOf(':');
            attrs.put(line.substring(0, index), line.substring(index + 2));
        }
    }

    private void readContent(int size) {

        this.content = new byte[size];
        byte[] buff = new byte[1024];
        int copied = 0;
        int read = 0;
        while (copied < size) {
            try {
                read = dis.read(buff);
                if (read == -1)
                    break;
            } catch (IOException e) {
                e.printStackTrace();
            }
            // byte[] byteArray = new String(buff).getBytes();
            System.arraycopy(buff, 0, content, copied, read);
            copied += read;
        }
        System.out.println(copied + "///" + size);
    }

    public void writeFile(String name) throws IOException {
        String tab[] = name.split("/");
        String filename = tab[tab.length - 1];
        FileOutputStream fos = new FileOutputStream("./" + filename);
        fos.write(content);
        fos.flush();
        fos.close();
    }
}

The problem comes from readContent(). The content-length is fine, but it doesn't read all the data. From this example it will reads that :

22325///38125

The BufferedReader is buffering some of the binary data. You'll have to find another way to read the header lines using the unbuffered input stream instead, eg DataInputStream.readLine() , deprecation or no.

But you should use HttpURLConnection . It's easier.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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