【问题标题】:File upload failure on Google Drive APIGoogle Drive API 上的文件上传失败
【发布时间】:2013-07-15 09:10:51
【问题描述】:

我正在尝试使用 Jersey 2.0 为 Google Drive 实现 REST 客户端。

根据以下文档,我编写了通过多部分请求发送文件的代码。

https://developers.google.com/drive/v2/reference/files/insert https://developers.google.com/drive/manage-uploads

    String targetUrl = "https://www.googleapis.com/upload/drive/v2/files?uploadType=multipart&access_token=" + token.access_token;
    WebTarget target = client.target(targetUrl);
    final FileDataBodyPart filePart = new FileDataBodyPart("file", file);
    final MultiPart multipart = new FormDataMultiPart().field("title", file.getName())
        .field("description", file.getName())
        .bodyPart(filePart);
    Response response = target.request(MediaType.APPLICATION_JSON_TYPE)
            .post(Entity.entity(multipart, multipart.getMediaType()));
    System.out.println("response:" + response.readEntity(String.class));

这是代码的 POST 请求。

POST /upload/drive/v2/files?uploadType=multipart&access_token={ACCESS_TOKEN} HTTP/1.1\r\n
Accept: application/json\r\n
Content-Type: multipart/form-data; boundary=Boundary_1_1833261898_1373877178038\r\n
User-Agent: Jersey/2.0 (HttpUrlConnection 1.7.0_25)\r\n
MIME-Version: 1.0\r\n
Host: www.googleapis.com\r\n
Content-Length: 42430\r\n

MIME Multipart Media Encapsulation, Type: multipart/form-data, Boundary: "Boundary_1_1833261898_1373877178038"
Type: multipart/form-data
First boundary: --Boundary_1_1833261898_1373877178038\r\n
Encapsulated multipart part:  (text/plain)
    Content-Type: text/plain\r\n
    Content-Disposition: form-data; name="title"\r\n\r\n
    Line-based text data: text/plain
Boundary: \r\n--Boundary_1_1833261898_1373877178038\r\n
Encapsulated multipart part:  (text/plain)
    Content-Type: text/plain\r\n
    Content-Disposition: form-data; name="description"\r\n\r\n
    Line-based text data: text/plain
Boundary: \r\n--Boundary_1_1833261898_1373877178038\r\n
Encapsulated multipart part:  (text/plain)
    Content-Type: text/plain\r\n
    Content-Disposition: form-data; filename="GoogleDrive.txt"; modification-date="Fri, 12 Jul 2013 08:15:47 GMT"; size=41918; name="file"\r\n\r\n
    Line-based text data: text/plain
Last boundary: \r\n--Boundary_1_1833261898_1373877178038--\r\n

我的发帖请求已发送,但出现以下错误。

{
 "error": {
  "errors": [
   {
    "domain": "global",
    "reason": "parseError",
    "message": "Parse Error"
   }
  ],
  "code": 400,
  "message": "Parse Error"
 }
}

请告诉我如何避免此错误。

【问题讨论】:

    标签: jersey google-drive-api jersey-client jersey-2.0


    【解决方案1】:

    一个上传请求应该有两个多部分:元数据和文件内容,元数据部分应该是 JSON。

    在您的情况下,您为每个属性创建一个新的表单编码部分。使用文档上的参考来查看正确的格式:https://developers.google.com/drive/manage-uploads#multipart

    final MultiPart multipart = new FormDataMultiPart().field("title", file.getName())
        .field("description", file.getName())
        .bodyPart(filePart);
    

    您的多部分正文应该是有效的 JSON。

    【讨论】:

    • 感谢您的回复,多根。正如您所说,我修改了代码并确认我的代码工作正常并且可以将文件发送到 Google Drive。真的非常感谢。
    猜你喜欢
    • 1970-01-01
    • 2020-03-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-06
    • 1970-01-01
    相关资源
    最近更新 更多