JAVA Basic 강의자료] 단순한 파일 업로드

 

JAVA Basic 강의자료] 단순한 파일 업로드

 

 

 

 

실무개발자를위한 실무교육 전문교육센터학원
www.oraclejava.co.kr에 오시면 보다 다양한 강좌를 보실 수 있습니다.

 

 

 

단순한 파일 업로드

 

<소스코드>

 

JSP
form속성에 enctype="multipart/form-data“ 추가

<h4>파일 업로드</h4>
<form action="${appUrl}/test/fileupload/single.html" method="post" enctype="multipart/form-data">
   
    <input type="file" name="file" />
   
    <input type="submit"/>
</form> 

 

=================

 

컨트롤러
@Controller
@RequestMapping("/test/fileupload")
public class FileuploadController {
   
    @RequestMapping(method=RequestMethod.GET)
public void setupForm(Model model) {        
    }
   
    // 하나의 파일을 업로드함
    @RequestMapping(value="single", method=RequestMethod.POST)
    public ModelAndView doAction(@RequestParam("file") MultipartFile file) throws IllegalStateException, IOException { 

 

=================

 

컨트롤러 계속
if(!file.getOriginalFilename().isEmpty() && !file.isEmpty()) {
            File uploadFile = new File("d:/upload/", file.getOriginalFilename());
            //서버내의 다른 장소에 업로드함
            file.transferTo(uploadFile);            
            ModelAndView mav = new ModelAndView("/test/complete");
            mav.addObject("filename", file.getOriginalFilename());
            mav.addObject("filesize", FileUtils.byteCountToDisplaySize(file.getSize()));
            return mav;
           
        } else {
            ModelAndView mav = new ModelAndView("/test/fail");
            return mav;
        }
    }
} 

 

 

 

 

+ Recent posts