본문 바로가기
Web Programing!/JAVA / JSP

[JSP] 다중업로드/다운로드

by 어설픈봉봉이 2011. 9. 20.
반응형
SMALL





fileSelectForm.jsp [다중 파일 선택]

<%@ page language="java" contentType="text/html; charset=EUC-KR" pageEncoding="EUC-KR"%>
<%@ include file="/view/color.jsp" %>

<%!
 public String getParam(HttpServletRequest request,String paramName){
  if(request.getParameter(paramName)!=null){
   return request.getParameter(paramName);
  }else{
   return "";
  }
 }
%>

<%
 request.setCharacterEncoding("euc-kr");
 int filecounter = 0;
 if(request.getParameter("addcnt")!=null){
  filecounter = Integer.parseInt(request.getParameter("addcnt"));
 }
%>

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>멀티 파일 업로드 예제</title>
<link href="style.css" rel="stylesheet" type="text/css">

<script type="text/javascript">
 function inputValue(form1,param,form2,idx){
  var paramValue = form1.elements[idx].value;
  form2.elements[idx].value = paramValue;
  return;
 }
 
 function addFile(formName){
  if(formName.addcnt.value==""){
   alert("입력할 파일 갯수를 입력하고 확인 단추를 눌러주세요.");
   formName.addcnt.focus();
   return;
  }
  formName.submit();
 }
 
 function elementCheck(formName){
  paramIndex = 1;
  for(idx=0; idx<formName.elements.length;idx++){
   if(formName.elements[idx].type=="file"){
    if(formName.elements[idx].value==""){
     var message = paramIndex+" 번째 파일정보가 없습니다.\n업로드할 파일을 선택해 주세요.";
     alert(message);
     formName.elements[idx].focus();
     return;
    }
    paramIndex++;
   }
  }
  formName.action = "fileSelectPro.jsp";
  formName.submit();
 } 
</script>
</head>
<body bgcolor="<%=bodyback_c%>">
 <div align="center">
  <h2>여러개의 파일을 업로드 하는 예제 </h2>
  <font color="#0000ff" size="2">파일갯수를 입력한 후 [확인]버튼을 눌러주세요.<br>
  입력이 완료되면 파일업로드를 위한 [업로드]버튼을 눌러주세요</font>
 </div>
 <br>
 <form name="frmName1" method="post">
  <table width="50%" border="1" align="center" cellpadding="1" cellspacing="1">
   <tr>
    <td width="15%" bgcolor="<%=value_c %>" align="center">작성자</td>
    <td><input type="text" name="user" onkeyup="inputValue(this.form,user,frmName2,0)" value="<%=getParam(request,"user")%>"></td>
   </tr>
   <tr>
    <td width="15%" bgcolor="<%=value_c %>" align="center">제목</td>
    <td><input type="text" name="title" onkeyup="inputValue(this.form,title,frmName2,1)" value="<%=getParam(request,"title")%>"></td>
   </tr>
   <tr>
    <td width="15%" bgcolor="<%=value_c %>" align="center">내용</td>
    <td width="50%" colspan="3">
     <textarea name="abstract" cols="40" onkeyup="inputValue(this.form,abstract,frmName2,2)"><%=getParam(request,"abstract") %></textarea>
    </td>
   </tr>
   <tr>
    <td colspan="4" bgcolor="<%=value_c %>" align="center">
     업로드할 파일 수 입력
     <input type="Text" name="addcnt">
     <input type="button" value="확인" onclick="addFile(this.form)">    
    </td>
   </tr>
  </table>
 </form>
 <form name="frmName2" method="post" enctype="multipart/form-data">
  <table width="50%" border="1" align="center" cellpadding="1" cellspacing="1">
   <tr>
    <td width="40%" bgcolor="<%=value_c %>" align="center">
     <input type="hidden" name="txtUser" value="<%=getParam(request,"user")%>">
     <input type="hidden" name="txtTitle" value="<%=getParam(request,"title")%>">
     <input type="hidden" name="txtAbstract" value="<%=getParam(request,"abstract")%>">
     <%
      for(int i=0;i<filecounter;i++){
     %>
      <input type="File" size="50" name="selectFile<%=i %>"><br>
     <%
      }
     %>    
     <input type="button" value="업로드" onclick="elementCheck(this.form)">    
    </td>
   </tr>
  </table>
 </form>
</body>
</html>



fileSelectPro.jsp (파일 업로드)


