我正在尝试使用他们的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(); }


但是,它通过以下消息向我返回错误:

{不可接受的格式,Content-Type必须是\“ formats \”中列出的格式之一,但是您发送了\“ multipart / form-data; boundary = yTuwTm4hWmnasxIMB9dC-sxdELIGoNJVudjJdCz \”,“ formats”:[“ application / vnd .oasis.opendocument.spreadsheet”,“ application / vnd.openxmlformats-officedocument.spreadsheetml.sheet”,“ application / vnd.ms-excel”,“ text / csv”,“ text / plain”]}}

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

最佳答案

您在Java中的哪里设置了标头?

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


请尝试以下方法:

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

09-25 22:18