user3740082 user3740082 - 17 days ago 5
reST (reStructuredText) Question

Converting image response, after post request, to file

At the moment a try to render highcharts charts (JS) on a server, like described here http://www.highcharts.com/docs/export-module/render-charts-serverside.

This is my code:

Helper.getRenderedImage("{xAxis: {categories: ['Jan1', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']},series: [{data: [29.9, 71.5, 106.4, 129.2, 144.0, 176.0, 135.6, 148.5, 216.4, 194.1, 95.6, 54.4]}]}");


/*
* returns rendered highcharts image
*/
public static void getRenderedImage(String json) {
HttpClient httpClient = HttpClientBuilder.create().build();

try {

HttpPost request = new HttpPost("http://export.highcharts.com");
ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
postParameters.add(new BasicNameValuePair("content", "options"));
postParameters.add(new BasicNameValuePair("options", json));
postParameters.add(new BasicNameValuePair("constr", "Chart"));
postParameters.add(new BasicNameValuePair("type", "image/png"));

request.setEntity(new UrlEncodedFormEntity(postParameters));

System.out.println(postParameters);
HttpResponse response = httpClient.execute(request);

System.out.println(response);
String result = getStringFromInputStream(response.getEntity().getContent());



// Converting a Base64 String into Image byte array
byte[] imageByteArray = result.getBytes();

// Write a image byte array into file system
FileOutputStream imageOutFile = new FileOutputStream("/Users/joern/bild.png");

imageOutFile.write(imageByteArray);
imageOutFile.close();

}catch (Exception e) {
e.printStackTrace();
}
}


// convert InputStream to String
private static String getStringFromInputStream(InputStream is) {

BufferedReader br = null;
StringBuilder sb = new StringBuilder();

String line;
try {

br = new BufferedReader(new InputStreamReader(is));
while ((line = br.readLine()) != null) {
sb.append(line);
}

} catch (IOException e) {
e.printStackTrace();
} finally {
if (br != null) {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}

return sb.toString();

}


From the server I get the following response:

HttpResponseProxy{HTTP/1.1 200 [Date: Sun, 13 Nov 2016 11:32:38 GMT, Content-Type: image/png;charset=utf-8, Content-Length: 19735, Connection: keep-alive, Set-Cookie: __cfduid=d4b9a89789dd7b781d6d3acc19ee923031479036757; expires=Mon, 13-Nov-17 11:32:37 GMT; path=/; domain=.highcharts.com; HttpOnly, Access-Control-Allow-Origin: *, Set-Cookie: JSESSIONID=0A990D121FD5C4C6E89D71D07BF41CE3;path=/;HttpOnly, Content-Disposition: attachment; filename=chart.png, Server: cloudflare-nginx, CF-RAY: 3011e77890796391-FRA] ResponseEntityProxy{[Content-Type: image/png;charset=utf-8,Content-Length: 19735,Chunked: false]}}


But the saved image is just black and I assume there is something wrong with my decoding.

Really appreciate your help!

Answer

This works

/*
 * returns rendered highcharts image
 */
public static void getRenderedImage(String json) {
    HttpClient httpClient = HttpClientBuilder.create().build(); 

    try {

        HttpPost request = new HttpPost("http://export.highcharts.com");
        ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
        postParameters.add(new BasicNameValuePair("content", "options"));
        postParameters.add(new BasicNameValuePair("options", json));
        postParameters.add(new BasicNameValuePair("constr", "Chart"));
        postParameters.add(new BasicNameValuePair("type", "png"));

        request.setEntity(new UrlEncodedFormEntity(postParameters));

        HttpResponse response = httpClient.execute(request);


        // write the inputStream to a FileOutputStream
        OutputStream outputStream =
                    new FileOutputStream(new File("/Users/joern/bild11.png"));

        int read = 0;
        byte[] bytes = new byte[1024];

        while ((read = response.getEntity().getContent().read(bytes)) != -1) {
            outputStream.write(bytes, 0, read);
        }

        outputStream.close();

    }catch (Exception e) {
        e.printStackTrace();
    }
}
Comments