공부한것들을 정리하는 블로그 입니다.
2. 파일 다운로드 기능 본문
반응형
이번 실습에서는 파일 다운로드 기능을 구현해보도록 하겠습니다.
프로젝트 경로
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
반응형
'(2019) 사이드 프로젝트 > AdminMngSys(관리자페이지-MVC1,MVC2)' 카테고리의 다른 글
6. JavaScript 및 Java 엑셀 다운로드 : ie chrome 브라우저별로 구현 (2) | 2019.11.20 |
---|---|
5. 버튼 클릭으로 특정경로의 파일 삭제하는 기능 : 프론트엔드, 동적으로 구현(Dynamic) (0) | 2019.08.27 |
4. 마우스 우클릭,드래그를 차단하는 기능 : javascript, jQuery (2) | 2019.08.27 |
3. 특정 경로에 있는 파일명(file list) 읽어오는 기능 + Ajax로 동적으로 이미지 로딩(Dynamic) (0) | 2019.08.27 |
1-2. 업로드 된 파일을 사용자에게 출력하는 기능 (0) | 2019.08.27 |
1-1. 파일 업로드 기능 : 구현 및 테스트 (0) | 2019.08.27 |
1. 파일 업로드 기능 : 환경설정 (0) | 2019.08.27 |
0. 프로젝트 소개 (0) | 2019.08.27 |
Comments