๊ฐ๋ฐ๊ธฐ/JAVA
[Java] ์ด๋ฏธ์ง ์ฒจ๋ถ ์ ์ฅ ๋ก์ง์์ ๋ฌผ๋ฆฌํ์ผ ๊ฒฝ๋ก ์ด๋ฏธ์ง ๋ถ๋ฌ์ค๊ธฐ
๋ ๋
2025. 2. 16. 10:45
์ฌ๋ฌ ๋ฐฉ๋ฒ์ด ์๊ฒ ์ง๋ง ์ด๊ฑด ์์ค๊ฐ ๋ฐฐํฌ๋ ๊ฒฝ๋ก ์ธ์ ํน์ ๊ฒฝ๋ก๋ก ์ง์ ํ์ฌ
๋ง์ดํธํ๊ฑฐ๋ ๋ฐ๋ก ๊ด๋ฆฌํ๋ ๋ฐฉ์์ผ ๋ ์ฌ์ฉ
๋ฐฐํฌ๋ war๋ฅผ ์ญ์ ํ๊ฑฐ๋ ์์ ์ฌ ๋ฐฐํฌ์ ์ํฅ์์
์ค์ ํ์ผ์ ๊ฒฝ๋ก๋ฅผ ์๋์๊ฐ์ด ์ค์ ํจ
์ค์ ํ์ผ์ด ์ ๋ก๋๋ ๊ฒฝ๋ก
DB์๋ ๋ฌผ๋ฆฌ๊ฒฝ๋ก๋ฅผ ์ด๋ฐ์์ผ๋ก ์ ์ฅ
์ด๋ฏธ์ง๋ฅผ ๋ถ๋ฌ์ฌ ๋ ์ฌ์ฉํ ์์ค
/**
* ์ด๋ฏธ์ง ํ์ผ์ ์ถ๋ ฅํ๋ค.
* @param attachFileVO AttachFileVO
* @param response HttpServletResponse
*
* @throws Exception
*/
@RequestMapping(value="/cmm/file/imgServlet.do")
public void imgServlet(AttachFileVO attachFileVO, HttpServletResponse response) throws Exception{
Map<String,Object> fileMap = attchFileService.selectFileOne(attachFileVO);
String savePath = (String)fileMap.get("fileStreCours");
String saveName = (String)fileMap.get("saveFileNm");
long fileSize = Long.parseLong((String)fileMap.get("fileSize"));
String fileFullPath = savePath + saveName;
String contentType = null;
String ext = (String)fileMap.get("fileExtsn");
ext = StringUtil.lowerCase(ext);
if("jpg".equals(ext) || "jpeg".equals(ext)) {
contentType = "image/jpeg";
} else if("gif".equals(ext)) {
contentType = "image/gif";
} else if("png".equals(ext)) {
contentType = "image/png";
} else if ("bmp".equals(ext)) {
contentType = "image/bmp";
} else {
contentType = "image/jpeg";
}
response.setContentType(contentType);
response.setHeader("Content-Disposition", "filename=image");
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
File file = new File(fileFullPath);
bos = new BufferedOutputStream(response.getOutputStream());
int read = 0;
byte b[] = new byte[2048];
if (fileSize > 0 && file.isFile()) {
bis = new BufferedInputStream(new FileInputStream(file));
while ((read = bis.read(b)) != -1) {
bos.write(b, 0, read);
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if(bis != null){
bis.close();
}
if(bos != null){
bos.close();
}
}
}