@RequestMapping(value="/uploadForm", method=RequestMethod.POST) public String uploadForm(MultipartFile file, Model model) throws Exception{ logger.info("originalName : " + file.getOriginalFilename()); logger.info("size : " + file.getSize()); logger.info("contentType : " + file.getContentType()); String savedName = uploadFile(file.getOriginalFilename(), file.getBytes()); model.addAttribute("saveName", savedName); return "uploadResult"; } private String uploadFile(String originalName, byte[] fileDate) throws Exception{ UUID uid = UUID.randomUUID(); String savedName = uid.toString() + "_" + originalName; File target = new File(uploadPath, savedName); FileCopyUtils.copy(fileDate, target); return savedName; }


파일 이름이 중복될 경우 문제가 발생하기 때문에 고유값을 가진 파일이름으로 저장해야 된다.

uploadFile이라는 함수를 만들어서 UUID.randomUUID()을 이용하여 고유값을 가진 값 + 파일명으로 새로운 파일명을 만들고 업로드 함수로 리턴한다.


(출처 : 코드로 배우는 스프링 웹프로그래밍)

반응형

'JSP & Spring' 카테고리의 다른 글

@ModelAttribute, @RequestParam  (0) 2016.09.08
스프링 초기환경세팅  (0) 2016.09.04
utf-8 인코딩.  (0) 2016.09.01
페이징 처리하기 Pagination  (0) 2016.05.09
redirect 문법  (0) 2016.03.31

endPage 


endPage = (int) (Math.ceil(현재 페이지() / (double)페이지 번호의 수) * displayPageNum);

endPage는 현재의 페이지 번호를 기준으로 계산함.


현재 페이지가 3일 경우 : Math.ceil(5/10) * 10 = 10

현재 페이지가 1일 경우 : Math.ceil(1/10) * 10 = 10

현재 페이지가 20일 경우 : Math.ceil(20/10) * 10 = 20

현재 페이지가 21일 경우 : Math.ceil(21/10) * 10 = 30




startPage


endPage가 20이라면 startPage는 11이 됨. 

startPage = (endPage - 페이지 번호의 수 ) + 1;


100개의 데이터(totalCount)를 10개씩 보여준다면 endPage는 10. 20개씩 보여줘야 하는 경우 endPage는 5가 된다.


int tempEndPage = (int)(Math.ceil(totalCount / (double)cri.getPerPageNum());


if(endPage > tempEndPage){

endPage = tempEndPage;

}




Prev and Next


prev = startPage == 1 ? false : true;


next = endPage * cri.getPerPageNum() >= totalCount ? false : true;





- 코드로 배우는 스프링 웹 프로젝트 - 

반응형

'JSP & Spring' 카테고리의 다른 글

@ModelAttribute, @RequestParam  (0) 2016.09.08
스프링 초기환경세팅  (0) 2016.09.04
utf-8 인코딩.  (0) 2016.09.01
파일 업로드와 UUID  (0) 2016.07.30
redirect 문법  (0) 2016.03.31

다음과 같이 상대 경로로 리다이렉트 사용할 수 있음.

@Controller

public class RedirectController {


@RequestMapping("/studentConfirm")

public String studentRedirect(HttpServletRequest httpServletRequest, Model model){

String id = httpServletRequest.getParameter("id");

if(id.equals("abc")) {

return "redirect:studentOk";

}


return "redirect:studentNg";



}


절대 경로로도 리다이렉트를 사용할 수 있음.



@RequestMapping("/studentURL1")

public String studentURL1(Model model) {

return "redirect:http://localhost:8181/spring_14_3_ex1_srpingex/studentURL1.jsp";

}

반응형

'JSP & Spring' 카테고리의 다른 글

@ModelAttribute, @RequestParam  (0) 2016.09.08
스프링 초기환경세팅  (0) 2016.09.04
utf-8 인코딩.  (0) 2016.09.01
파일 업로드와 UUID  (0) 2016.07.30
페이징 처리하기 Pagination  (0) 2016.05.09

+ Recent posts