Applilation / Octet-Stream - Android

时间:2015-12-15 17:16:20

标签: android

我需要以application / octet-stream格式传递图像。我认为这意味着二进制图像数据。如何将我的drawable转换为这种格式?

以下是我将代替身体传递此数据的代码:

StringEntity reqEntity = new StringEntity(“{body}”);

1 个答案:

答案 0 :(得分:0)

您可以使用HttpURLConnection,如下所示:

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();