以下示例使用 cURL 上传包含为二进制文件的图像文件.
The example below uses cURL to upload image file included as a binary file.
curl -i --upload-file /path/to/image.png --header "Authorization: Token" 'url....'它工作正常.我需要从我的 Java 应用程序发出这个请求.
It works fine. I need to make this request from my Java application.
我已经尝试了下一个代码
I have tried next code
URL image_url = Thread.currentThread().getContextClassLoader().getResource("jobs_image.jpg"); String path = image_url.getFile(); HttpResponse<String> response = Unirest.post(uploadUrl) .header("cache-control", "no-cache") .header("X-Restli-Protocol-Version", "2.0.0") .header("Authorization", "Bearer " + token + "") .field("file", new File(path)) .asString();但是,它返回状态 400 Bad Request.有没有办法从Java调用这样的请求?
However, it returns status 400 Bad Request. Is there any way to call such request from Java?
这是来自 LinkedIn v2 API 的请求:docs.microsoft/en-us/linkedin/consumer/integrations/self-serve/share-on-linkedin?context=linkedin/consumer/context#upload-image-二进制文件
This is a request from LinkedIn v2 API: docs.microsoft/en-us/linkedin/consumer/integrations/self-serve/share-on-linkedin?context=linkedin/consumer/context#upload-image-binary-file
推荐答案下面的方法会将图片上传到linkedIn
Below method will upload the image to linkedIn
参考:docs.microsoft/en-us/linkedin/marketing/integrations/community-management/shares/vector-asset-api#upload-the-image
private void uploadMedia(String uploadUrl,String accessToken) throws IOException { RestTemplate restTemplate = new RestTemplate(); HttpHeaders headers = new HttpHeaders(); headers.add("Authorization","Bearer "+accessToken); byte[] fileContents = Files.readAllBytes(new File("path_to_local_file").toPath()); HttpEntity<byte[]> entity = new HttpEntity<>(fileContents, headers); restTemplate.exchange(uploadUrl,HttpMethod.PUT, entity, String.class); }更多推荐
如何使用“
发布评论