title: Converting image response, after post request, to file
body: <p>At the moment a try to render highcharts charts (JS) on a server, like described here <a href="http://www.highcharts.com/docs/export-module/render-charts-serverside" rel="nofollow noreferrer">http://www.highcharts.com/docs/export-module/render-charts-serverside</a>.</p>
  
  <p>This is my code:</p>
  
  <pre><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&lt;NameValuePair&gt; postParameters = new ArrayList&lt;NameValuePair&gt;();
          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();
  
  }
  </code></pre>
  
  <p>From the server I get the following response:</p>
  
  <pre><code>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]}}
  </code></pre>
  
  <p>But the saved image is just black and I assume there is something wrong with my decoding.</p>
  
  <p>Really appreciate your help!</p>