import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.util.Base64;
import javax.imageio.ImageIO;

public class Base64ToImage {

    public static void main(String[] args) {
        // Exemple de Base64
        String base64String = "iVBORw0KGgoAAAANSUhEUgA..."; // Ta chaîne Base64 ici
        try {
            // Convertir le Base64 en image
            BufferedImage image = decodeBase64ToImage(base64String);

            // Redimensionner l'image à 130x130 pixels
            BufferedImage resizedImage = resizeImage(image, 130, 130);

            // Enregistrer l'image redimensionnée sous forme de fichier
            saveImage(resizedImage, "output.png", "png");

            System.out.println("L'image a été convertie et redimensionnée avec succès !");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // Méthode pour décoder la chaîne Base64 en BufferedImage
    public static BufferedImage decodeBase64ToImage(String base64String) throws IOException {
        byte[] imageBytes = Base64.getDecoder().decode(base64String);
        ByteArrayInputStream bis = new ByteArrayInputStream(imageBytes);
        return ImageIO.read(bis);
    }

    // Méthode pour redimensionner l'image
    public static BufferedImage resizeImage(BufferedImage originalImage, int width, int height) {
        Image resized = originalImage.getScaledInstance(width, height, Image.SCALE_SMOOTH);
        BufferedImage resizedImage = new BufferedImage(width, height, originalImage.getType());

        Graphics2D g2d = resizedImage.createGraphics();
        g2d.drawImage(resized, 0, 0, null);
        g2d.dispose();

        return resizedImage;
    }

    // Méthode pour enregistrer l'image redimensionnée dans un fichier
    public static void saveImage(BufferedImage image, String filePath, String format) throws IOException {
        File outputFile = new File(filePath);
        ImageIO.write(image, format, outputFile);
    }
}

@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/uploads/**")
                .addResourceLocations("file:/path/to/uploads/");
    }
}
app.prop

spring.web.resources.static-locations=file:./uploads/


import org.springframework.core.io.Resource;
import org.springframework.core.io.UrlResource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.nio.file.Path;
import java.nio.file.Paths;

@RestController
public class FileController {

    private final String uploadDir = "uploads/";

    @GetMapping("/files/{filename}")
    public ResponseEntity<Resource> serveFile(@PathVariable String filename) {
        try {
            Path file = Paths.get(uploadDir).resolve(filename);
            Resource resource = new UrlResource(file.toUri());

            if (resource.exists() || resource.isReadable()) {
                return ResponseEntity.ok()
                        .header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + filename + "\"")
                        .body(resource);
            } else {
                return ResponseEntity.notFound().build();
            }
        } catch (Exception e) {
            return ResponseEntity.badRequest().build();
        }
    }
}


