티스토리 뷰
# 파일 업로드
<form action="..." method="post" enctype="multipart/form-data">
--> 파일 업로드를 하려면 폼태그를 이렇게 설정하면 됨
multipart/form-data 형식으로 전송된 데이터는 request.getParameter()같은
메서드로 접근 할 수 없어서 서블릿 3.0에 추가된 Part 인터페이스를 사용해서 접근
파일 업로드를 하려면 web.xml 에 세팅
================================
<multipart-config>
<location>C:\Tomcat 8.5\webapps\pp1\upload</location>
<max-file-size>-1</max-file-size>
<max-request-size>-1</max-request-size>
<file-size-threadshold>1024</file-size-threadshold>
</multipart-config>
request.getContentType();
--> enctype을 얻어옴(default:application/x~~ , multipart/form-data)
Collection<Part> parts=request.getParts();
--> 넘어온 파라미터들 Collection(set,list 등등)에 나눠서 넣음(배열처럼)
for(Part part:parts ){
part.getName()
--> 넘어온 파라미터 이름 얻어옴
part.getHeader("Content-Disposition").contains("filename=")
--> 넘어온 Header에 filename이 포함되있냐
--> 왜 구분? 파일이면 file writer 써야됨
part.getSize();
--> 파일인 경우 파일의 크기
part.getSubmittedFileName();
--> 업로드한 파일의 이름을 구함
part.write();
--> part의 업로드 데이터를 filename이 지정한 파일에 복사(임시보관)
--> 그래서 파일업로드 안에 클래스를 새로 만들어줘야됨(FileUploadUtil)
--> 그래서 실제 파일을 업로드할려면 이 클래스를 불러줄거임 얘말고
part.delete();
--> part와 관련된 파일 삭제
}
FileUploadUtil
==============================
public class FileUploadUtil {
public static BoardFile doFileUpload(FileItem fileitem)
throws FileNotFoundException, IOException {
InputStream in = fileitem.getInputStream();
String filename=fileitem.getName().substring(fileitem.getName().lastIndexOf("\\")+1);
// 실제 파일이름가져오기(part의 getSubmittedFileName();)
String newFileName = Long.toString(System.currentTimeMillis()+new Object().hashCode());
//고유한 이름 따로 설정.
//파일을 업로드할 절대 경로를 지정해야 한다.
String path ="C:/upload/";
FileOutputStream fout = new FileOutputStream(path + newFileName );
int bytesRead = 0;
byte[] buffer = new byte[8192];
while ((bytesRead = in.read(buffer, 0, 8192)) != -1)
fout.write(buffer, 0, bytesRead);
//bytesRead = 읽어온개수. 마지막은 8kb가 다 안될수도 있기때문에 읽은만큼만 write해주기위해
in.close();
fout.close();
BoardFile boardFile = new BoardFile();
boardFile.setFileName(filename);
boardFile.setTempFileName(newFileName);
// 답변형 게시판에 세팅
return boardFile;
}
}
# 답변형 게시판 (스트럭쳐? mvc 형식-옛날꺼)
*init 흐름 (mvc랑 거의 비슷해 뭐 따로 별건없음)
**ActionMapper**
configFile = /WEB-INF/member.properties,/WEB-INF/board.properties
commandFiles [0] = /WEB-INF/member.properties
[1] = /WEB-INF/board.properties
for문 첫번째
command = /WEB-INF/member.properties
keys = member.properties
key = member
commands = new HashMap<String,Action>();
commandList.put(key,commands); 세팅
initCommands(config,commands,command); 호출
*initCommands*
fis = C:\apache-tomcat-8.5.14/webapps/WEB-INF/member.properties
set = loginform.do
login.do
logout.do
...
n = 공백 짜른 set
className = com.seoulit.member.action.LoginFormAction
com.seoulit.member.action.LoginAction
com.seoulit.member.action.LogoutAction
....
commands.put(n, (Action)(Class.forName(className).newInstance()) );
commands.put(n,class들 객체값 주소) 세팅
for문 두번째
command = /WEB-INF/board.properties
나머지 똑같은 흐름
commandList,commands 에 각자 2개씩 세팅 완료(init완료)
*테이블 구조
newboard2테이블
board_seq :게시판 번호
ref_seq :그룹(참조) 번호
reply_seq :답글 번호
새글인경우 ref_seq=board_seq
새글인경우 reply_seq=0
답변글인경우 ref_seq=부모ref_seq
답변글인경우 reply_seq=부모board_seq
board_seq ref_seq reply_seq name
1 1 0 종우 //새글
2 1 1 동욱 //종우글 답글
3 3 0 초희 //새글
4 1 1 홍길동 //종우글 답글
5 1 2 김길동 //동욱글 답글
order by 1.ref_seq 내림차순 (그래야 최신글이 제일 위에)
2.borad_seq 오름차순 (default라서 생략가능)
boardfile2테이블
종우가 파일 3개 올림
file_seq board_seq filename tempfilename
1 1 a.java 1234
2 1 b.gif 5678
3 1 c.jsp 91011
'VIEW > JSP&SERVLET' 카테고리의 다른 글
[JSP] sitemesh (0) | 2017.12.01 |
---|---|
[JSP] 어노테이션(annotation) (0) | 2017.12.01 |
[JSP] 국제화태그,필터 (0) | 2017.12.01 |
[JSP] JSTL (0) | 2017.12.01 |
[JSP] <jsp:useBean>,Cookie,EL (0) | 2017.12.01 |