<%@ page language="java" contentType="text/html; charset=EUC-KR" pageEncoding="EUC-KR"%>
<%@ page import="com.oreilly.servlet.MultipartRequest" %>
<%@ page import="com.oreilly.servlet.multipart.DefaultFileRenamePolicy" %>
<%@ page import="java.util.*" %>
<%@ page import="java.net.*" %>
<%@ page import="java.io.*" %>
 <%@ include file="/view/color.jsp" %>

<%
 String realPath="";
 String savePath="fileSave";
 String type="euc-kr";
 int maxSize=5*1024*1024;
 
 ServletContext context = getServletContext();
 realPath = context.getRealPath(savePath);
 ArrayList saveFiles = new ArrayList();
 ArrayList origFiles = new ArrayList();
 
 String user="";
 String title="";
 String content="";
 
 try{
  MultipartRequest multi = new MultipartRequest(request,realPath,maxSize,type,new DefaultFileRenamePolicy());
  user = multi.getParameter("txtUser");
  title = multi.getParameter("txtTitle");
  content = multi.getParameter("txtAbstract");
 
  Enumeration files = multi.getFileNames();
  while(files.hasMoreElements()){
   String name=(String)files.nextElement();
   saveFiles.add(multi.getFilesystemName(name));
   origFiles.add(multi.getOriginalFileName(name));
  }
%>
 <html>
 <head>
  <title>멀티 업로드 예제</title>
  <link href="style.css" rel="stylesheet" type="text/css">
 </head>
 <body bgcolor="<%=bodyback_c%>">
  <table width="50%" border="1" align="center" cellpadding="1" cellspacing="1">
   <tr>
    <td width="15%" bgcolor="<%=value_c%>" align="center"><strong>작성자</strong></td>
    <td width="35%"><%=user %></td>
   </tr>
   <tr>
    <td width="15%" bgcolor="<%=value_c%>" align="center"><strong>제목</strong></td>
    <td width="35%"><%=title %></td>
   </tr>
   <tr>
    <td width="15%" bgcolor="<%=value_c%>" align="center"><strong>내용</strong></td>
    <td width="35%"><%=content %></td>
   </tr>
   <tr>
    <td colspan="4" bgcolor="<%=value_c%>">&nbsp;</td>
   </tr>  
   <tr>
    <td colspan="4"><strong>업로드된 파일 리스트</strong></td>
   </tr>
   <% for(int i=0; i<saveFiles.size(); i++){ %>
   <tr>
    <td colspan="4">
     <%
      String y = (String)saveFiles.get(i);
      String x = request.getContextPath()+"/"+savePath+"/"+URLEncoder.encode(y,"UTF-8");    
     %>
     <%=i+1 %>.<a href="file_down.jsp?file_name=<%=saveFiles.get(i)%>"><strong><%=origFiles.get(i) %></strong></a>
    </td>
   </tr>
   <% } %>
  </table>
 </body>
 </html>
<%
 }catch(IOException ioe){
  System.out.println(ioe);
 }catch(Exception ex){
  System.out.println(ex);
 }
%>



file_down.jsp [다운로드]


<%@ page language="java" contentType="text/html; charset=EUC-KR" pageEncoding="EUC-KR"%>
<%@ page import ="java.io.*" %>
<%
 String fileName = request.getParameter("file_name");
 String savePath = "fileSave";
 ServletContext context = getServletContext();
 String sDownloadPath = context.getRealPath(savePath);
 String sFilePath = sDownloadPath+"\\"+fileName;
 
 byte b[] = new byte[4096];
 
 FileInputStream in = new FileInputStream(sFilePath);
 
 String sMimeType = getServletContext().getMimeType(sFilePath);
 System.out.println("sMimeType>>>"+sMimeType);
 
 if(sMimeType == null) sMimeType = "application/octet-stream";
 response.setContentType(sMimeType);
 
 String sEncoding = new String(fileName.getBytes("euc-kr"),"8859_1");
 response.setHeader("Content-Disposition","attachment;filename="+sEncoding);
 
 ServletOutputStream out2 = response.getOutputStream();
 int numRead;
 
 while((numRead = in.read(b,0,b.length))!=-1){
  out2.write(b,0,numRead);
 }
 out2.flush();
 out2.close();
 in.close();
%>
<html>
<head>
<title>Insert title here</title>
</head>
<body>
</body>
</html>






반응형