-4

Here is my curl code

curl \

-F 'image1=YOUR_IMAGE_URL' \

-F 'image2=YOUR_IMAGE_URL' \

-H 'api-key:YOUR_API_KEY' \

https://url.org/

how can I use this with a HttpUrlConnection

Tom Tom
  • 13
  • 1
  • 3
  • 1
    you can't, you need to use a http library in java. – Jocke May 11 '20 at 07:40
  • 1
    Does this answer your question? [Java - sending HTTP parameters via POST method easily](https://stackoverflow.com/questions/4205980/java-sending-http-parameters-via-post-method-easily) – tevemadar May 11 '20 at 07:42
  • 1
    Your question is unclear. You could simply make a *system call* within java and run curl. But as Jocke says, the normal way would be to do such things *programmatically*. In other words: you have to spend some time to *learn* how you write code in java that makes use of http(s) connections. – GhostCat May 11 '20 at 07:43
  • 1
    See proposed dupe, the `-F`-s are the form fields, and the API key goes to `setRequestProperty()`. – tevemadar May 11 '20 at 07:43

1 Answers1

2

you can use the code below : `

String charset = "UTF-8";
String lineEnd = "\r\n";
String twoHyphens = "--";
String boundary = Long.toHexString(System.currentTimeMillis()); // Just generate some unique random value.
String CRLF = "\r\n";
int bytesRead, bytesAvailable, bufferSize;
byte[] buffer;
int maxBufferSize = 1 * 1024 *1024*1024*100;
String param = "value";
URL url = new URL(urlServer);
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("PUT");
connection.setRequestProperty("api-key", "your-api-key");
outputStream = new DataOutputStream(connection.getOutputStream());
OutputStream output=connection.getOutputStream();
PrintWriter writer = new PrintWriter(new OutputStreamWriter(output, charset), true);
FileInputStream fileInputStream = new FileInputStream(new File(pathToOurFile));
writer.append("Content-Type: text/plain; charset=" + charset).append(CRLF);
writer.append(CRLF).append(param).append(CRLF).flush();
// Send text file.
writer.append("--" + boundary).append(CRLF);
writer.append("Content-Disposition: form-data; name=\"image1\"; filename=\"" + file.getName() + "\"").append(CRLF)
writer.append("Content-Type: text/plain; charset=" + charset).append(CRLF); 
writer.append(CRLF).flush(); 
Files.copy(file.toPath(), output);
output.flush(); 
writer.append(CRLF).flush(); 
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
buffer = new byte[bufferSize];
// Read file
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
System.out.println("Image length " + bytesAvailable + "");
try {
    while (bytesRead > 0) {
          try {
          outputStream.write(buffer, 0, bufferSize);
           } catch (OutOfMemoryError e) {
           e.printStackTrace();
           }
bytesAvailable = fileInputStream.available();
bufferSize = Math.min(bytesAvailable, maxBufferSize);
bytesRead = fileInputStream.read(buffer, 0, bufferSize);
                 }
             } catch (Exception e) {
                 e.printStackTrace();
             }
outputStream.writeBytes(lineEnd);
outputStream.writeBytes(twoHyphens + boundary + twoHyphens+lineEnd);
int serverResponseCode = connection.getResponseCode();
String serverResponseMessage = connection.getResponseMessage();

`

zahra.dgh
  • 123
  • 9