WordCloud with Java

How can I get a png image from WordCloud with QuickChart and java?

public void getCurl() {
StringBuilder sb = new StringBuilder();
sb.append("curl -X POST -H ‘Content-Type: application/json’ ").append("https://quickchart.io/wordcloud “);
sb.append(” -d @img/churlig.json -o “).append(“img/churchill.png”);
String Demo_Command = sb.toString();
ProcessBuilder Process_Builder = new ProcessBuilder(Demo_Command.split(” "));
Process_Builder.directory(new File(“img/”));
Process Demo_process = null;;
try {
Demo_process = Process_Builder.start();
Demo_process.waitFor();
InputStream Input_Stream = Demo_process.getInputStream();
int Exit_Code = Demo_process.exitValue();
System.out.println(Exit_Code);
Demo_process.destroy();

	} catch (IOException e) {
		e.printStackTrace();
	} catch (InterruptedException e) {
		e.printStackTrace();
	}
}

Just installed curl on windows.
@ian Could you please help me with this?

Tanks

Hi @apoloben, what happens when you run this code? What is the error output?

In general, it probably doesn’t make sense to shell out to CURL when Java has native HTTP request support. You could do something like this.

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import org.json.JSONObject;

public class WordCloudAPI {
    public static void main(String[] args) {
        try {
            URL url = new URL("https://quickchart.io/wordcloud");
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Content-Type", "application/json");
            connection.setDoOutput(true);

            // Wordcloud params
            JSONObject jsonParam = new JSONObject();
            jsonParam.put("format", "png");
            jsonParam.put("width", 1000);
            jsonParam.put("height", 1000);
            jsonParam.put("fontScale", 15);
            jsonParam.put("scale", "linear");
            jsonParam.put("text", "Your text goes here...");

            // Send the request
            try(OutputStream os = connection.getOutputStream()) {
                byte[] input = jsonParam.toString().getBytes("utf-8");
                os.write(input, 0, input.length);           
            }

            // Get the response
            try(BufferedInputStream bis = new BufferedInputStream(connection.getInputStream())) {
                FileOutputStream fos = new FileOutputStream("wordcloud.png");
                byte[] buffer = new byte[1024];
                int count=0;
                while((count = bis.read(buffer,0,1024)) != -1) {
                    fos.write(buffer, 0, count);
                }
                fos.close();
            }

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

Excellent!
Thank You @ian