[개발] 프로그램 지식

[java] 이미지 미리보기 구현 방법 feat. fileZiller 파일질라에 이미지 저장하기

  • -
반응형

1. 이미지 불러오는 메서드

	@RequestMapping("/cmm/fms/getImage1.do")
	public void getImageInf1(SessionVO sessionVO, ModelMap model, @RequestParam Map<String, Object> commandMap, HttpServletResponse response) throws Exception {

		String fileStreCours = (String)commandMap.get("fileStreCours");
		String streFileNm = (String)commandMap.get("streFileNm");
		String fileExtsn = (String)commandMap.get("fileExtsn");
		
		streFileNm = streFileNm+"."+fileExtsn;
		
		File file = null;
		FileInputStream fis = null;

		BufferedInputStream in = null;
		ByteArrayOutputStream bStream = null;

		try {

			file = new File(fileStreCours, streFileNm);
			fis = new FileInputStream(file);

			in = new BufferedInputStream(fis);
			bStream = new ByteArrayOutputStream();

			int imgByte;
			while ((imgByte = in.read()) != -1) {
				bStream.write(imgByte);
			}

			String type = "";

			if (fileExtsn != null && !"".equals(fileExtsn)) {
				if ("jpg".equals((fileExtsn).toLowerCase())) {
					type = "image/jpeg";
				} else {
					type = "image/" + (fileExtsn).toLowerCase();
				}
				type = "image/" + (fileExtsn).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();

		} finally {
			EgovResourceCloseHelper.close(bStream, in, fis);
		}
	}

streFileNm = streFileNm+"."+fileExtsn; 파일 확장자까지 맞춰줘야 stream에서 오류가 발생하지 않음

 

 

 

2. 보통 서버에서 이미지를 저장할 때 파일질라를 이용하는데 폴더에 대해서 권한설정이 필요함

> 이미지 바로 전 폴더에 대해서 권한 설정 ( 해당 폴더 마우스 우클릭 )

 

 

 

 

 

3. 읽기, 쓰기, 실행 모두 체크해준 후 확인 눌러주기

 

 

 

4. 파일 잘 들어갔는지 확인하기

 

반응형
Contents

포스팅 주소를 복사했습니다

이 글이 도움이 되었다면 공감 부탁드립니다.