使用curl将文件上传到Box API

时间:2017-04-07 14:29:10

标签: java curl

我尝试使用Box API将文件上传到Box。但无论我尝试什么,我总是收到400 Bad Request而没有任何其他信息。对这个问题有什么看法吗?

  

卷曲https://upload.box.com/api/2.0/files/content \   -H“授权:持票人ACCESS_TOKEN”-X POST \   -F attributes ='{“name”:“tigers.jpeg”,“parent”:{“id”:“11446498”}}'   -F file=@myfile.jpg

try {  
    String url = "https://upload.box.com/api/2.0/files/content";
    WebResource resource = client.resource(url);

    JSONObject parentInput = new JSONObject();
    parentInput.put("id", "0");

    JSONObject inputJson = new JSONObject();
    fileName = fileName.trim();
    inputJson.put("name", fileName);
    inputJson.put("parent", parentInput);
    input = inputJson.toString();

    FormDataMultiPart  form = new FormDataMultiPart();  

    String cdMeta = "form-data; name=\"attributes\";";
    String cdFile =  "form-data; name=\""+fileName+"\";";

    FormDataContentDisposition cdmetaObj = new FormDataContentDisposition(cdMeta);
    FormDataBodyPart metaDdbpart = new FormDataBodyPart(cdmetaObj, input);
    form.bodyPart(metaDdbpart);

    FormDataContentDisposition cdFileObj = new FormDataContentDisposition(cdFile);

    FormDataBodyPart fileDdbpart = new FormDataBodyPart("file", inputStream,  
    MediaType.APPLICATION_OCTET_STREAM_TYPE);

    fileDdbpart.setContentDisposition(cdFileObj);
    form.bodyPart(fileDdbpart);

    //Here we are using builder for executing client request
    Builder builder = resource.header("Content-Type", 
    MediaType.MULTIPART_FORM_DATA).header("Authorization","Bearer " + accessToken);
    ClientResponse response = builder.post(ClientResponse.class, form);
    String result = response.getEntity(String.class);
} catch (Exception e) {
    e.printStackTrace(e);
}

1 个答案:

答案 0 :(得分:1)

我得到了解决方案,现在它已成功上传,只需稍加改动。这里的问题是正确管理contentDisposition

try {
    String url = "https://upload.box.com/api/2.0/files/content";
    WebResource resource = client.resource(url);

    JSONObject parentInput = new JSONObject();
    parentInput.put("id", "0");

    JSONObject inputJson = new JSONObject();
    fileName = fileName.trim();
    inputJson.put("name", fileName);
    inputJson.put("parent", parentInput);
    input = inputJson.toString();

    FormDataMultiPart form = new FormDataMultiPart();

    String cdMeta = "form-data; name=\"metadata\";";
    String cdFile = "form-data; name=\"content\"; 
    filename = \"" + fileName + "\";";


    FormDataContentDisposition cdmetaObj = new FormDataContentDisposition(cdMeta);
    FormDataBodyPart metaDdbpart = new FormDataBodyPart(cdmetaObj, input);
    form.bodyPart(metaDdbpart);

    FormDataContentDisposition cdFileObj = new FormDataContentDisposition(cdFile);

    FormDataBodyPart fileDdbpart = new FormDataBodyPart("file", inputStream,
        MediaType.APPLICATION_OCTET_STREAM_TYPE);

    fileDdbpart.setContentDisposition(cdFileObj);
    form.bodyPart(fileDdbpart);

    //Here we are using builder for executing client request

    Builder builder = resource.header("Content-Type",
        MediaType.MULTIPART_FORM_DATA).header("Authorization", "Bearer " + accessToken);
    ClientResponse response = builder.post(ClientResponse.class, form);
    String result = response.getEntity(String.class);
} catch (Exception e) {
    e.printStackTrace(e);
}
}