2013년 6월 24일 월요일

2013-06-25 Spring Framework Commons FileUpload

17.10.2 Using a MultipartResolver with Commons FileUpload

The following example shows how to use the CommonsMultipartResolver:

<bean id="multipartResolver"
 class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
 <property name="maxUploadSize" value="100000" /> <!-- 업로드 파일 용량 제한 -->
</bean>


The next step is to create a controller that handles the file upload. This controller is very similar to a normal annotated @Controller, except that we use MultipartHttpServletRequest or MultipartFile in the method parameters:

@Controller
public class FileUploadController {

    @RequestMapping(value = "/form", method = RequestMethod.POST)
    public String handleFormUpload(@RequestParam("name") String name,
        @RequestParam("file") MultipartFile file) {

        if (!file.isEmpty()) {
            byte[] bytes = file.getBytes();
            // store the bytes somewhere
           return "redirect:uploadSuccess";
       } else {
           return "redirect:uploadFailure";
       }
    }
}

댓글 없음:

댓글 쓰기