【问题标题】:Google drive file uploads getting converted to doc谷歌驱动器文件上传被转换为文档
【发布时间】:2017-06-11 17:12:33
【问题描述】:

我正在使用 google drive V3 REST api 从 POST 请求上传多部分文件,下面是 java 代码

                String accessToken = "xyz";
                    Credential credential = new GoogleCredential().setAccessToken(accessToken);
                    Drive service = new Drive.Builder(HTTP_TRANSPORT, JSON_FACTORY, credential)
                            .setApplicationName("xyz")
                            .build();
                File fileMeta = new File();
                fileMeta.setName(fileName);
                fileMeta.setMimeType("application/vnd.google-apps.file");
                if (!parentFolderId.isEmpty()) {
                    fileMeta.setParents(Collections.singletonList(parentFolderId));
                }
                //Code to upload file in GDrive
                java.io.File tmpFile = java.io.File.createTempFile("uploadFile", ".tmp");

                //Writing the file content to the new file
                InputStream in = multipartFile.getInputStream();
                FileOutputStream fos = new FileOutputStream(tmpFile);

                IOUtils.copy(in, fos);

                in.close();
                fos.close();

                tmpFile.deleteOnExit();
                FileContent mediaContent = new FileContent(null, tmpFile);
                File file = service.files().create(fileMeta, mediaContent)
                        .setFields("id")
                        .execute();

上传工作正常,但所有上传都自动转换为我不想要的文档。我从问题Google Drive: Automatically convert files on upload? 中看到,在插入期间需要设置convert=true。但此选项在 V3 中不可用。

谁能告诉我如何在上传期间禁用此自动转换?

【问题讨论】:

    标签: java file-upload google-drive-api file-conversion


    【解决方案1】:

    自动转换的原因是因为在文件对象中设置了 mime 类型,因此每个上传的文件都被转换为谷歌文档

    fileMeta.setMimeType("application/vnd.google-apps.file");
    

    上传时文件对象中未设置 mimeType 后,此问题已解决

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-07-06
      • 1970-01-01
      • 2021-10-22
      • 2021-04-29
      • 1970-01-01
      • 1970-01-01
      • 2021-06-01
      • 2019-08-04
      相关资源
      最近更新 更多