【问题标题】:Use Multipart while uploading上传时使用 Multipart
【发布时间】:2013-09-18 00:47:05
【问题描述】:

我没有使用 google drive 的 sdk 我想通过我的应用程序在我的谷歌驱动器帐户上上传一个文件,我已为其设置了 url、headers、filename 的 post api。但我收到错误为“错误的内容类型。使用 multipart”。 我的代码如下-

文件文件 = 新文件(文件名);

字符串 url = "https://www.googleapis.com/upload/drive/v2/files/" + file.getName() + "?uploadType=multipart&access_token=" + accessToken;

headers.put("Content-type", "multipart/related");

response = GlobalConstants.SERVICE_CLIENT.Execute(url, "post", file, headers); //调用发帖。

请尽早提供帮助 提前谢谢

【问题讨论】:

    标签: google-drive-api


    【解决方案1】:

    您需要阅读并理解此页面https://developers.google.com/drive/v2/reference/files/insert

    尝试“立即尝试”链接 (https://developers.google.com/drive/v2/reference/files/insert#try-it) 并查看 URL 和有效负载是如何形成的,然后您的应用需要重新创建它。您代码中的 URL 与 Drive API 所需的 URL 完全不同。

    【讨论】:

    • 谢谢回复,我更新的代码如下 String url = "googleapis.com/upload/drive/v2/…" + accessToken; headers.put("内容类型", "multipart/related"); headers.put("内容长度", String.valueOf(file.length())); response = GlobalConstants.SERVICE_CLIENT.Execute(url, "post", file, headers); jsonFile = new JSONObject(响应);我仍然收到错误,因为“不良内容使用多类型”并且文件未上传。标题和 url 根据文档。
    • 如果我将标题保留为空白,则文件正在上传,但为“无标题”,并且文件的 mimetype 始终为“application/octet-stream”文件包含数据但没有原始标题和扩展名。请提前提供帮助和thnx
    • 我需要查看实际的 URL 和请求,而不是您的代码。您可以使用调试或代理来跟踪 http
    • URL - googleapis.com/upload/drive/v2/… 请求 - 我们正在尝试上传请求中的文件内容如果我按照文档“401”将 accessToken 放入“授权”中,则会遇到错误。即使认为 accessToken 没有过期并且是有效的
    • 使用“Logger.getLogger(HttpTransport.class.getName()).setLevel(Level.CONFIG);”开启日志记录。这将捕获请求和响应,并提供一些线索为什么它不工作。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-12-22
    • 2018-06-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-31
    • 1970-01-01
    相关资源
    最近更新 更多