将google驱动器上的文档上传到文件夹中

时间:2015-10-21 07:40:35

标签: java spring

我想在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;

}

请帮我将文档插入文件夹。

1 个答案:

答案 0 :(得分:0)

使用

        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();

现在,请您建议我如何生成subfoler。