英文:
Java-spingboot how to save text document via REST-API?
问题
我是一名初学者程序员,需要帮忙实现通过 REST API 在 Java 中上传文本文件。
我已经实现了最简单的操作 - 从服务器卸载文件,以下是我的代码:
@GetMapping(value = "/file/{filename:.+}")
public ResponseEntity<Resource> unloadFile(@PathVariable String filename) {
Resource file = storageService.loadAsResource(filename);
return ResponseEntity.ok().header(HttpHeaders.CONTENT_DISPOSITION,
"attachment; filename=\"" + file.getFilename() + "\"").body(file);
}
我可以通过简单地访问链接来测试文件卸载!但是我无法测试上传。我发现编写测试很困难。请告诉我是否有一个可行的代码,也许有更好的上传方式。这是我的上传代码:
@PostMapping(value = "/file")
public ResponseEntity<MultipartFile> uploadFile(MultipartFile file) {
storageService.store(file);
return ResponseEntity.ok().body(file);
}
非常感谢!
英文:
I am a beginner programmer, help with the implementation of uploading a text file via rest-api java.
I have already implemented the simplest action - unload a file from the server, here is my code:
@GetMapping(value = "/file/{filename:.+}")
public ResponseEntity<Resource> unloadFile(@PathVariable String filename) {
Resource file = storageService.loadAsResource(filename);
return ResponseEntity.ok().header(HttpHeaders.CONTENT_DISPOSITION,
"attachment; filename=\"" + file.getFilename() + "\"").body(file);
}
I can test the file unload by simply following the link!
I cannot test the upload. I find it difficult to write tests. Please tell me if I got a working code and maybe there is a better way to upload. My code upload:
@PostMapping(value = "/file")
public ResponseEntity<MultipartFile> uploadFile(MultipartFile file) {
storageService.store(file);
return ResponseEntity.ok().body(file);
}
Thank you so much!
答案1
得分: 0
以下是您要翻译的内容:
上传文件使用 Spring Boot 应用程序的方法与我们在 servlet 容器中使用的方法相同。在您的控制器中:
@PostMapping("/uploadFile")
public ResponseEntity<Object> uploadFile(@RequestParam("file") MultipartFile file) {
String fileName = yourStorageService.storeFile(file);
String fileDownloadUri = ServletUriComponentsBuilder.fromCurrentContextPath()
.path("/downloadFile/")
.path(fileName)
.toUriString();
// 您甚至可以生成下载链接。
return new ResponseEntity<Object>("Uploaded", HttpStatus.OK, fileDownloadUri);
}
下载文件的方法:
@GetMapping("/downloadFile/{fileName}")
public ResponseEntity<Resource> downloadFile(@PathVariable String fileName, HttpServletRequest request) {
// 从数据库或本地加载文件作为资源
Resource resource = fileStorageService.loadFileAsResource(fileName);
return ResponseEntity.ok()
.contentType(MediaType.parseMediaType(contentType))
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + resource.getFilename() + "\"")
.body(resource);
}
英文:
To upload the file/files using spring boot application we use same method that we had for servlet containers. In your controller
@PostMapping("/uploadFile")
public ResponseEntity<Object> uploadFile(@RequestParam("file") MultipartFile file) {
String fileName = yourStorageService.storeFile(file);
String = ServletUriComponentsBuilder.fromCurrentContextPath()
.path("/downloadFile/")
.path(fileName)
.toUriString();
//You can even generate download links.
return new ResponseEntity<Object>(HttpStatus.Ok, "Uploaded", fileDownloadUri);
}
To download the files:
@GetMapping("/downloadFile/{fileName}")
public ResponseEntity<Resource> downloadFile(@PathVariable String fileName, HttpServletRequest request) {
// Load file as Resource from DB or local
Resource resource = fileStorageService.loadFileAsResource(fileName);
return ResponseEntity.ok()
.contentType(MediaType.parseMediaType(contentType))
.header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + resource.getFilename() + "\"")
.body(resource);
答案2
得分: 0
对于 @PostMapping(value = "/file")
端点,最好返回成功/错误状态,而不是返回文件,如果文件较大... 这需要时间才能返回。
最好返回成功状态,200 OK。
英文:
For @PostMapping(value = "/file") endpoint , its best to return a success/error status instead of returning the file,if file is larger ..it takes time to return back.
Better to return success state. 200 ok.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论