ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

728x90

 

๐ŸŠ ์ด๋ฏธ์ง€ ์ €์žฅ

ํ”„๋กœํ•„ ์ด๋ฏธ์ง€๋ฅผ ์ €์žฅํ•˜๋Š” ํ•จ์ˆ˜๋กœ ์‚ฌ์šฉ์ž ์•„์ด๋””, ์ด๋ฏธ์ง€ ํŒŒ์ผ์„ ์ธ์ž๋กœ ๋ฐ›์•„ ์ด๋ฅผ ๋กœ์ปฌ์— ์ €์žฅํ•œ๋‹ค. 

 

// ImageService.java
public String saveProfileImage(Long uid, MultipartFile imageFile) throws Exception {
    String imagePath = null;
    String absolutePath = new File("").getAbsolutePath() + "\\";
    String path = "images/profile";
    File file = new File(path);
    if (!file.exists()) {
        file.mkdirs();
    }

    if (!imageFile.isEmpty()) {
        String contentType = imageFile.getContentType();
        String originalFileExtension;
        if (ObjectUtils.isEmpty(contentType)) {
            throw new Exception("์ด๋ฏธ์ง€ ํŒŒ์ผ์€ jpg, png ๋งŒ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.");
        } else {
            if (contentType.contains("image/jpeg")) {
                originalFileExtension = ".jpg";
            } else if (contentType.contains("image/png")) {
                originalFileExtension = ".png";
            } else {
                throw new Exception("์ด๋ฏธ์ง€ ํŒŒ์ผ์€ jpg, png ๋งŒ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.");
            }
        }
        imagePath = path + "/" + uid + originalFileExtension;
        file = new File(absolutePath + imagePath);
        imageFile.transferTo(file);
    }
    else {
        throw new Exception("์ด๋ฏธ์ง€ ํŒŒ์ผ์ด ๋น„์–ด์žˆ์Šต๋‹ˆ๋‹ค.");
    }

    return imagePath;
}

 

๐Ÿ“Œ ํ”„๋กœํ•„ ๊ด€๋ฆฌ REST API

[ํ”„๋กœ์ ํŠธ/mnm] Spring - ํ”„๋กœํ•„ ๊ด€๋ฆฌ REST API ๊ตฌํ˜„ (tistory.com)

 

[ํ”„๋กœ์ ํŠธ/mnm] Spring - ํ”„๋กœํ•„ ๊ด€๋ฆฌ REST API ๊ตฌํ˜„

๐ŸŠ ํ”„๋กœํ•„ ๋“ฑ๋ก { "response": "success", "message": "ํ”„๋กœํ•„ ๋“ฑ๋ก์„ ์„ฑ๊ณต์ ์œผ๋กœ ์™„๋ฃŒํ–ˆ์Šต๋‹ˆ๋‹ค.", "data": { "id": 3, "user": { "id": 2, "email": "2@test.com", "password": "{bcrypt}$2a$10$eXYG4wE1hESNMN..

programmer-ririhan.tistory.com

 

๐ŸŠ ์ด๋ฏธ์ง€ ์กฐํšŒ

๋กœ์ปฌ์˜ ์ด๋ฏธ์ง€ ๊ฒฝ๋กœ๋ฅผ ๋ฆฌํ€˜์ŠคํŠธ ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ ์ „์†กํ•˜๋ฉด ํ•ด๋‹น ์ด๋ฏธ์ง€๋ฅผ ๋ฐ”์ดํŠธ ์–ด๋ ˆ์ด๋กœ ๋ณ€ํ™˜ํ•œ ๊ฐ’์„ ์‘๋‹ตํ•œ๋‹ค. 

 

-> ๋ฐ”์ดํŠธ ์–ด๋ ˆ์ด๋กœ ๋ณ€ํ™˜๋œ ๊ฐ’์ด ํ”„๋ก ํŠธ์—์„œ ์ž˜ ๋ณด์—ฌ์งˆ ์ง€ ๋ชจ๋ฅด๊ฒ ๋‹ค. ํ…Œ์ŠคํŠธ ํ•ด๋ด์•ผํ•˜๋Š”๋Ž…...

 

// ImageService.java
public byte[] getImage(String imagePath) throws Exception {
    System.out.println(imagePath);
    FileInputStream inputStream = null;
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();

    String absolutePath = new File("").getAbsolutePath() + "\\";
    try {
        inputStream = new FileInputStream(absolutePath + imagePath);
    }
    catch (FileNotFoundException e) {
        throw new Exception("ํ•ด๋‹น ํŒŒ์ผ์„ ์ฐพ์„ ์ˆ˜ ์—†์Šต๋‹ˆ๋‹ค.");
    }

    int readCount = 0;
    byte[] buffer = new byte[1024];
    byte[] fileArray = null;

    try {
        while((readCount = inputStream.read(buffer)) != -1){
            outputStream.write(buffer, 0, readCount);
        }
        fileArray = outputStream.toByteArray();
        inputStream.close();
        outputStream.close();

    }
    catch (IOException e) {
        throw new Exception("ํŒŒ์ผ์„ ๋ณ€ํ™˜ํ•˜๋Š”๋ฐ ๋ฌธ์ œ๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค.");
    }

    return fileArray;
}
// ImageController.java
@GetMapping("/image")
public Response getImage(@RequestParam("image_path") String imagePath) {
    Response response = new Response();

    try {
        response.setResponse("success");
        response.setMessage("์ด๋ฏธ์ง€ ์กฐํšŒ๋ฅผ ์„ฑ๊ณต์ ์œผ๋กœ ์™„๋ฃŒํ–ˆ์Šต๋‹ˆ๋‹ค.");
        response.setData(imageService.getImage(imagePath));
    }
    catch (Exception e) {
        response.setResponse("failed");
        response.setMessage("์ด๋ฏธ์ง€ ์กฐํšŒ๋ฅผ ํ•˜๋Š” ๋„์ค‘ ์˜ค๋ฅ˜๊ฐ€ ๋ฐœ์ƒํ–ˆ์Šต๋‹ˆ๋‹ค.");
        response.setData(e.toString());
    }

    return response;
}

 


Ref.

Spring - local image ๊ฐ€์ ธ์˜ค๊ธฐ (tistory.com)

Spring boot image๋ฅผ ๋“ฑ๋กํ•˜๊ณ  ์ด๋ฏธ์ง€๋ฅผ ๋ถˆ๋Ÿฌ์˜ค๋Š” ๋ฐฉ๋ฒ• (tistory.com)

 

728x90
๋Œ“๊ธ€
๊ณต์ง€์‚ฌํ•ญ
์ตœ๊ทผ์— ์˜ฌ๋ผ์˜จ ๊ธ€