我正在尝试使用他们的api将数据上传到CrowdFlower,我正在编写JAVA包装器。我正在使用HttpClient Apache。
CrowrFlower给出的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(); }
Hower它使用以下消息返回错误:
{不可接受的格式,Content-Type必须是\“formats \”中列出的格式之一,但您发送了“multipart / form-data; boundary = yTuwTm4hWmnasxIMB9dC-sxdELIGoNJVudjJdCz \”“,”格式“ :[ “应用/ vnd.oasis.opendocument.spreadsheet”, “应用/ vnd.openxmlformats-officedocument.spreadsheetml.sheet”, “应用/ vnd.ms-EXCEL”, “文本/ CSV”, “文本/纯”] }}
我不太了解Apache HttpClient,所以我不明白我的代码中的问题在哪里。
答案 0 :(得分:1)
在java中你设置了标题?
-H 'Content-Type: application/vnd.ms-excel'
尝试以下方法:
mpEntity.setContentType("application/vnd.ms-excel");