dimanche 25 octobre 2015

java.net.SocketException: Unexpected end of file from server Http Server and Client

I am having problems with my Java HTTP Server and Client.

I want, that the Client connects to the HTTP Server, use Basic Auth and download a file.

My Client Code:

HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestProperty("Authorization", "Basic " + Base64.encodeBase64(auth.getBytes()));
conn.setRequestMethod("GET");
InputStream in = new BufferedInputStream(conn.getInputStream());
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int n = 0;
while (-1 != (n = in.read(buf))) {
    out.write(buf, 0, n);
}
out.close();
in.close();
byte[] response = out.toByteArray();

FileOutputStream fos = new FileOutputStream(f);
fos.write(response);
fos.close();

My Server Code:

        http.getResponseHeaders().add("Content-Type", "application/octet-stream");
        byte[] bytearray = new byte[(int) local.length()];
        FileInputStream fis = new FileInputStream(local);
        BufferedInputStream bis = new BufferedInputStream(fis);
        bis.read(bytearray, 0, bytearray.length);
        http.sendResponseHeaders(200, local.length());
        OutputStream os = http.getResponseBody();
        os.write(bytearray, 0, bytearray.length);
        os.close();
        bis.close();

If I try it via my browser it works...




Aucun commentaire:

Enregistrer un commentaire