gpt4 book ai didi

java - HTTPClient Apache 模拟 cURL

转载 作者:行者123 更新时间:2023-12-01 14:32:44 25 4
gpt4 key购买 nike

我正在尝试使用他们的 api 将数据上传到 CrowdFlower,为此我正在编写 JAVA 包装器。我正在使用 HttpClient Apache。

CrowdFlower 给出的 cURL 示例如下:curl -T 'sampledata.xlsx' -H 'Content-Type: application/vnd.ms-excel' https://api.crowdflower.com/v1/jobs/upload.json?key={api_key}

这是我的代码:

public InputStream HTTPmethodPostUpload (String authKey, File file) throws ClientProtocolException, IOException{

HttpClient httpclient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost("https://api.crowdflower.com/v1/jobs/upload.json?key="+authKey);

MultipartEntity mpEntity = new MultipartEntity();
ContentBody cbody = new FileBody( file,"application/vnd.ms-excel");
mpEntity.addPart("sampledata.xlsx", cbody );
httpPost.setEntity(mpEntity);
HttpResponse response = httpclient.execute(httpPost);
HttpEntity entityResponse = response.getEntity();
return entityResponse.getContent(); }

但是它返回一个错误并显示以下消息:

{ Not Acceptable 格式,内容类型必须是“格式”中列出的格式之一,但您发送了“multipart/form-data;boundary=yTuwTm4hWmnasxIMB9dC-sxdELIGoNJVudjJdCz\””,“格式” :[“application/vnd.oasis.opendocument.spreadsheet”,“application/vnd.openxmlformats-officedocument.spreadsheetml.sheet”,“application/vnd.ms-excel”,“text/csv”,“text/plain”] }}

我不太了解 Apache HttpClient,所以我不明白我的代码中的问题出在哪里。

最佳答案

你在java中的哪里设置了 header ?

-H 'Content-Type: application/vnd.ms-excel'

尝试以下方法:

mpEntity.setContentType("application/vnd.ms-excel");

关于java - HTTPClient Apache 模拟 cURL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16736112/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com