๊ฐœ๋ฐœ๊ธฐ/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();
		}
	}

}