-개발환경-
IDE : Eclipse IDE for Enterprise Java Developers ver-2020-06 (4.16.0) Tomcat : Tomcat v8.0 Server JQuery : jquery-3.6.0.min.js |
-index.jsp-
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="uploadAction.jsp" method="post" enctype="multipart/form-data">
파일 : <input type="file" name="file"/></br>
<input type="submit" value="upload"/>
</form>
<a href="fileDownload.jsp">파일 다운로드 페이지</a>
</body>
</html>
|
cs |
-fileDownload.jsp-
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
|
<%@page import="java.net.URLEncoder"%>
<%@page import="java.io.File"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
String uploadDirectory = application.getRealPath("./upload/");
String[] files= new File(uploadDirectory).list();
for(String file : files){
%>
<a href="<%=request.getContextPath()%>/downloadAction?file=<%=URLEncoder.encode(file,"UTF-8")%>"><%=file%></a></br>
<%
}
%>
<a href="index.jsp">돌아가기</a>
</body>
</html>
|
cs |
-Description
15 : list() : 지정된 디렉토리(폴더)에 저장된 파일이름목록을 얻어온다.
-downloadAction.java(서블렛파일)-
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
|
package com.koreait.fileupload;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/downloadAction")
public class downloadAction extends HttpServlet {
private static final long serialVersionUID = 1L;
public downloadAction() {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
actionDo(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
actionDo(request, response);
}
protected void actionDo(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
String fileName = request.getParameter("file");
String uploadDirectory = this.getServletContext().getRealPath("./upload/")+fileName;
File file = new File(uploadDirectory);
String mimeType = getServletContext().getMimeType(file.toString());
if(mimeType == null) {
response.setContentType("application/octet-stream");
}
String downloadName = null;
if (request.getHeader("user-agent").indexOf("MSIE")==-1){
downloadName = new String(fileName.getBytes("UTF-8"),"8859_1");
}else {
downloadName = new String(fileName.getBytes("EUC-KR"),"8859_1");
}
response.setHeader("Content-Disposition", "attachment;filename=\""+downloadName+"\";");
FileInputStream fileInputStream= new FileInputStream(file);
ServletOutputStream servletOutputStream=response.getOutputStream();
byte[] b= new byte[1024];
int data =0;
while((data = (fileInputStream.read(b,0,b.length))) !=-1) {
servletOutputStream.write(b,0,data);
}
servletOutputStream.flush();
servletOutputStream.close();
fileInputStream.close();
}
}
|
cs |
-Description
31 : //다운로드할 파일의 이름을 받는다.
33 : //다운로드 할 파일이 저장된 실제물리적 디렉토리 경로를 얻어온다.
//String uploadDirectory = this.getServletContext().getRealPath("./upload/");
//다운로드할 파일이 저장된 실제 물리적 디렉토리 경로와 다운로드할
//파일이름을 연결한다.
35 : //다운로드 할 파일 객체를 만든다.
37 : //MIME = > Multipurpose Internet Mail Extension의 약자로
//파일 변환을 의미한다.
//getMimeType() : 인수로 지정된 파일의 MIME 타입을 얻어온다.
39 : //MIME 타입을 얻어오지 못했다면 파일을 전송하는 것을 사용자에게 알려주기 위해서
// response객체를 사용해서 file 형태의 데이터를 전송한다는 것을
//"application/octet-stream"을 사용해 알려준다.
43 : //접속한 브라우저에 따라 다운로드할 파일의 헤더 정보를 다르게
//설정한다.
44: //접속한 브라우저가 뭔지 알아내는 헤더
46: //IE를 제외한 나머지 브라우저들
51 : //attachment 뒤의 filename은 반드시 큼따옴표를 사용해서 묶어줘야한다.
53,54 : //파일 전송에 사용할 객체를 선언한다.
56 : //파일을 1024바이트(1KB) 단위로 전송한다.
59~61 : //디스크에서 읽어들인 전송할 파일의 데이터가 있는 동안 반복하며
//브라우저로 전송한다
62~64 : //flush() : servletOutputStream객체의 출력버퍼에 남아있는 모든 데이터를
//전송하고 전송에사용한 객체를 닫는다.
-uploadAction.jsp-
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
<%@page import="com.oreilly.servlet.multipart.DefaultFileRenamePolicy"%>
<%@page import="com.oreilly.servlet.MultipartRequest"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
request.setCharacterEncoding("UTF-8");
MultipartRequest mr = new MultipartRequest(
request,
application.getRealPath("./upload"),
5*1024*1024,
"UTF-8",
new DefaultFileRenamePolicy()
);
String fileName = mr.getOriginalFileName("file");
out.println("fileName : " + fileName+ "<br/>");
String fileRealName = mr.getFilesystemName("file");
out.println("fileRealName : " + fileRealName+ "<br/>");
if( fileName.endsWith(".jar") || fileName.endsWith(".zip") ){
out.println("업로드한 파일 이름: " +fileName+"</br>");
out.println("실제 업로드된 파일 이름: " +fileRealName+"</br>");
}else{
out.println("<script>");
out.println("alert('"+fileName+"은 업로드 할수 없는 형식의 파일입니다.\\njar,zip파일만 업로드가 가능합니다"+"')");
out.println("</script>");
File file = new File(application.getRealPath("./upload/")+fileName);
file.delete();
}
%>
<a href="index.jsp">돌아가기</a>
</body>
</html>
|
cs |
-Description
24 : //단일 파일 업로드
//getOriginalFileName() : 사용자가 업로드 한 파일 이름을 얻어온다.
26 : //getFilesystemName() : 업로드되서 실제 디스크에 저장된 파일 이름을 얻어온다.
29 : //업로드제한
//startsWith() : 인수로 지정된 문자열로 문자열이 시작하면 true, 그렇지 않으면
//false를 리턴한다.
//endsWith() : :인수로 지정된 문자열로 문자열이 끝나면 true, 그렇지 않으면
//false를 리턴한다.
// jar파일과 zip파일만 업로드 할 수 있도록 한다.
36 : //업로드된 파일을 삭제한다.
37 : //delete()메소드로 파일을 삭제한다.
-요약-
JAVASCRIPT
-alert-
alert내 개행을 원한다면 \\n을 사용한다
-마침글-
단순문법공부는 boring하다..
인용
'21년이전 > 국비-JSP' 카테고리의 다른 글
JSP-파일업로드 (0) | 2021.04.14 |
---|---|
JSP+Oracle - DBCP , JDBC 연결 (0) | 2021.03.31 |
Oracle-명령어정리 (0) | 2021.03.31 |
Jquery- AJAX 이용 (0) | 2021.03.30 |
Jquery-table (0) | 2021.03.30 |