spring-boot - 如何使用邮递员测试下面给出的获取请求?
问题描述
我想创建一个 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/
解决方案
在邮递员中测试上述获取请求。假设您在 localhost:8080 上运行,那么请求将是 -
localhost:8080?path="directory_name"
推荐阅读
- java - 布尔默认值 MapStruct
- amazon-web-services - 推荐用于 Adobe ColdFusion 生产环境的 Docker 映像是什么?
- c - C printf 可选小数位
- javascript - 重构 Express 代码并且无法确定变量范围
- python - 不同python版本的子流程执行
- java - 使用 Optional.map() 而不是 Optional.ifPresent() 来执行可能引发异常的代码并返回任何异常作为返回值
- javascript - 有没有办法让一个元素旋转而没有一个变量告诉它已经转动了多远?
- javascript - JavaScript 操作 DOM(onclick 事件)
- powershell - Powershell sshd 命令不同
- python - Flask - 无法覆盖需要不同子域的蓝图的默认子域