我正在将Java Web应用程序转换为Spring框架,并对我在上传文件时遇到的问题提供了一些建议。原始代码是使用org.apache.commons.fileupload编写的。Spring 3.0 MultipartFile上传
春天在什么MultipartFile包装org.apache.commons.fileupload或者我可以排除我的POM文件这种依赖?
我已经看到了下面的例子:
@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"; } }
本来我想以此为榜样,但总是得到一个错误,因为它找不到此请求PARAM。所以,在我的控制器我已经做了以下:
@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; }
,它是工作。如果有人能告诉我为什么@RequestParam不适合我,我会很感激。顺便说一句我有
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="2097152"/>
</bean>
在我的servlet上下文文件。
你是什么意思将它作为方法参数传递?谁将从请求中提取它并将其提供给该方法? – Gary
春天会... :) – Bozho