728x90
์ ๋ก๋ ์ดํ๋ก ๋ค์ด๋ก๋ ๊ธฐ๋ฅ์ ์ ๋ฆฌํด๋ณด์๋ค.
xml
<select id="selectFileList" resultType="CommonFileVO" parameterType="CommonFileVO">
SELECT file_no,
board_no,
original_file_name,
stored_file_name,
file_size,
create_date,
create_user,
del_y_n
FROM commonfiles
WHERE board_no = #{boardNo}
</select>
mapper
List<CommonFileVO> selectFileList(String boardNo);
service
public List<CommonFileVO> selectFileList(String boardNo) {
return boardMapper.selectFileList(boardNo);
}
jsp
<c:forEach items="${infofileList}" var="map">
<a href="${pageContext.request.contextPath}/main/board/fileDown.do?storedFileName=${map.storedFileName}&originalFileName=${map.originalFileName}">${map.originalFileName}</a>
</c:forEach>
controller
@RequestMapping(value="/fileDown.do") //๊ธ ๋ฆฌ์คํธ ํ์ด์ง
public void fileDown(@RequestParam("originalFileName")String originalFileName, @RequestParam("storedFileName")String storedFileName, HttpServletRequest request, HttpServletResponse response) throws Exception {
CommonFileVO fileVO = new CommonFileVO();
fileVO.setOriginalFileName(originalFileName);
fileVO.setStoredFileName(storedFileName);
fileutils.fileDownload(fileVO, request, response);
}
fileUtils
//ํ์ผ ๋ค์ด๋ก๋
public static void fileDownload (CommonFileVO fileVO, HttpServletRequest request, HttpServletResponse response) throws Exception {
// globals.properties
File file = new File(EgovProperties.getProperty("Globals.filePath"), fileVO.getStoredFileName());
BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));
//User-Agent : ์ด๋ค ์ด์์ฒด์ ๋ก ์ด๋ค ๋ธ๋ผ์ฐ์ ๋ฅผ ์๋ฒ( ํํ์ด์ง )์ ์ ๊ทผํ๋์ง ํ์ธํจ
String header = request.getHeader("User-Agent");
String fileName;
if ((header.contains("MSIE")) || (header.contains("Trident")) || (header.contains("Edge"))) {
//์ธํฐ๋ท ์ต์คํ๋ก๋ฌ 10์ดํ ๋ฒ์ , 11๋ฒ์ , ์ฃ์ง์์ ์ธ์ฝ๋ฉ
fileName = URLEncoder.encode(fileVO.getOriginalFileName(), "UTF-8");
} else {
//๋๋จธ์ง ๋ธ๋ผ์ฐ์ ์์ ์ธ์ฝ๋ฉ
fileName = new String(fileVO.getOriginalFileName().getBytes("UTF-8"), "iso-8859-1");
}
//ํ์์ ๋ชจ๋ฅด๋ ํ์ผ์ฒจ๋ถ์ฉ contentType
response.setContentType("application/octet-stream");
//๋ค์ด๋ก๋์ ๋ค์ด๋ก๋๋ ํ์ผ์ด๋ฆ
response.setHeader("Content-Disposition", "attachment; filename=\""+ fileName + "\"");
//ํ์ผ๋ณต์ฌ
FileCopyUtils.copy(in, response.getOutputStream());
in.close();
response.getOutputStream().flush();
response.getOutputStream().close();
}
globals.properties
# ํ์ผ ์
๋ก๋ ๊ฒฝ๋ก(๊ฒฝ๋ก ์ค์ ์ ๋ฐ๋์ ์ ๋๊ฒฝ๋ก๋ฅผ ์ฌ์ฉํด์ผํจ, ๊ฒฝ๋ก ๋ค์ /๋ฅผ ๋ถ์ฌ ์ฃผ์ด์ผํจ.)
Globals.filePath = C:/file/upload/
'Spring' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
@RequestParam ๊ฐ์ด Null / ์์ธ ์ฒ๋ฆฌ (required=false) (0) | 2022.02.16 |
---|---|
[Spring] WebSocket sockJS ์ค์๊ฐ ์๋ฆผ ๊ตฌํํ๊ธฐ (3) (1) | 2022.01.10 |
์ปจํธ๋กค๋ฌ ์๋๊ณณ์์ session ๊ฐ ์ป๊ธฐ (0) | 2021.11.28 |
[JAVA] sever ip ์ป๋ ๋ฐฉ๋ฒ InetAddress.getLocalHost() (0) | 2021.11.28 |
[Spring] WebSocket sockJS Q&A ์ค์๊ฐ ์๋ฆผ ๊ตฌํํ๊ธฐ (2) (3) | 2021.11.12 |