개발/WEB PROGRAMMING

[ JAVA ] 파일 다운로드 FILE DOWNLOAD

itaekwon class 2020. 11. 7.
728x90

파일 다운로드 [ JAVA ]

 

일반적으로 많이 쓰는 파일 다운로드 모듈이다.

	/**
	 * 지정된 파일을 다운로드 한다.
	 * 
	 * @param request
	 * @param response
	 * @param file
	 *            다운로드할 파일
	 * 
	 * @throws ServletException
	 * @throws IOException
	 */
	public static void download(HttpServletRequest request, HttpServletResponse response, File file, String fileNm)
    	throws ServletException, IOException {
 
		String mimetype = request.getSession().getServletContext().getMimeType(file.getName());
 
		if (file == null || !file.exists() || file.length() <= 0 || file.isDirectory()) {
			throw new IOException("파일 객체가 Null 혹은 존재하지 않거나 길이가 0, 혹은 파일이 아닌 디렉토리이다.");
		}
 
		InputStream is = null;
 
		try {
			is = new FileInputStream(file);
			download(request, response, is, fileNm, file.length(), mimetype);
		} finally {
			try {
				is.close();
			} catch (Exception ex){}
		}
	}
728x90

댓글