0

我正在尝试使用他们的 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,所以我不明白我的代码中的问题在哪里。

4

1 回答 1

1

你在java哪里设置标题?

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

试试下面的:

mpEntity.setContentType("application/vnd.ms-excel");
于 2013-05-24T14:33:06.997 回答