Spring 3.0 MultipartFile загрузка
Я конвертирую веб-приложение Java в среду Spring и благодарю за некоторые советы по вопросам, с которыми я сталкиваюсь при загрузке файлов. Оригинальный код был написан с использованием org.apache.commons.fileupload.
Does Spring MultipartFile wraps org.apache.commons.fileupload or I can exclude this dependency from my POM file?
I have seen following example:
<code>@RequestMapping(value = "/form", method = RequestMethod.POST) public String handleFormUpload(@RequestParam("file") MultipartFile file) { if (!file.isEmpty()) { byte[] bytes = file.getBytes(); // store the bytes somewhere return "redirect:uploadSuccess"; } else { return "redirect:uploadFailure"; } } </code>
Originally I tried to follow this example but was always getting an error as it couldn't find this request param. So, in my controller I have done the following:
<code>@RequestMapping(value = "/upload", method = RequestMethod.POST) public @ResponseBody ExtResponse upload(HttpServletRequest request, HttpServletResponse response) { // Create a JSON response object. ExtResponse extResponse = new ExtResponse(); try { if (request instanceof MultipartHttpServletRequest) { MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request; MultipartFile file = multipartRequest.getFiles("file"); InputStream input = file.getInputStream(); // do the input processing extResponse.setSuccess(true); } } catch (Exception e) { extResponse.setSuccess(false); extResponse.setMessage(e.getMessage()); } return extResponse; } </code>
и это работает. Если кто-то может сказать мне, почему @RequestParam не работает для меня, я буду признателен. Кстати, у меня есть
<code> <bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"> <property name="maxUploadSize" value="2097152"/> </bean> </code>
в моем файле контекста сервлета.