androidAsync.jar
httpclient-4.5.4.jar
httpmime-4.5.4.jar
String apiUrl = "http://your.api.url";
String filePath = "/path/to/your/file.bin";
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(apiUrl);
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
File file = new File(filePath);
FileBody fileBody = new FileBody(file);
builder.addPart("file", fileBody);
httppost.setEntity(builder.build());
HttpResponse response = httpclient.execute(httppost);
HttpEntity resEntity = response.getEntity();
if (resEntity != null) {
String responseString = EntityUtils.toString(resEntity);
Log.d(TAG, responseString);
}
} catch (Exception e) {
e.printStackTrace();
}
请注意,在上述代码示例中,需要'http://your.api.url”替换为实际的 API 地址,并'/path/to/your/file.bin”替换为实际的文件路径。此外,建议在异步任务或线程中执行上述代码,以避免阻塞主线程。
以上就是 Android Java HTTP Post .bin 文件的上传解决方法。