【问题标题】:BOX API Upload a file JavaBOX API 上传文件 Java
【发布时间】:2016-05-24 13:46:39
【问题描述】:

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

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

对这个问题有任何想法吗?

API 中的示例是这个 curl 请求:

卷曲https://upload.box.com/api/2.0/files/content \
-H "授权:承载 ACCESS_TOKEN" -X POST \
-F 属性='{"name":"tigers.jpeg", "parent":{"id":"11446498"}}' \
-F 文件=@myfile.jpg

我的代码如下:

    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


    【解决方案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();
        }
    

    【讨论】:

      【解决方案2】:

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

      【讨论】:

      • 父 id "11446498" 是 API 中使用的示例。我在我的代码中使用的是有效的,我检查了它。
      • 尝试从 URL 端点中删除尾随的“/”。根据documentation,正确的端点是https://upload.box.com/api/2.0/files/content。您的代码的第一行应该是:String URL = "https://upload.box.com/api/2.0/files/content";
      • 谢谢布伦特,这是我代码中最大的问题!
      【解决方案3】:
      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 授权新应用

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-10-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多