์ ๋ก๋๋ ์ด๋ฏธ์ง๋ฅผ ๋ธ๋ผ์ฐ์ ์์ ๋ณด๊ธฐ
โ
-WEB-INF ์๋์ ๋๋ ํ ๋ฆฌ๋ ์น๋ธ๋ผ์ฐ์ ์์ ์ ๊ทผ ๋ถ๊ฐ(WEB-INF/files/)
-์๋ฒ์ธก์์ WEB-INF ์๋์ ์ด๋ฏธ์ง๋ฅผ ์ ํด์ค ์๋ ์์
-<img src="/image/mypic.jpg">
-์์ ํ๊ทธ๊ฐ ์น๋ธ๋ผ์ฐ์ ์ ๋ก๋๋๋ฉด ์น๋ธ๋ผ์ฐ์ ๋ ํด๋น ์ด๋ฏธ์ง๋ฅผ ์๋ฒ์ ์์ฒญํ๊ฒ ๋จ
-์์ ์์ฒญ์ ์ฒ๋ฆฌํ๋ ์ปจํธ๋กค๋ฌ ๋ฉ์๋๊ฐ ์๋ค๋ฉด ์ด ๋ ๊ทธ ๋ฉ์๋๊ฐ ์คํ๋จ
-์ปจํธ๋กค๋ฌ์์๋ @ResponseBody๋ฅผ ์ฌ์ฉํ์ฌ byte[ ]์ด ๋ธ๋ผ์ฐ์ ๋ก ๋ฆฌํด๋๋๋ก ํจ
-์ด๋ฏธ์ง ๋ฐ์ดํฐ๋ฅผ ๋ฐ์ <img src='xxxx'> ํ๊ทธ๋ ์ด๋ฏธ์ง๋ฅผ ํ๋ฉด์ ํ์ํ ์ ์์
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
|
import java.io.InputStream;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import lombok.extern.slf4j.Slf4j;
@Slf4j
@Controller
@RequestMapping("/image")
public class ImageController
{
@Autowired
private ResourceLoader resourceLoader;//spring์์ ์ง์ํด์ฃผ๋ ResourceLoader : ์ฝ๋๊ฐ ์๋ ๋ค๋ฅธ ๋ชจ๋ ๊ฒ์ resource๋ผ๊ณ ํ๋ค.
@GetMapping("/test")
public String showImage()
{
return "thymeleaf/showImage";
}
@GetMapping(value="/{filename}", produces=MediaType.IMAGE_JPEG_VALUE)
@ResponseBody
public byte[] getImage(@PathVariable String filename)
{
try {
Resource resource = resourceLoader.getResource("WEB-INF/files/"+filename);
//Resource ๊ฐ์ฒด๋ ํ์ผ์ ๋ชจ๋ ์ ๋ณด(ํ์ผ ์ด๋ฆ, ์ฌ์ด์ฆ, ๊ฒฝ๋ก ๋ฑ๋ฑ)๋ฅผ ๊ฐ์ ธ์ ๋ด๋๋ค.
InputStream in = resource.getInputStream();
int len = (int)resource.getFile().length(); //ํ์ผ์ ์ ์ฒด ํฌ๊ธฐ๊ฐ longํ์ผ๋ก ๋์ค์ง๋ง ๋๋ถ๋ถ์ image size๋ int๋ก ๋์ด ์์ด์ int๋ก ๋ณํํด์ค๋ค.
log.info("ํ์ผ ํฌ๊ธฐ={}, available={}", len, in.available());
byte[] buf = new byte[len]; //ํ์ผ ์ ์ฒด๋ฅผ ๋ด์ ์ ์๋ ๋ฉ๋ชจ๋ฆฌ ๊ณต๊ฐ ํ๋ณด
in.read(buf, 0, len); //"in"์ด ์ฝ์ด์จ ํ์ผ์ ๋ฐ์ดํฐ๋ฅผ ๋น byte array "buf"์ ๋ด๋๋ค.
//์ด ๋ฒํผ์ ์ฒ์(0)๋ถํฐ len๋งํผ๊น์ง์ ๋ด๊ธธ ๋งํผ ํ์ผ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ ธ์์ ๋ด๊ฒ๋๋ค.
in.close();
return buf; //buf๋ฐํ
}catch(Exception ex) {
ex.printStackTrace();
}
return null;
}
}
|
cs |
์ด๋ฏธ์ง ํ์ผ์ ๋ฌธ์์ด์ด ์๋ byte ๋ฐฐ์ด๋ก ์ ํ๋์ด ์ ์ก๋๋ค. ๋ฐ๋ผ์ stream์ ํตํด์ ๋ฐ์ดํฐ๋ฅผ ์ ์ถ๋ ฅํ ์ ์๋ค.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
|
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>์
๋ก๋๋ ์ด๋ฏธ์ง๋ฅผ ๋ธ๋ผ์ฐ์ ๋ก ๋ณด๊ธฐ</title>
</head>
<body>
<p></p>
<h3>์
๋ก๋๋ ์ด๋ฏธ์ง๋ฅผ ๋ธ๋ผ์ฐ์ ๋ก ๋ณด๊ธฐ</h3>
<img src="/image/anjelina.jfif">
<!-- src์ ์์ฑ์ผ๋ก ์ฌ์ฉ๋ url์ ๋ธ๋ผ์ฐ์ ๊ฐ ์๋ฒ์ ๋ํ ์์ฒญ์ผ๋ก ์ฌ์ฉํ๋ฏ๋ก
ํด๋น ์์ฒญ์ ์ฒ๋ฆฌํ๋ ์ปจํธ๋กค๋ฌ ๋ฉ์๋๊ฐ ์๋ ๊ฒฝ์ฐ์๋ ์ด๋ฏธ์ง ๋ฐ์ดํฐ๋ฅผ ์๋ต์ผ๋ก ๋ฆฌํดํ๋ฉด ๋๋ค
-->
</body>
</html>
|
cs |
Thymeleaf์์ ์ด๋ฏธ์ง ๊ฒฝ๋ก๋ก ๋ธ๋ผ์ฐ์ ์ ์ด๋ฏธ์ง ๋์ฐ๋ ๋ฐฉ๋ฒ
1
|
<img th:src="@{/dageul/getprofile/}+${member.profile}" align="left" width="96px" height="117px" />
|
cs |
dageul/getprofile/ : url
${member.profile} : ํ์ผ๋ช
๋๊ธ