如何使用“

编程入门 行业动态 更新时间:2024-10-11 11:22:42
本文介绍了如何使用“-i --upload-file"转换 curl 调用进入 java Unirest 或任何其他 http 请求?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

以下示例使用 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); }

更多推荐

如何使用“

本文发布于:2023-10-10 22:17:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1479913.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:如何使用

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!