Spring boot Sencha 연동 파일 업로드 구성
2024. 12. 26. 18:27ㆍJava/Spring Boot
Map을 통해서 응답 처리해야 함
Case1
@PostMapping("/location/upload")
public ResponseEntity<?> uploadFile(@RequestParam("file") MultipartFile file) {
try {
// Handle file storage
Path path = Paths.get("uploads/" + file.getOriginalFilename());
Files.write(path, file.getBytes());
// Success response
return ResponseEntity.ok(Map.of("success", true, "message", "File uploaded successfully!"));
} catch (IOException e) {
// Failure response
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
.body(Map.of("success", false, "message", "File upload failed."));
}
}
Case2
@PostMapping("/location/upload")
public Map<String, Ojbect> uploadFile(@RequestParam("file") MultipartFile file) {
Map<String, Object> result = new HashMap<String, Object>();
try {
// Handle file storage
Path path = Paths.get("uploads/" + file.getOriginalFilename());
Files.write(path, file.getBytes());
// Success response
result.push("success", true);
result.push("message", "File uploaded successfully!");
} catch (IOException e) {
// Failure response
result.put("success", false);
result.put("message", "File upload failed.");
}
return result;
}
정상적인 응답
{
"success": true,
"message": "File uploaded successfully!"
}
'Java > Spring Boot' 카테고리의 다른 글
Spring boot 파일업로드 OutOfMemoryError 고려사항 (0) | 2024.12.26 |
---|---|
Spring boot 업로드 디렉토리 설정하기 (0) | 2024.12.26 |
Spring boot X-Frame-Options 처리 (0) | 2024.12.26 |
Spring boot request header 정보 출력하기 (0) | 2024.12.13 |
Spring boot 소스 변경시 자동으로 restart하기 (0) | 2024.12.07 |