gpt4 book ai didi

java - BOX API 上传文件 Java

转载 作者:行者123 更新时间:2023-11-30 07:21:55 24 4
gpt4 key购买 nike

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

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

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

API 的示例是这个curl 请求:

curl https://upload.box.com/api/2.0/files/content \
-H "Authorization: Bearer ACCESS_TOKEN" -X POST \
-F attributes='{"name":"tigers.jpeg", "parent":{"id":"11446498"}}' \
-F file=@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();
}

最佳答案

我找到了解决方案,不同的部分不正确。我必须创建 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();
}

关于java - BOX API 上传文件 Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37415573/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com