在发布请求后将图像响应转换为文件

时间:2016-11-15 07:01:46

标签: java image rest highcharts export

目前尝试在服务器上呈现高级图表(JS),如此处所述http://www.highcharts.com/docs/export-module/render-charts-serverside

这是我的代码:

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();

}

从服务器我得到以下回复:

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]}}

但保存的图像只是黑色,我认为我的解码有问题。

真的很感谢你的帮助!

1 个答案:

答案 0 :(得分:0)

这有效

/*
 * 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();
    }
}