💡 핵심 요약 (TL;DR)
Servlet게시판8: 파일업로드·파일보기의 동작 원리를 이해하고 실제 프로젝트에 적용하기 위한 실무 가이드다.
* 파일 업도르시 request.getRealPath("/upload")는 이제 deprecated -> 실무에선 context에 있는 realpath를 사용함을 적용할 수 있다.
BoardFrontController.java의 doProcess()의 주소비교 후 처리부분에 코드 추가1 2 3 4 5 6 7 8 9 10 11 12 13 }else if (command.equals("/FileBoardWrite.bo" )){ System.out.println("C : /FileBoardWrite.bo 호출" ); forward = new ActionForward (); forward.setPath("./board/fwriteForm.jsp" ); forward.setRedirect(false ); }else if (command.equals("/FileBoardWriteAction.bo" )){ System.out.println("C : /FileBoardWriteAction.bo 호출" ); action = new FileBoardWriteAction (); try { forward = action.execute(request, response); }catch (Exception e){e.printStackTrace(); } }
FileBoardWriteAction.java 생성
파일 업도르시 request.getRealPath("/upload")는 이제 deprecated -> 실무에선 context에 있는 realpath를 사용함
request를 MultipartRequest로 바뀌었으니 MultipartRequest에 정보를 저장해야한다.
BoardDAO객체생성 -> insertBoard() 재사용
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 31 32 33 34 35 36 37 38 39 40 41 42 43 44 public class FileBoardWriteAction implements Action { @Override public ActionForward execute (HttpServletRequest request, HttpServletResponse response) throws Exception { System.out.println("M : FileBoardWriteAction의 execute()실행" ); String realpath = request.getRealPath("/upload" ); System.out.println("realpath: " +realpath); int maxSize = 10 * 1024 * 1024 ; MultipartRequest multi = new MultipartRequest ( request, realpath, maxSize, "UTF-8" , new DefaultFileRenamePolicy () ); System.out.println("파일업로드성공" ); BoardDTO bdto = new BoardDTO (); bdto.setName(multi.getParameter("name" )); bdto.setPw(multi.getParameter("pw" )); bdto.setSubject(multi.getParameter("subject" )); bdto.setContent(multi.getParameter("content" )); bdto.setFile(multi.getFilesystemName("file" )); bdto.setIp(request.getRemoteAddr()); System.out.println("업로드 할 객체 정보: " +bdto); BoardDAO bdao = new BoardDAO (); bdao.insertBoard(bdto); ActionForward forward = new ActionForward (); forward.setPath("./BoardList.bo" ); forward.setRedirect(true ); return forward; } }
cos.jar라이브러리설치
폼태그속성 enctype=”multipart/form-data”, method=”post”
get방식은 처리되지 않음. post로 해야함
가상경로를 가진 `upfile’ 폴더생성
1 2 3 4 5 6 7 8 9 10 11 12 13 14 <fieldset > <legend > 파일업로드(p305~)</legend > <form action ="./FileBoardWriteAction.bo" method ="post" enctype ="multipart/form-data" > 글쓴이 : <input type ="text" name ="name" required > <br > 비밀번호 : <input type ="password" name ="pw" required > <br > 제목 : <input type ="text" name ="subject" maxlength ="15" required > <br > 내용 : <br > <textarea rows ="10" cols ="35" name ="content" placeholder ="여기에 작성해주세요" required > </textarea > <br > 파일 : <input type ="file" name ="file" > <input type ="submit" value ="글등록" class ="btn" > <button type ="reset" class ="btn" > 초기화</button > <input type ="button" value ="목록으로" class ="btn" onclick ="location.href='./BoardList.bo'" > </form > </fieldset >
content.jsp 첨부파일 코드 수정
a태그의 href를 ./upload/<%=bdto.getFile()%>로 변경하면 이름을 클릭하는 순간 바로 볼 수 있다.
브라우저가 지원해주는 파일확장자인 경우 바로 파일 보기 가능 ex)이미지, 텍스트 둥둥
브라우저가 지원해주는 파일확장자가 아닌 경우 다운로드 됨 ex) 압축파일 등등
1 2 3 4 <tr > <th > 첨부파일</th > <td colspan ="3" > <a href ="./upload/<%=bdto.getFile()%>" > <%=bdto.getFile() %></a > </td > </tr >
#TIL ITWILL(JAVA)