0

I am trying to download image from server using CURL. Below is the code I am using.

private void executeCMD(String[] cmd) {
        ProcessBuilder process = new ProcessBuilder(cmd);
        Process p;
        try
        {
            p = process.start();
            BufferedReader reader =  new BufferedReader(new InputStreamReader(p.getInputStream()));
            StringBuilder builder = new StringBuilder();
            String line = null;
            while ( (line = reader.readLine()) != null) {
                builder.append(line);
                builder.append(System.getProperty("line.separator"));
            }
            String result = builder.toString();
            System.out.print(result);
        }
        catch (IOException e)
        {   System.out.print("error");
            e.printStackTrace();
        }
    }

CMD is : enter image description here

I can see the binary output(result) on the log files, but its not downloading as image on local drive.

Vivek Dhiman
  • 1,967
  • 6
  • 46
  • 82
  • 1
    Did you try to print out cmd and run it directly on the shell? – lwi Apr 24 '18 at 06:49
  • What exactly do you expect in the output? An image is binary data, that won't fit a `String` very well. And why do you inject newlines into it? – daniu Apr 24 '18 at 06:52
  • 1
    Because you need a shell to do the pipe trick `>`. Try `cmd -c "curl ... > C:..."`. – xiaofeng.li Apr 24 '18 at 06:55
  • 1
    But what's stopping you from using `HttpURLConnection` if you are already using Java? – xiaofeng.li Apr 24 '18 at 06:57
  • I tried same command on terminal, Its download image in the destination folder. Output, I want the image should be download. What will be the CMD in that case If I am using windows. – Vivek Dhiman Apr 24 '18 at 07:01
  • Is there any reason why you want to use curl when you could just use something like `URL`? – MadProgrammer Apr 24 '18 at 07:06
  • I'm not sure if Java supports OS "redirection" (`>`) through `Process`, might be able to use a batch file, don't know – MadProgrammer Apr 24 '18 at 07:13
  • Then what could be the good option? Server also need the authentication. And later we also need to post the image onto the server. – Vivek Dhiman Apr 24 '18 at 07:15
  • `ProcessBuilder pb = new ProcessBuilder("curl", "-O", "https://....png");` the `-O` (that's Ohh, not zero) will write out the file automatically – MadProgrammer Apr 24 '18 at 07:23
  • *"Server also need the authentication"* - What type? Pretty sure you could get `URL` to write header information to support authentication - never really tried - usually just use Apache Commons HTTP library – MadProgrammer Apr 24 '18 at 07:24
  • --ouput worked for me. – Vivek Dhiman Apr 24 '18 at 07:50
  • @xiaofeng.li+ you need cmd (shell on Unix) for `> <` etc redirections (not pipe), but ProcessBuilder can do redirection since j7, and curl can take _option_ `-o/--output file` instead of redirection, or as noted `-O/--remote-name` to default the filename. – dave_thompson_085 Apr 24 '18 at 07:51

0 Answers0