在 spring mvc 中上传多个文件
Multiple Files upload in spring mvc
我正在尝试使用 spring mvc 和 commons-fileupload*.jar 上传多个文件。我正在使用 HttpServletRequest 获取请求对象,然后使用 request.getPart() 获取文件部分。但是当我 运行 我发现下面的代码错误。
java.lang.IllegalStateException: Unable to process parts as no multi-part configuration has been provided
代码如下:
public ModelAndView uploadCon(@RequestParam Map<String,String> map, HttpServletRequest request)
{
Part part1=request.getPart("wallpaper0");
Part part2=request.getPart("wallpaperx");
//then write these files
}
spring-servlet.xml 文件的 bean 定义如下:
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>
这是示例
@RequestMapping(value = "/upload", method = RequestMethod.POST)
public @ResponseBody
ResponseEntity<Map<String, Object>> handleFileUpload(
@RequestParam("file1") MultipartFile file1,
@RequestParam("file2") MultipartFile file2) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("status", "failure");
try {
if (!file1.isEmpty() && !file2.isEmpty()) {
// get the files here
map.put("status", "success");
}
} catch (Exception e) {
LOGGER.severe(e.toString());
}
ResponseEntity<Map<String, Object>> responseEntity = new ResponseEntity<Map<String, Object>>(
map, HttpStatus.OK);
return responseEntity;
}
并确保您已添加
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="50000000"/>
</bean>
在您的 spring.xml 文件中。
或者 refer this sample 如果你想得到文件列表 List<MultipartFile>
.
我正在尝试使用 spring mvc 和 commons-fileupload*.jar 上传多个文件。我正在使用 HttpServletRequest 获取请求对象,然后使用 request.getPart() 获取文件部分。但是当我 运行 我发现下面的代码错误。
java.lang.IllegalStateException: Unable to process parts as no multi-part configuration has been provided
代码如下:
public ModelAndView uploadCon(@RequestParam Map<String,String> map, HttpServletRequest request)
{
Part part1=request.getPart("wallpaper0");
Part part2=request.getPart("wallpaperx");
//then write these files
}
spring-servlet.xml 文件的 bean 定义如下:
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver"/>
这是示例
@RequestMapping(value = "/upload", method = RequestMethod.POST)
public @ResponseBody
ResponseEntity<Map<String, Object>> handleFileUpload(
@RequestParam("file1") MultipartFile file1,
@RequestParam("file2") MultipartFile file2) {
Map<String, Object> map = new HashMap<String, Object>();
map.put("status", "failure");
try {
if (!file1.isEmpty() && !file2.isEmpty()) {
// get the files here
map.put("status", "success");
}
} catch (Exception e) {
LOGGER.severe(e.toString());
}
ResponseEntity<Map<String, Object>> responseEntity = new ResponseEntity<Map<String, Object>>(
map, HttpStatus.OK);
return responseEntity;
}
并确保您已添加
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="maxUploadSize" value="50000000"/>
</bean>
在您的 spring.xml 文件中。
或者 refer this sample 如果你想得到文件列表 List<MultipartFile>
.