티스토리 뷰

자바(Java)

jsp 파일 다운로드

xemaker 2021. 5. 10. 14:31

http://www.servlet.com/

com.oreilly.servlet 클릭

Download 해당 zip 파일 다운: 예) cos-26Dec2008.jar

다운 받았으면 서버의 WEB-INF\lib 폴더에 위치시킨다.

여기서는 파일 2개를 만들어 해보겠다.
1. 폼만 있는 fileUploadForm.jsp
2. 폼의 값을 받아 서버에 저장하는 fileUpload.jsp



fileUploadForm.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
</head>

<body>
<form action="fileUpload.jsp" method="post" enctype="multipart/form-data">
<input type="file" name="fileName1" /><br/>
<input type="submit" value="전송"/>
</form>
</body>
</html>

fileUpload.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<%@page import="java.io.File"%>
<%@page import="java.util.Enumeration"%>
<%@page import="com.oreilly.servlet.multipart.DefaultFileRenamePolicy"%>
<%@page import="com.oreilly.servlet.MultipartRequest"%>

<%

String path=request.getSession().getServletContext().getRealPath("file");
File folder=new File(path);

int size=1024*1024*10;
String file="";
String originalFile="";

if(!folder.exists()){
try{
folder.mkdir();
out.println("폴더 생성");
}catch(Exception e){
out.println("error="+e.getMessage());
}
}

try{
out.println("start");
MultipartRequest multi=new MultipartRequest(request, path, size, "UTF-8", new DefaultFileRenamePolicy());

Enumeration files = multi.getFileNames();
String str=(String)files.nextElement();

out.println("str="+str);

out.println("end");

}catch(Exception e){
out.println("error="+e.getMessage());
}

%>

<html>
<body>
file upload seccess
</body>
</html>

file 이라는 디렉토리에 업로드한 파일들이 저장된다.
여기서는 File 클래스를 사용해 디렉토리를 미리 안만들어 놔도 없으면 생성하게 추가하였다.


댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/09   »
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
글 보관함