ํ์ผ์ ์ฌ๋ ธ์ผ๋ฉด ๋ค์ด๋ก๋๋ฅผ ๋ฐ์ ์ ์๊ฒ ์ฒ๋ฆฌํด์ผํ๋น..
<a href="๊ฒฝ๋ก?ํค๊ฐ=${ํ
์ด๋ธ.ํค๊ฐ}">
${file.orgFileName}
</a>
์ผ๋จ ์๋จ์์ aํ๊ทธ์ ํ์ผ ๊ฒฝ๋ก๋ฅผ ์ค์ ํด์ฃผ๊ณ
ํน์ ํ์ผ์ ์กฐํํ๋ ๋ก์ง์ ๋ง๋ ๋ค.. (mapper..service..)
๊ทธ๋ฐ๋ค์ ์ปจํธ๋กค๋ฌ์ ๋ทฐ๋จ์ ์ค์ ํ ๊ฒฝ๋ก๋ก ์์ฒญ์ด ๋ค์ด์์ ๋ ์ฒ๋ฆฌ๋ฅผ ํด์ค์ผ ํ๋๋ฐ
ํ์ผ ์ ๋ณด๋ฅผ ์๊น ๋ง๋ ํน์ ํ์ผ์ ์กฐํํ๋ ๋ก์ง์ผ๋ก ํ์ธํ๊ณ
ํ์ผ ๊ฒฝ๋ก๋ฅผ ๊ฐ์ง๊ณ ํ์ผ์ด ์กด์ฌํ๋์ง ํ์ธํ๋ค์
์๋์ฒ๋ผ ํ์ผ ๋ค์ด๋ก๋์ ํ์ํ ์๋ตํค๋๋ฅผ ์ค์ ํด์ผ ํ๋ค.
response.setContentType("application/octet-stream");
response.setContentLength((int) file.length());
response.setHeader("Content-Disposition",
"attachment; filename=\"" + URLEncoder.encode(fileDTO.getOrgFileName(), "UTF-8") + "\"");
๊ทธ๋ฐ ๋ค์ ํ์ผ์ธํ์คํธ๋ฆผ์ผ๋ก ํ์ผ์ ์ฝ๊ณ
response.getOutputStream()์ ์ด์ฉํด ํด๋ผ์ด์ธํธ์๊ฒ ๋ฐ์ดํฐ๋ฅผ ์ ์กํ๋ค.
FileInputStream fis = new FileInputStream(file);
OutputStream os = response.getOutputStream()) {
byte[] buffer = new byte[1024];
int bytesRead;
while ((bytesRead = fis.read(buffer)) != -1) {
os.write(buffer, 0, bytesRead);
}
os.flush();
๊ทธ๋ ๋ฒํผ๋ฅผ ์ฌ์ฉํ์ฌ 1KB์ฉ ๋ฐ์ดํฐ๋ฅผ ์ฝ์ด ์คํธ๋ฆผ์ผ๋ก ์ ์กํ๋ค.
flush()๋ฅผ ํธ์ถํ๋ฉด ๋ชจ๋ ๋ฐ์ดํฐ๊ฐ ํด๋ผ์ด์ธํธ๋ก ์ ์ก๋๋ค.
์๋ฌ๊ฐ ๋ ๋ถ๋ถ์ ์์ธ์ฒ๋ฆฌ๋ฅผ ์ ํด์ฃผ์ฅ..!
'๐๏ธ ๋ฐฑ์๋ > ๐Spring' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| # JSP - iframe ์ธ๋ถ(๋ถ๋ชจ)์ ๋ณ์ ๋๊ธฐ๊ธฐ (0) | 2025.03.25 |
|---|---|
| # @ConfigurationPropertiesScan, @ConfigurationProperties (0) | 2025.03.06 |
| # ์ฐจํธ๊ฐ ๋ง์๋ ์๋ ๋น ๋ฅด๊ฒ ์ฒ๋ฆฌํ๋ ๋ฐฉ๋ฒ (0) | 2025.02.26 |
| # Spring - MyBatis ๊ตฌ์กฐ (0) | 2025.02.17 |
| # MultipartFile - ๋ค์ค ํ์ผ์ฒจ๋ถ (0) | 2025.02.13 |