首页 > 解决方案 > 如何使用邮递员测试下面给出的获取请求?

问题描述

我想创建一个 android 应用程序来在使用 Spring Boot 作为后端的谷歌驱动器中执行文件操作。所以我在谷歌搜索了很多次,最后我得到了这个。但他们没有提到工作。如果有人知道请帮助我。请推荐一些好的教程来使用 Spring boot rest api 在谷歌驱动器中执行文件操作。

获取在 Google Drive 中创建目录的请求

@GetMapping("/directory/create")
  public ResponseEntity<String> createDirectory(@RequestParam String path) throws Exception {
    String parentId = fileManager.getFolderId(path);
    return ResponseEntity.ok("parentId: "+parentId);
}

getFolderId 函数

public String getFolderId(String path) throws Exception {
    String parentId = null;
    String[] folderNames = path.split("/");

    Drive driveInstance = googleDriveManager.getInstance();
    for (String name : folderNames) {
        parentId = findOrCreateFolder(parentId, name, driveInstance);
    }
    return parentId;
}

findOrCreateFolder 如果给定文件夹在谷歌驱动器中不存在,则创建函数

private String findOrCreateFolder(String parentId, String folderName, Drive driveInstance) throws Exception {
    String folderId = searchFolderId(parentId, folderName, driveInstance);
    // Folder already exists, so return id
    if (folderId != null) {
        return folderId;
    }
    //Folder dont exists, create it and return folderId
    File fileMetadata = new File();
    fileMetadata.setMimeType("application/vnd.google-apps.folder");
    fileMetadata.setName(folderName);

    if (parentId != null) {
        fileMetadata.setParents(Collections.singletonList(parentId));
    }
    return driveInstance.files().create(fileMetadata)
            .setFields("id")
            .execute()
            .getId();
}

发布将文件上传到谷歌驱动器的请求

@PostMapping(value = "/upload",
        consumes = {MediaType.MULTIPART_FORM_DATA_VALUE},
        produces = {MediaType.APPLICATION_JSON_VALUE} )
public ResponseEntity<String> uploadSingleFileExample4(@RequestBody MultipartFile file,@RequestParam(required = false) String path) {
    logger.info("Request contains, File: " + file.getOriginalFilename());
    String fileId = fileManager.uploadFile(file, path);
    if(fileId == null){
        return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).build();
    }
    return ResponseEntity.ok("Success, FileId: "+ fileId);
}

上传功能将文件上传到谷歌驱动器

public String uploadFile(MultipartFile file, String filePath) {
    try {
        String folderId = getFolderId(filePath);
        if (file != null) {
            File fileMetadata = new File();
            fileMetadata.setParents(Collections.singletonList(folderId));
            fileMetadata.setName(file.getOriginalFilename());
            File uploadFile = googleDriveManager.getInstance()
                    .files()
                    .create(fileMetadata, new InputStreamContent(
                            file.getContentType(),
                            new ByteArrayInputStream(file.getBytes()))
                    )
                    .setFields("id").execute();
            return uploadFile.getId();
        }
    } catch (Exception e) {
        System.out.print("Error: "+e);
    }
    return null;
}

参考 https://technicalsand.com/file-operations-in-google-drive-api-with-spring-boot/

标签: spring-bootgoogle-drive-apispring-restcontroller

解决方案


在邮递员中测试上述获取请求。假设您在 localhost:8080 上运行,那么请求将是 -

localhost:8080?path="directory_name"


推荐阅读