업로드 폼
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>멀티 파트 폼</title>
</head>
<body>
<form action="multipart_data.jsp" method="post">
text1 : <input type="text" name="text1" /><br/>
file1 : <input type="file" name="file1" /><br/>
file2 : <input type="file" name="file2" /><br/>
<input type="submit" value="전송"/>
</form>
</body>
</html>
업로드 데이터 폼(확인)
<%@page contentType="text/plain; charset=utf-8"%>
<%@page import="java.io.InputStream"%>
<%@page import="java.io.IOException"%>
<%@page trimDirectiveWhitespaces="true" %>
<%
InputStream is = null;
out.print("[");
out.print(request.getContentType());
out.print("]");
try{
is = request.getInputStream();
int data= -1;
while((data=is.read())!=-1){
out.print((char)data);
}
}finally{
if(is!=null) try{ is.close(); } catch(IOException e){}
}
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>Insert title here</title>
</head>
<body>
</body>
</html>
결과
다른 실습
MultipartServlet 생성
import 생략
@WebServlet("/MultipartServlet")
public class MultipartServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
public MultipartServlet() {
super();
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
String type = request.getContentType();
int len = request.getContentLength();
out.println("<html><head><title>파일 업로드 테스트1</title></head>");
out.println("<body");
out.println("<h3> 파일 업로드 테스트 1 </h3><hr>");
out.println("MIME Type : "+type+"<br>");
out.println("Content Length : "+len+"<br>");
out.println("파일 내용 : <br>");
ServletInputStream in = request.getInputStream();
byte buf[] = new byte[512];
int n=0;
while((n=in.read(buf,0,512))>0){
out.println(new String(buf,0,n));}
out.println("</body>");
out.println("</html>");
}
}
업로드 폼에서 action만 서블릿id와 같게 하면 됨
[결과]