728x90
★ FileTest
- Dynamic Web package 생성
■ 파일 생성
1. 업로드 폴더 생성
- webapp > "files"
2. 패키지
- "com.test.file"
3. 뷰 폴더
- webapp > WEB-INF > "veiws"
4. 라이브러리
- webapp > WEB-INF > lib
- ojdbc6.jar
- mylibrary.jar
- jstl-1.2.jar
- cos.jar
다중 파일 업로드
- com.test.file > "MultiFile.java"
> "MultiFileOk.java"
> "Download.java"
- webapp > WEB-ING > "multifile.jsp"
> "multifileok.jsp"
■ MultiFile.java : 파일 업로드 하는 Servlet
package com.test.file;
import java.io.IOException;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/multifile.do")
public class MultiFile extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//MultiFile.java
RequestDispatcher dispatcher = req.getRequestDispatcher("/WEB-INF/views/multifile.jsp");
dispatcher.forward(req, resp);
}
}
■ multifile.jsp : 실질적으로 구동되는 jsp 파일
<%@ 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>
<link rel="stylesheet" href="https://me2.do/5BvBFJ57">
<style>
#files > div {margin-bottom : 5px;}
.item {margin-bottom : 5px;}
</style>
</head>
<body>
<h1>다중 파일 업로드</h1>
<form method="POST" action="/file/multifileok.do" enctype="multipart/form-data">
<div>
<div>이름: </div>
<div><input type="text" name="name"></div>
</div>
<div id="files">
<div>파일: </div>
<div class="item"><input type="file" name="attach"></div>
<div id="list"></div>
<div><input type="button" value="+" id="btnadd"></div>
</div>
<div>
<input type="submit" value="업로드">
</div>
</form>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.4/jquery.min.js"></script>
<script>
$('#btnadd').click(()=> {
$('#list').append(
`<div class="item">
<input type="file" name="attach">
<input type="button" value="X" onclick="$(this).parent().remove()">
</div>`)
});
</script>
</body>
</html>
■ MultiFileOk.java
package com.test.file;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.oreilly.servlet.MultipartRequest;
import com.oreilly.servlet.multipart.DefaultFileRenamePolicy;
@WebServlet("/multifileok.do")
public class MultiFileOk extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//MultiFileOk.java
req.setCharacterEncoding("UTF-8");
List<String> olist = new ArrayList<String>();
List<String> flist = new ArrayList<String>();
try {
MultipartRequest multi = new MultipartRequest(
req,
req.getRealPath("/files"),
1024 * 1024 * 100,
"UTF-8",
new DefaultFileRenamePolicy()
);
System.out.println(req.getRealPath("/files"));
//<input type="file" name="attach">
//String orgfilename = multi.getOriginalFileName("attach");
//String filename = multi.getFilesystemName("attach");
//System.out.println(orgfilename);
//System.out.println(filename);
Enumeration e = multi.getFileNames();
while (e.hasMoreElements()) {
//System.out.println(e.nextElement());
String file = e.nextElement().toString();
String orgfilename = multi.getOriginalFileName(file);
String filename = multi.getFilesystemName(file);
System.out.println(orgfilename);
System.out.println(filename);
olist.add(orgfilename);
flist.add(filename);
}
} catch (Exception e) {
e.printStackTrace();
}
req.setAttribute("olist", olist);
req.setAttribute("flist", flist);
RequestDispatcher dispatcher = req.getRequestDispatcher("/WEB-INF/views/multifileok.jsp");
dispatcher.forward(req, resp);
}
}
■ multifileok.jsp
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<link rel="stylesheet" href="https://me2.do/5BvBFJ57">
<style>
</style>
</head>
<body>
<!-- multifileok.jsp -->
<h1>결과</h1>
<c:forEach var="i" begin="0" end="${flist.size()-1}">
<div>다운로드: <a href="/file/download.do?filename=${flist[i]}&orgfilename=${olist[i]}">${olist[i]}</a></div>
</c:forEach>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.4/jquery.min.js"></script>
<script>
</script>
</body>
</html>
■ 실행 결과
- 업로드 화면
- 업로드 후 화면
728x90
'Server' 카테고리의 다른 글
AJAX STEP 1 - 기초 (0) | 2023.05.12 |
---|---|
Servlet + JSP STEP 3 - 이미지 뷰어(갤러리) (0) | 2023.05.12 |
Servlet+JSP STEP 1 - File UPLOAD (0) | 2023.05.11 |
JSTL STEP 1 - JSTL 기초 셋팅 및 응용 (0) | 2023.05.11 |
JSP STEP 11 - JSP Model 2 + EL + JSTL (0) | 2023.05.11 |