ํฐ์คํ ๋ฆฌ ๋ทฐ
ํ๋ก์ ํธ
[ํ๋ก์ ํธ/mnm] Spring - ์ด๋ฏธ์ง ์ ์ฅ, ์กฐํ REST API ๊ตฌํ
ellie.strong 2021. 11. 17. 17:01728x90
๐ ์ด๋ฏธ์ง ์ ์ฅ
ํ๋กํ ์ด๋ฏธ์ง๋ฅผ ์ ์ฅํ๋ ํจ์๋ก ์ฌ์ฉ์ ์์ด๋, ์ด๋ฏธ์ง ํ์ผ์ ์ธ์๋ก ๋ฐ์ ์ด๋ฅผ ๋ก์ปฌ์ ์ ์ฅํ๋ค.
// 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)
๐ ์ด๋ฏธ์ง ์กฐํ
๋ก์ปฌ์ ์ด๋ฏธ์ง ๊ฒฝ๋ก๋ฅผ ๋ฆฌํ์คํธ ํ๋ผ๋ฏธํฐ๋ก ์ ์กํ๋ฉด ํด๋น ์ด๋ฏธ์ง๋ฅผ ๋ฐ์ดํธ ์ด๋ ์ด๋ก ๋ณํํ ๊ฐ์ ์๋ตํ๋ค.
-> ๋ฐ์ดํธ ์ด๋ ์ด๋ก ๋ณํ๋ ๊ฐ์ด ํ๋ก ํธ์์ ์ ๋ณด์ฌ์ง ์ง ๋ชจ๋ฅด๊ฒ ๋ค. ํ ์คํธ ํด๋ด์ผํ๋๋ ...
// 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
'ํ๋ก์ ํธ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[ํ๋ก์ ํธ/mnm] Spring - ๋งค์นญ ์ ๋ณด ๊ด๋ฆฌ REST API ๊ตฌํ (2) | 2021.11.17 |
---|---|
[ํ๋ก์ ํธ/mnm] Spring - ๊ฑฐ์ฃผ ์ ๋ณด ๊ด๋ฆฌ REST API ๊ตฌํ (0) | 2021.11.17 |
[ํ๋ก์ ํธ/mnm] Spring - ํ๋กํ ๊ด๋ฆฌ REST API ๊ตฌํ (0) | 2021.11.17 |
[ํ๋ก์ ํธ/mnm] Spring - ํ์๊ฐ์ , ๋ก๊ทธ์ธ ๊ตฌํ (JWT ์ธ์ฆ) (0) | 2021.11.14 |
[ํ๋ก์ ํธ/mnm] Spring - MySQL ์ฐ๋ (0) | 2021.11.06 |
๋๊ธ
๊ณต์ง์ฌํญ
์ต๊ทผ์ ์ฌ๋ผ์จ ๊ธ