问题描述
根据API,我们可以将图像上传为二进制文件.
As per the API we can upload images as a binary file.
https://api.imgur.com/endpoints/image#image-upload
我尝试了以下操作,将文件读入字节数组.
I tried the following to read the file into a byte array.
// Example 1
byte[] fileBytes = new byte[(int) new File("/home/sample.png").length()];
fileBytes = FileUtils.readFileToByteArray( this.imageRequest.getFile() );
String sImageBinaryData = new String( fileBytes );
我应该如何精确提取图像的二进制数据?
How exactly should i extract binary data of an image?
PS:我不知道如何使用(base64& URL)上传图像.
PS: I am not interested to know how to upload image using (base64 & URL).
推荐答案
我使用HttpClient/HttpMime将图像发布为二进制文件
I used HttpClient/HttpMime to post the image as a binary file
"MultipartEntity"当前已被弃用.我不得不使用MultipartEntityBuilder
"MultipartEntity" is currently deprecated. I had to use MultipartEntityBuilder
1)包括以下jar httpmime,它是HttpClient的依赖jar.
1) Included the following jar httpmime which is a dependent jar to HttpClient.
2)
MultipartEntityBuilder builder = MultipartEntityBuilder.create();
builder.setMode(HttpMultipartMode.BROWSER_COMPATIBLE);
File file = new File("/sample.jpg");
FileBody filebody = new FileBody(file);
builder.addPart("image", filebody );
builder.addTextBody("type", "file");
builder.addTextBody("album", '<Deleted hash>' ); //anonymous album
HttpEntity entity = builder.build();
HttpPost httpPost = new HttpPost("<Imgur API endpoint>");
httpPost.setEntity( entity );
CloseableHttpClient httpClient = HttpClientBuilder.create().build()
HttpResponse response = httpClient.execute( httpPost );
这篇关于如何使用图片的二进制格式上传到Imgur(API v3)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!