繁体   English   中英

在文件夹中的Google Drive上上传文档

[英]Upload document on google drive in a folder

我想将我的文档上传到Google驱动器上,但是在文件夹中。 您能建议我如何将其插入文件夹吗? 我已经上传了,但这不在文件夹中。 代码是-

@RequestMapping(value = "/uploadDDFile", method = RequestMethod.POST)
public ModelAndView uploadDDFile(@RequestParam(value = "ddid", required = true) Integer ddid,
        @RequestParam(value = "catageryId", required = true) Integer catageryId,
        @RequestParam(value = "document", required = true) GMultipartFile document[], HttpServletRequest request) {
    System.out.println("-------------------------");
    String name = "";
    DdeDriveDocuments ddeDriveDocuments = new DdeDriveDocuments();

    if (ServletFileUpload.isMultipartContent(request) && document != null) {
        for (GMultipartFile gdocument : document) {
            try {
                boolean user = true;
                List<DdeDriveDocuments> dds = ddeDriveDocumentsService.fatchData(ddid, catageryId);
                for (DdeDriveDocuments file : dds) {
                    System.out.println(file.getDocument_name());
                    if (file.getDocument_name().equals(gdocument.getOriginalFilename())) {
                        user = false;
                    }
                }
                if (user == true) {
                    Client client = sessionService.getClient();
                    System.out.println(gdocument.getOriginalFilename());
                    ddeDriveDocuments
                            .setDocument_name((gdocument.getName() != null ? gdocument.getOriginalFilename() : ""));
                    ddeDriveDocuments.setDocument_uploadby(client.getEmail());
                    ddeDriveDocuments.setDocument_created(new Date());
                    ddeDriveDocuments.setCatagery_id(catageryId);
                    ddeDriveDocuments.setDd_id(ddid);
                    ddeDriveDocuments.setDd_uuid(GeneralUtil.getUUID());
                    ddeDriveDocuments.setClientID(client.getClientID());
                    Lawyer googleAuthToken = lawyerService
                            .getAuthorisedUserToken(Configurator.getInstance().getDriveAccountEmail());
                    if (googleAuthToken != null) {
                        // upload file in drive
                        if (ServletFileUpload.isMultipartContent(request) && document != null) {

                            // It's value either we need to get from form.
                            String description = "Testing";

                            File file = DriveService.uploadDocumentToDrive(googleAuthToken, gdocument,
                                    ddeDriveDocuments.getDocument_name(), description);

                            File thumFile = DriveService.getFileById(googleAuthToken, file.getId());
                            System.out.println("thumFile ====" + thumFile);
                            System.out.println("thab url" + thumFile.getIconLink());

                            if (file != null) {
                                ddeDriveDocuments.setDocument_drive_id(file.getId());
                                ddeDriveDocuments.setImageurl(thumFile.getIconLink());
                                ddeDriveDocuments = ddeDriveDocumentsService.create(ddeDriveDocuments);
                            }
                        }
                    } else {
                        System.out.println("Autorised token not available for configured drive account.");
                    }
                } else {
                    System.out.println("wroung Input");
                    System.out.println("wroung Input");
                    name = name.concat(gdocument.getOriginalFilename() + " , ");

                    System.out.println("This is ::::::::::::: " + name);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    if(name !=""){
    sessionService.setUnupload_files_name(name);
    }
    return new ModelAndView("redirect:/al/client/ddeclientportal/" + ddid + "/" + catageryId);
}





public static File uploadDocumentToDrive(Lawyer googleAuthToken,
        GMultipartFile file, String fileName,  String description) {

    File driveFile = null;
    try {

        InputStream fileStream = file.getInputStream();
        String mimeType = DocumentListEntry.MediaType.fromFileName(
                file.getOriginalFilename()).getMimeType();
        GoogleCredential googleCrednetial = getGoogleCredential(googleAuthToken);
        Drive drive = buildService(googleCrednetial);

        String file_name = fileName.contains(FilenameUtils.getExtension(file.getOriginalFilename())) ? fileName : fileName + "." + FilenameUtils.getExtension(file.getOriginalFilename());


        File body1 = new File();
        body1.setTitle("cloudbox");
        body1.setMimeType("application/vnd.google-apps.folder");
        driveFile = drive.files().insert(body1).execute();




        File body = new File();
        body.setTitle(file_name);
        body.setDescription(description);
        body.setMimeType(mimeType);
        driveFile = drive.files()
                .insert(body, new InputStreamContent(mimeType, fileStream))
                .execute();

    } catch (Exception e) {
        e.printStackTrace();
    }

    return driveFile;

}

请帮助我要将我的文档插入文件夹中。

通过使用

        File body1 = new File();
        body1.setTitle("cloudbox");

        body1.setMimeType("application/vnd.google-apps.folder");
        driveFile = drive.files().insert(body1).execute();



        File body = new File();
        body.setTitle(file_name);
        body.setDescription(description);
        body.setMimeType(mimeType);
        body.setParents(Arrays.asList(new ParentReference().setId(driveFile.getId()))); 
        driveFile = drive.files()
                .insert(body, new InputStreamContent(mimeType, fileStream))
                .execute();

现在,您能建议我如何生成子对象。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM