web developer๐Ÿ‘ฉ๐Ÿป‍๐Ÿ’ป

[File] ํ”„๋กœ์ ํŠธ ์™ธ๋ถ€์— ์ด๋ฏธ์ง€ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ ๋ณธ๋ฌธ

Spring

[File] ํ”„๋กœ์ ํŠธ ์™ธ๋ถ€์— ์ด๋ฏธ์ง€ ๋ถˆ๋Ÿฌ์˜ค๊ธฐ

natrue 2021. 4. 22. 11:31
728x90

 

ํ”„๋กœ์ ํŠธ ๋‚ด๋ถ€์— ์žˆ๋Š” ํด๋”๋กœ ์ ‘๊ทผํ•˜์—ฌ ์ด๋ฏธ์ง€ ํŒŒ์ผ์„ ๊ฐ€์ ธ์˜ฌ ๊ฒฝ์šฐ ์ฃผ์˜ํ•  ์ 

<img src="/images/ํด๋”๋ช…/img์ด๋ฆ„.jpg'" style="width:400px;height:400px;vertical-align:middle;"/>

 

์—ฌ๋Ÿฌ ์ด๋ฏธ์ง€(ํŒŒ์ผ)๋ฅผ ํ”„๋กœ์ ํŠธ ๋‚ด๋ถ€์— ์ถ”๊ฐ€ ํ›„ ํ”„๋กœ์ ํŠธ๋ฅผ ๋นŒ๋“œํ•˜๊ฒŒ ๋˜๋ฉด ์šฉ๋Ÿ‰์ด ์ปค ๋นŒ๋“œ ์‹œ๊ฐ„์ด ๋Š๋ ค์ง€๊ณ ,

Git์ด๋‚˜ SVN์— push์— ์ œํ•œ์ด ์žˆ์„ ์ˆ˜ ์žˆ๋‹ค. 

 

๊ทธ๋ž˜์„œ ์ด ๋ฐฉ๋ฒ•๋ณด๋‹จ ํ”„๋กœ์ ํŠธ ์™ธ๋ถ€์— ์žˆ๋Š” ํด๋”๋กœ ์ ‘๊ทผํ•˜์—ฌ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ ์„ ์ถ”์ฒœํ•œ๋‹ค. 

 

*  ํ”„๋กœ์ ํŠธ ์™ธ๋ถ€์— ์žˆ๋Š” ํด๋”๋กœ ์ ‘๊ทผํ•˜์—ฌ ์ด๋ฏธ์ง€ ํŒŒ์ผ ๊ฐ€์ ธ์˜ค๋Š” ๋ฐฉ๋ฒ• * 

Globals.dir=/home/WEB_FILE/FILES/

globals.properties๋ฅผ ํ†ตํ•ด์„œ ํ”„๋กœ์ ํŠธ ๋ฐ–์— ์žˆ๋Š” ๊ฒฝ๋กœ๋ฅผ ์„ค์ • 

/home/WEB_FILE/FILES/์— ์ด๋ฏธ์ง€๋“ค์ด ์žˆ์–ด์•ผ ํ•œ๋‹ค. 

 

js 

<img src="/common/getImg.do?fileNM='+item.fileNM+'"style="width:400px;height:400px;vertical-align:middle;"/>

 

 

controller 

@RequestMapping(value="/common/getImg.do" , method=RequestMethod.GET)
  public void getImg( 
    @RequestParam(value="fileNM") String fileNM,
    HttpServletResponse response) throws Exception{

    String DIR = globalProperties.getProperty("Globals.dir");
    String filePath = DIR+fileNM;

  getImage(filePath,response);
}

์™ธ๋ถ€ ์„ค์ • ํŒŒ์ผ(globalProperties)์—์„œ ์ง€์ •ํ•œ key๊ฐ’(Globals.mgeoDir)์„ ๋ถˆ๋Ÿฌ์˜ค๋ฉด DIR ๋ณ€์ˆ˜์— ํ•ด๋‹น ๊ฒฝ๋กœ ๊ฐ’์ด ๋ฆฌํ„ด๋œ๋‹ค.

public void getImage(String filePath, HttpServletResponse response) throws Exception{
	
	File file = new File(filePath);
	if(!file.isFile()){
		response.setContentType("text/html; charset=UTF-8");
		PrintWriter out = response.getWriter();
		out.write("<script type='text/javascript'>alert('์กฐํšŒ๋œ ์ •๋ณด๊ฐ€ ์—†์Šต๋‹ˆ๋‹ค.'); self.close();</script>");
		out.flush();
		return;
	}
	
	FileInputStream fis = null;
	new FileInputStream(file);
	
	BufferedInputStream in = null;
	ByteArrayOutputStream bStream = null;
	try {
		fis = new FileInputStream(file);
		in = new BufferedInputStream(fis);
		bStream = new ByteArrayOutputStream();
		int imgByte;
		while ((imgByte = in.read()) != -1) {
			bStream.write(imgByte);
		}

		String type = "";
		String ext = FilenameUtils.getExtension(file.getName());
		if (ext != null && !"".equals(ext)) {
			if ("jpg".equals(ext.toLowerCase())) {
				type = "image/jpeg";
			} else {
				type = "image/" + ext.toLowerCase();
			}

		} else {
			LOGGER.debug("Image fileType is null.");
		}

		response.setHeader("Content-Type", type);
		response.setContentLength(bStream.size());

		bStream.writeTo(response.getOutputStream());

		response.getOutputStream().flush();
		response.getOutputStream().close();

	} catch (Exception e) {
		LOGGER.debug("{}", e);
	} finally {
		if (bStream != null) {
			try {
				bStream.close();
			} catch (Exception est) {
				LOGGER.debug("IGNORED: {}", est.getMessage());
			}
		}
		if (in != null) {
			try {
				in.close();
			} catch (Exception ei) {
				LOGGER.debug("IGNORED: {}", ei.getMessage());
			}
		}
		if (fis != null) {
			try {
				fis.close();
			} catch (Exception efis) {
				LOGGER.debug("IGNORED: {}", efis.getMessage());
			}
		}
	}
}

 

 

test