일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
- 에러페이지
- layout
- 클래스
- 배열
- Eclips
- 어노테이션
- AWT
- Android
- 예외처리
- 기본
- 메소드
- 오버로딩
- Java
- paint
- OGNL
- HTML
- Spring
- 안드로이드
- mybatis
- struts2
- Graphic
- JavaScript
- JSP
- 전화걸기
- oracle
- 국제화
- 이클립스
- 생성자
- Menu
- 메서드
- Today
- Total
note
파일 다운로드 본문
현재의 폴더 위치를 얻은후에 폴더/파일명에 접근합니다
파일명이 계속 달라져야 하는 경우 jsp에서 submit 하면 되겠습니다
String filename = "파일명"; String path = "/"; ServletContext context = getServletContext(); String absFolder = context.getRealPath(path); System.out.println(absFolder); String filePath = absFolder+"/폴더/"; InputStream in = null; OutputStream os = null; File file = null; File viewFile = null; boolean skip = false; String client = ""; try { String fname1 = ""; String fname2 = ""; String fname = ""; fname = filename; fname1 = new String(fname.getBytes("8859_1"), "utf-8"); try { file = new File(filePath, fname); viewFile = new File(filePath, fname1); //out.print("file : " + file); in = new FileInputStream(file); } catch (FileNotFoundException fe) { skip = true; }//catch fname2 = new String(fname1.getBytes("utf-8"), "8859_1"); response.reset(); client = request.getHeader("User-Agent"); response.setContentType("application/x-msdownload;"); response.setHeader("Content-Description", "JSP Generated Data"); if (!skip) { if (client.indexOf("MSIE 5.5") != -1) { response.setHeader("Content-Type", "doesn/matter; charset=utf-8"); response.setHeader("Content-Disposition", "filename=" + new String(fname.getBytes("utf-8"), "8859_1")); } else { response.setHeader("Content-Type", "application/octet-stream; charset=utf-8"); response.setHeader( "Content-Disposition", "attachment; filename=" + new String(fname.getBytes("utf-8"), "8859_1")); }//else response.setHeader("Content-Transfer-Encoding", "binary;"); response.setHeader("Content-Length", "" + file.length()); response.setHeader("Pragma", "no-cache;"); response.setHeader("Expires", "-1;"); os = response.getOutputStream(); byte b[] = new byte[4096]; int leng = 0; while ((leng = in.read(b)) > 0) { os.write(b, 0, leng); }//while } else { //out.println("<script language='javascript'>"); //out.println("alert('File Downloading Fail !!');"); //out.println("</script>"); return; }//else } catch (Exception e) { System.out.println(e); } finally { if (in != null) in.close(); if (os != null) os.close(); }//finally
'JSP > 기본' 카테고리의 다른 글
절대 경로 구하기(서버 경로) (0) | 2012.06.05 |
---|---|
xml Url 파싱 (0) | 2012.06.04 |
JAVA 임시 비밀번호 생성 (0) | 2012.05.07 |
위도 경도 거리 계산 (2) | 2012.04.17 |
이미지 픽셀단위로 비율에 맞게 리사이징 하기 / 썸네일 (0) | 2012.03.16 |