관리 메뉴

공부한것들을 정리하는 블로그 입니다.

2. 파일 다운로드 기능 본문

(2019) 사이드 프로젝트/AdminMngSys(관리자페이지-MVC1,MVC2)

2. 파일 다운로드 기능

호 두 2019. 8. 27. 15:55
반응형

 

이번 실습에서는 파일 다운로드 기능을 구현해보도록 하겠습니다.

 

프로젝트 경로

 

 

file_download.jsp

<%@ page language="java" pageEncoding="EUC-KR"
	contentType="text/html; charset=EUC-KR"%>
<%@page import="java.io.File"%>
<%@page import="java.io.BufferedInputStream"%>
<%@page import="java.io.OutputStream"%>
<%@page import="java.io.FileInputStream"%>
<%
	//String imgPath = (String) request.getAttribute("imgPath");
	
	System.out.println("자신을 호출한 페이지 : " + request.getHeader("REFERER"));
	

/* 	String pathA = "C:/Users/TY/Desktop/workspace/workspace_20190611/BoardWeb/src/main/webapp/uploadFile/"; 
	String pathB = "sample.html";
	String imgPath = pathA + pathB; */
	
 	//String parent_url = request.getHeader("REFERER"); 
	//String filename = "";
	
	/* String[] arrTest2 = parent_url.split("\\/");
    for(int i=0; i<arrTest2.length; i++){
        
    	if("type=html".equals(arrTest2[i])) {
    		
    		filename = "sample.html";
	    	//log
	    	System.out.println(arrTest2[i]);
    	} else if("type=css".equals(arrTest2[i])) {
	    	
    		filename = "sample.css";
    		//log
	    	System.out.println(arrTest2[i]);
    		
    	}
    } */
	String type = request.getParameter("type");
    String filename = "";
        
   	if("html".equals(type)) {
   		filename = "sample.html";
   	} else if("css".equals(type)) {
   		filename = "sample.css";
   	}
	//log
   	System.out.println(type);
    
    String path = "C:/Users/TY/Desktop/workspace/workspace_20190611/BoardWeb/src/main/webapp/700_FileMngSys/uploadFile/";
	String imgPath = path + filename;
	
	// 이미지 파일 정보 없을때 에러시 메시지 출력
	if (imgPath.length() == 0) {
		out.print("DB 조회 실패");
		return;
	}

	// 파일 다운로드
	try {
		// 파일 존재 확인
		File file = new File(imgPath);
		if (!file.exists()) {
			throw new Exception();
		}

		System.out.println(file);
		
		// 파일명 변환
		int index = imgPath.lastIndexOf("/") + 1;
		String fileName = imgPath.substring(index);
		String convertString = new String(fileName.getBytes("euc-kr"), "8859_1");
		fileName = convertString;

		// response header setting
		response.setContentType("application/unknown");
		response.setHeader("Content-disposition", "attachment;filename=" + fileName);

		int read;
		byte readByte[] = new byte[4096];

		BufferedInputStream fin = new BufferedInputStream(new FileInputStream(file));
		OutputStream outs = response.getOutputStream();

		while ((read = fin.read(readByte, 0, 4096)) != -1) {
			outs.write(readByte, 0, read);
		}

		outs.flush();
		outs.close();
		fin.close();

		out.clear();
		out = pageContext.pushBody();

	} catch (Exception e) {
		// 에러 발생시 처리
		out.print("에러발생");
	}
%>

 

 

admin_WebSourcePage.jsp

 

 

fileMngSys_admin.js

 

 

파일첨부

https://drsggg.tistory.com/303

 

[토이 프로젝트/(2019) Admin FileManagementSystem] 파일첨부1

공부한것들을 정리하는 블로그 입니다. [토이 프로젝트/(2019) Admin FileManagementSystem] 파일첨부1 본문 비공개용/자료실 [토이 프로젝트/(2019) Admin FileManagementSystem] 파일첨부1 호두 호 두 2019.08.27 14:57 Prev 1 2 3 4 5 ··· 247 Next

drsggg.tistory.com

 

반응형
Comments