'Application/octet-stream

I need to pass an image in application/octet-stream format. I think it means binary image data. How can I convert my drawable to this format?

Here is the code where I'll pass this data in the place of body :

StringEntity reqEntity = new StringEntity("{body}");



Solution 1:[1]

You can use HttpURLConnection, something like this:

Long BUFFER_SIZE = 4096;
String method = "POST";
String filePath = "FILE_NAME"

File uploadFile = new File(filePath);

if (!(uploadFile.isFile() && uploadFile.exists())) {
    println 'File Not Found !!!!'
    return;
}

URL url = new URL("http://your_url_here/" + uploadFile.name);
HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();

String contentType = "application/octet-stream"

httpConn.setDoOutput(true);
httpConn.setRequestMethod(method);
httpConn.setRequestProperty("Accept", "*/*");
httpConn.setRequestProperty("Content-type", contentType);
OutputStream outputStream = httpConn.getOutputStream();

FileInputStream inputStream = new FileInputStream(uploadFile);
byte[] buffer = new byte[BUFFER_SIZE];
int bytesRead = -1;

while ((bytesRead = inputStream.read(buffer)) != -1) {
    outputStream.write(buffer, 0, bytesRead);
}

outputStream.close();
inputStream.close();
println "Response message : "+httpConn.getResponseMessage();

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Anderson K