@PostMapping("/insert")
public String insert(HttpServletRequest requeset) {
DiskFileUpload upload = new DiskFileUpload(); // 파일 전송 컴포넌트 생성
try {
// request 객체는 웹에서 넘어오는(클라이언트->서버) 정보를..
// 웹브라우저 전송 객체 생성해서 업로드 컴포넌트에 전달
List items = upload.parseRequest(requeset);
// 반복자 생성
Iterator params = items.iterator();
String filepath = "C:\\myWorkSpace\\learnJsp\\pds";
// 객체 생성
BoardVO board = new BoardVO();
while (params.hasNext()) { // form 객체가 있을 경우 (params는 itertor객체에 대한 것)
FileItem item = (FileItem)params.next(); // 폼 형식 객체를 변수에 저장
if (item.isFormField()) { // 파일형식이 아니라면
String fieldname = item.getFieldName();
String fieldvalue = item.getString("utf-8");
//log.info(fieldname + "/" + fieldvalue);
if (fieldname.equals("b_subject")) {
board.setB_subject(fieldvalue);
} else if (fieldname.equals("b_name")) {
board.setB_name(fieldvalue);
} else if (fieldname.equals("b_contents")) {
board.setB_contents(fieldvalue);
}
} else { // 바이너리 파일이라면
String fname = item.getName(); // 전체 이름 리턴함
log.info(fname);
if (fname != "") { // fname이 ""이 아닌 경우 즉, 뭐라도 있는경우
board.setB_file(fname);
File file = new File(filepath + "/" + fname); // 파일객체 생성, 파일이 해당 경로에 상품코드 이름으로 저장됨
item.write(file); // 해당 경로에 파일 저장, item 에 파일을 쓴다
}
}
}
// ★while if 문이 시작되기 전에 넣어버리면 게시글 등록되지 않음
// 끝나고 들어간다
service.insert(board);
log.info(board);
} catch (Exception e) { // 예외 처리
System.out.println(e);
}
return "redirect:/board/list";
}
service.insert(board); 의 위치 문제
'Framework > Spring' 카테고리의 다른 글
새 프로젝트로 작업 시 항상 초기에 설정해야 할 것 (0) | 2022.03.22 |
---|---|
column 'column명' cannot be null 오류 (0) | 2022.03.22 |
[jsp] 리다이렉트시 alert창 띄우기 (0) | 2022.03.17 |
[에러]서블릿 [appServlet]을(를) 위한 Servlet.init() 호출이 예외를 발생시켰습니다. (0) | 2022.03.10 |
0304 (0) | 2022.03.04 |
댓글