BOX API上传文件Java

问题描述 投票:1回答:3

我尝试使用Box API将文件上传到Box。

但无论我尝试什么,我总是收到400 Bad Request而没有任何其他信息。

对这个问题有什么看法吗?

API中的示例是此curl请求:

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

我的代码如下:

    String URL = "https://upload.box.com/api/2.0/files/content/";

    HttpClient httpClient = new HttpClient();
    PostMethod postMethod = new PostMethod(URL);
    postMethod.setRequestHeader("Authorization", "Bearer "+ this.token);

    try {
        List<Part> parts = new ArrayList<Part>();

        JSONObject parent = new JSONObject();
        parent.put("id", this.parentId);

        JSONObject attributes = new JSONObject();
        attributes.put("parent", parent);
        attributes.put("name", file.getName());

        StringPart strPart = new StringPart("attributes", attributes.toString());
        strPart.setContentType("application/json");
        parts.add(strPart);

        ByteArrayPartSource source = new ByteArrayPartSource(file.getName(),
                IOUtils.toByteArray(this.file);
        parts.add(new FilePart("file", source));

        postMethod.setRequestEntity(new MultipartRequestEntity(parts.toArray(new Part[0]), postMethod.getParams()));
        httpClient.executeMethod(postMethod);

        int status = postMethod.getStatusCode();

        if (status == HttpURLConnection.HTTP_OK || status == HttpURLConnection.HTTP_ACCEPTED) {
            String jsonText = postMethod.getResponseBodyAsString();
            JSONObject json = new JSONObject(jsonText);
            System.out.println(jsonText);
        } else {
            throw new MyException(postMethod.getResponseBodyAsString());
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        postMethod.releaseConnection();
    }
java rest multipartform-data box-api
3个回答
0
投票

您验证了父ID = 11446498是否有效?如果您只是测试此端点,请尝试使用ID = 0表示根文件夹。


1
投票

我找到了解决方案,不同的部分不正确。我必须创建3个部分:

  1. Parent_id:父文件夹的id
  2. 元数据:json
  3. 文件:要上传的文件

此代码有效:

    String URL = "https://upload.box.com/api/2.0/files/content";
    HttpClient httpClient = new HttpClient();
    PostMethod postMethod = new PostMethod(URL);
    postMethod.setRequestHeader("Authorization", "Bearer "+ this.token);

    try {
        List<Part> parts = new ArrayList<Part>();

        parts.add(new StringPart("parent_id", parentId));

        JSONObject parent = new JSONObject();
        parent.put("id", this.parentId);

        JSONObject attributes = new JSONObject();
        attributes.put("parent", parent);
        attributes.put("name", file.getName());

        StringPart strPart = new StringPart("metadata", attributes.toString());
        strPart.setContentType("text/plain");
        parts.add(strPart);

        ByteArrayPartSource source = new ByteArrayPartSource(file.getName(),
                IOUtils.toByteArray(this.file));
        parts.add(new FilePart("file", source));

        postMethod.setRequestEntity(new MultipartRequestEntity(parts.toArray(new Part[0]), postMethod.getParams()));
        httpClient.executeMethod(postMethod);

        // checks server's status code first
        int status = postMethod.getStatusCode();
        System.out.println(status);
        if (status == HttpURLConnection.HTTP_OK || status == HttpURLConnection.HTTP_CREATED) {
            String jsonText = postMethod.getResponseBodyAsString();
            JSONObject json = new JSONObject(jsonText);
            System.out.println(jsonText);
        } else {
            throw new MyException(postMethod.getResponseBodyAsString());
        }
    } catch (Exception e) {
        e.printStackTrace();
    } finally {
        postMethod.releaseConnection();
    }

0
投票
BoxConfig boxConfig = BoxConfig.readFrom(new FileReader("box_config.json"));  //.json configuration file can be downloaded from dev console based on your app settings      
BoxAPIConnection api = BoxDeveloperEditionAPIConnection.
                                          getAppUserConnection(USER_ID, boxConfig);
BoxFolder boxFolder = new BoxFolder(api, FOLDER_ID);
boxFolder.uploadFile(stream, filename);

请注意,仅创建应用程序并将其设置为读取和写入是不够的,还需要在管理控制台中对应用程序进行授权 - 企业设置。使用客户端ID授权新应用

© www.soinside.com 2019 - 2024. All rights reserved.