일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- dock
- 전자정부프레임워크
- 가우스
- Spring
- PHP
- rowspan
- Android
- Eclipse
- WebLogic
- MySQL
- PLSQL
- JDOM
- iBATIS
- GPS
- jsr 296
- sencha touch
- MFC
- jQuery
- Struts
- tomcat
- 선택적조인
- phonegap
- oracle
- appspresso
- JSON
- Ajax
- node.js
- swingx
- ibsheet
- Google Map
Archives
- Today
- Total
Where The Streets Have No Name
org.apache.commons.fileupload를 이용한 파일업로드 본문
fileUP.html
<HTML> <HEAD> <META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=windows-1252"/> <TITLE>File Upload Page</TITLE> </HEAD> <BODY>Upload Files <FORM name="filesForm" action="ProcessFileUpload.jsp" method="post" enctype="multipart/form-data"> File 1:<input type="file" name="file1"/><br/> File 2:<input type="file" name="file2"/><br/> File 3:<input type="file" name="file3"/><br/> <input type="submit" name="Submit" value="Upload Files"/> </FORM> </BODY> </HTML> |
파일 업로드의 시작점은 HTML은 Form의 속성이 method="post" enctype="multipart/form-data" 이어야 합니다. FileUpload 컴포넌트는 enctype에 의해서 전송되는 파라미터를 inputStream으로 받을지 Reader로 받을지 결정합니다.
서버측 프로그램 ProcessFileUpload.jsp
<%@ page import="org.apache.commons.fileupload.*"%> <%@ page import="java.util.List"%> <%@ page import="java.util.Iterator"%> <%@ page import="java.io.*"%> <%@ page import="org.jdf.util.*"%> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=EUC-KR"> <title>Process File Upload</title> </head> <% //한글파일명처리를 위해서 코딩 request.setCharacterEncoding("EUC-KR"); String oldfilename=""; try { if (FileUpload.isMultipartContent(request)) { String upload = application.getRealPath("/")+"upload/"; DiskFileUpload fileUpload = new DiskFileUpload(); //fileUpload.setHeaderEncoding("EUC_KR"); fileUpload.setRepositoryPath(upload); fileUpload.setSizeMax(100*1024*1024); fileUpload.setSizeThreshold(1024*50); List items = fileUpload.parseRequest(request); while (iterator.hasNext()) { //title 입력문구처리 if ("title".equals(item.getFieldName())){ //title = item.getString("EUC_KR"); title = item.getString(); } if (item.getSize() > 0) { //파일 이름을 가져온다 String filename = item.getName().substring(item.getName().lastIndexOf("\")+1); System.out.println("filename : " + filename); //확장자명을 가져온다. String extName = filename.substring(filename.lastIndexOf(".")); //파일이름 변경시 적용 oldfilename = filename; filename = UniqueID.getString(); try { File file = new File(upload+filename+extName); item.write(file); } catch (IOException e) { System.out.println(e); } } } } } System.out.println("파일 사이즈가 100메가 보다 더 초과되었습니다"); } catch (Exception e) { System.out.println("업로드시 예기치 못한 오류가 발생하였습니다"); } %> <body> Upload Successful!!(<%=title%>)(<%=oldfilename%>) </body> </html> |