@Path("uploads")@Produces(MediaType.APPLICATION_JSON)@Consumes(MediaType.APPLICATION_JSON)public class UploadResource extends BaseResource { private static final int DEFAULT_BUFFER_SIZE = 8192; private static final int IMAGE_SIZE_LIMIT = 2000000; private static final int PDF_SIZE_LIMIT = 5000000; private static final Logger LOGGER = LoggerFactory.getLogger(UploadResource.class); private String imageExtension(String type) { switch (type) { case "image/jpeg": return "jpg"; case "image/png": return "png"; case "image/gif": return "gif"; case "image/webp": return "webp"; case "image/svg+xml": return "svg"; default: throw new IllegalArgumentException("Unsupported image type"); } } @Path("{entity}/{id}") @POST @Consumes("image/*") public Response uploadImage(@PathParam("entity") String entity, @PathParam("id") long entityId, File file, @HeaderParam(HttpHeaders.CONTENT_TYPE) String type) { try { // Extract the file extension from the content type String extension = type.substring("image/".length()); String name = entityId +"_" + new Date().getTime(); // Ensure correct buffer size and image size limit handling try (FileInputStream input = new FileInputStream(file); OutputStream output = Context.getMediaManager().createFileStream(entity, name, extension)) { byte[] buffer = new byte[8192]; // Use 8 KB buffer int read; long transferred = 0; // Read in chunks and write to output while ((read = input.read(buffer)) != -1) { output.write(buffer, 0, read); transferred += read; // Check if the image size exceeds the limit if (transferred > IMAGE_SIZE_LIMIT) { throw new IllegalArgumentException("Image size limit exceeded"+transferred); } } output.flush(); // Ensure all data is written } // Return successful response with the generated image file name Map<String, String> result = new HashMap<>(); result.put("image", name +"." + extension); return Response.ok(ResponseHelper.getResultWith_200(result)).build(); } catch (Exception ex) { LOGGER.warn("Image upload failed (LS): ", ex); // Log the entire exception stack trace return Response.ok(ResponseHelper.getResultWith_400(ResponseHelper.formatMsg(ex.getMessage()))).build(); } }}this ia my api cal and below is my mediamanager
public class MediaManager { private static final Logger LOGGER = LoggerFactory.getLogger(MediaManager.class); private final String path; public MediaManager(String path) { this.path = path; } private File createFile(String uniqueId, String name) throws IOException { Path filePath = Paths.get(path, uniqueId, name); Path directoryPath = filePath.getParent(); if (directoryPath != null) { Files.createDirectories(directoryPath); } LOGGER.info("Creating file at path: {}", filePath); return filePath.toFile(); } public OutputStream createFileStream(String uniqueId, String name, String extension) throws IOException { File file = createFile(uniqueId, name +"." + extension); LOGGER.info("Opening output stream for file: {}", file.getAbsolutePath()); return new FileOutputStream(file); }}each time im'm upload image no matter png/jpeg/jpg from postman using binary selectin in body or form-datathe empty image is created with correct name at correct diractory even it with size not like 0kb but correct 502kb and so onbut image is broken unable to pen usin any image viewvwrplease point out what im doing wrong.