Image error solve update
This commit is contained in:
@ -18,7 +18,7 @@ import java.util.UUID;
|
||||
@RestController
|
||||
@RequestMapping("/api/files")
|
||||
@CrossOrigin(origins = "*")
|
||||
public class FileController { // Changed from FileUploadController to FileController
|
||||
public class FileController {
|
||||
|
||||
@Value("${file.upload.directory:uploads}")
|
||||
private String uploadDirectory;
|
||||
@ -31,6 +31,7 @@ public class FileController { // Changed from FileUploadController to FileContro
|
||||
System.out.println("=== FILE UPLOAD DEBUG ===");
|
||||
System.out.println("File name: " + file.getOriginalFilename());
|
||||
System.out.println("Content type: " + file.getContentType());
|
||||
System.out.println("Base URL: " + baseUrl);
|
||||
|
||||
try {
|
||||
if (file.isEmpty()) {
|
||||
@ -58,12 +59,14 @@ public class FileController { // Changed from FileUploadController to FileContro
|
||||
Path filePath = uploadPath.resolve(uniqueFilename);
|
||||
Files.copy(file.getInputStream(), filePath, StandardCopyOption.REPLACE_EXISTING);
|
||||
|
||||
// Return file URL - use generic /files/ path for documents
|
||||
String fileUrl = "/uploads/" + uniqueFilename;
|
||||
// FIXED: Return full URL with base URL
|
||||
String fileUrl = baseUrl + "/uploads/" + uniqueFilename;
|
||||
Map<String, String> response = new HashMap<>();
|
||||
response.put("url", fileUrl);
|
||||
response.put("filename", uniqueFilename);
|
||||
|
||||
System.out.println("File uploaded successfully. URL: " + fileUrl);
|
||||
|
||||
return ResponseEntity.ok(response);
|
||||
|
||||
} catch (IOException e) {
|
||||
@ -88,6 +91,23 @@ public class FileController { // Changed from FileUploadController to FileContro
|
||||
);
|
||||
}
|
||||
|
||||
@DeleteMapping("/images/{filename}")
|
||||
public ResponseEntity<?> deleteImage(@PathVariable String filename) {
|
||||
try {
|
||||
Path filePath = Paths.get(uploadDirectory).resolve(filename);
|
||||
if (!Files.exists(filePath)) {
|
||||
return ResponseEntity.notFound().build();
|
||||
}
|
||||
|
||||
Files.delete(filePath);
|
||||
return ResponseEntity.ok().body(Map.of("message", "File deleted successfully"));
|
||||
|
||||
} catch (IOException e) {
|
||||
return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR)
|
||||
.body("Failed to delete file: " + e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@GetMapping("/images/{filename}")
|
||||
public ResponseEntity<byte[]> getImage(@PathVariable String filename) {
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user