Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/api/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用“转换curl调用”-i——上传文件;到java Unirest或任何其他http请求中?_Java_Api_Curl_Linkedin_Unirest - Fatal编程技术网

如何使用“转换curl调用”-i——上传文件;到java Unirest或任何其他http请求中?

如何使用“转换curl调用”-i——上传文件;到java Unirest或任何其他http请求中?,java,api,curl,linkedin,unirest,Java,Api,Curl,Linkedin,Unirest,下面的示例使用cURL上载作为二进制文件包含的图像文件 curl -i --upload-file /path/to/image.png --header "Authorization: Token" 'https://url....' private void uploadMedia(String uploadUrl,String accessToken) throws IOException { RestTemplate restTemplat

下面的示例使用cURL上载作为二进制文件包含的图像文件

curl -i --upload-file /path/to/image.png --header "Authorization: Token" 'https://url....' 
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);
      }
它很好用。我需要从我的Java应用程序发出这个请求

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);
      }
我已经尝试了下一个代码

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();
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);
      }
URL image\u URL=Thread.currentThread().getContextClassLoader().getResource(“jobs\u image.jpg”);
字符串路径=image_url.getFile();
HttpResponse response=Unirest.post(上传URL)
.header(“缓存控制”、“无缓存”)
.标题(“X-Restli-Protocol-Version”、“2.0.0”)
.header(“授权”、“承载人”+令牌+”)
.field(“文件”,新文件(路径))
.asString();
但是,它返回状态400错误请求。 有没有办法从Java调用这样的请求

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);
      }
这是来自LinkedIn v2 API的请求: 我认为curl命令
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);
      }
curl-i--upload file/path/to/image.png--header“Authorization:Token”https://url....“

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);
      }
使用
PUT
,而Java客户端使用
POST

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);
      }
来源:curl的手册页

       -T, --upload-file <file>
              This  transfers  the  specified local file to the remote URL. If
              there is no file part in the specified URL, Curl will append the
              local file name. NOTE that you must use a trailing / on the last
              directory to really prove to Curl that there is no file name  or
              curl will think that your last directory name is the remote file
              name to use. That will most likely cause the upload operation to
              fail. If this is used on an HTTP(S) server, the PUT command will
              be used.
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);
      }
-T,--上传文件
这会将指定的本地文件传输到远程URL。如果
指定的URL中没有文件部分,Curl将附加
本地文件名。请注意,必须在最后一个字符上使用尾随/字符
目录,以真正证明Curl中没有文件名或
curl会认为您的最后一个目录名是远程文件
要使用的名称。这很可能会导致上载操作失败
失败。如果在HTTP(S)服务器上使用此命令,则PUT命令将
被使用。

但不确定这是否是实际问题。您的API文档链接实际上指定了
POST

在我的头撞了几个小时之后,我终于想出了如何将curl调用转换为一个调用(我正在使用Ruby on Rails)

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);
      }
我认为您遇到的问题是必须在请求头中将MIME类型作为内容类型传递

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);
      }
我用它来计算上传到LinkedIn的图像的MIME类型。Minimagik还可以提供LinkedIn所需的二进制图像字符串,因此这是一个双赢的局面

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);
      }
这是最终奏效的呼吁:

file = MiniMagick::Image.open(FILE_PATH)
RestClient.post(UPLOAD_URL, file.to_blob, { 'Authorization': 'Bearer TOKEN', 'Content-Type': file.mime_type })
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);
      }

下面的方法将图像上传到linkedIn

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);
      }
参考:

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);
      }
private void uploadMedia(字符串uploadUrl,字符串accessToken)抛出IOException{
RestTemplate RestTemplate=新RestTemplate();
HttpHeaders=新的HttpHeaders();
添加(“授权”、“承载人”+accessToken);
byte[]fileContents=Files.readAllBytes(新
文件(“路径到本地文件”).toPath();
HttpEntity=新的HttpEntity(文件内容、标题);
exchange(uploadUrl,HttpMethod.PUT,entity,String.class);
}

我尝试了POST和PUT两种方法。我认为问题出在我的请求的错误结构上。我在使用php curl进行图像上传请求时遇到了同样的错误请求问题。但是当我从命令行发出curl-I--upload文件请求时,它工作得很好。仍在寻找答案。你找到上传的方法了吗?请在下面查看我的答案。我没有使用PHP或Java,但我认为所有语言的问题都是一样的。您需要在请求头中将图像的MIME类型作为内容类型传递。上传!我在将这个调用转换到Ruby的Rest客户端时遇到了同样的问题,但后来我发现了另一个问题(已经解决)。我还没有机会回到这一次。我也遇到了同样的问题,您可以在这里看到我的解决方案: