spring MVC 는 처리 테이프 파일 과 비 파일 폼 을 제출 합 니 다.

2784 단어 springMVC

1. 파일 업로드 에 필요 한 jar 가 져 오기
2. 웹. xml 설정

	
		SpringCharacterEncodingFilter
		org.springframework.web.filter.CharacterEncodingFilter
		
			encoding
			UTF-8
		
	
	
		SpringCharacterEncodingFilter
		/*
	

3. spring - servler. xml 프로필 설정

	
		
		
		
	
	
	
	
	
		
			
				
				error_fileupload
			
		
	

4. 컨트롤 러 작성
@RequestMapping(value="/add",method="RequestMethod.post")
public String addUser(User user,@RequestParam MultipartFile[] myfiles,HttpServletRequest request)
{
for(MultipartFile myfile : myfiles){
			if(myfile.isEmpty()){
				System.out.println("     ");
			}else{
				System.out.println("    : " + myfile.getSize());
				System.out.println("    : " + myfile.getContentType());
				System.out.println("    : " + myfile.getName());
				System.out.println("    : " + myfile.getOriginalFilename());
				System.out.println("========================================");
				//     Tomcat   ,       \\%TOMCAT_HOME%\\webapps\\YourWebProject\\WEB-INF\\upload\\    
				String realPath = request.getSession().getServletContext().getRealPath("/WEB-INF/upload");
				//      IO      ,  FileUtils.copyInputStreamToFile()           IO   ,           
				FileUtils.copyInputStreamToFile(myfile.getInputStream(), new File(realPath, myfile.getOriginalFilename()));
			}
		}
		users.put(user.getUsername(), user);
		return "/user/list";
}

좋은 웹페이지 즐겨찾기