spring mvc
1) Using a MultipartResolver with Commons FileUpload
The following example shows how to use the CommonsMultipartResolver
:
Of course you also need to put the appropriate jars in your classpath for the multipart resolver to work. In the case of the CommonsMultipartResolver
, you need to usecommons-fileupload.jar
.
When the Spring DispatcherServlet
detects a multi-part request, it activates the resolver that has been declared in your context and hands over the request. The resolver then wraps the current HttpServletRequest
into a MultipartHttpServletRequest
that supports multipart file uploads. Using theMultipartHttpServletRequest
, you can get information about the multiparts contained by this request and actually get access to the multipart files themselves in your controllers.
Upload a file please Please upload a file
spring-mvc.xml
---------------------------------------------------------------------------------------------------------------------------------------------
Controller
@PostMapping("/device")public String onSubmit(@RequestPart("file") MultipartFile file) { // ...}
---------------------------------------------------------------------------------------------------------------------------------------------
@Controllerpublic class FileUploadController { @PostMapping("/form") 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"; } return "redirect:uploadFailure"; }}
2) Using a MultipartResolver with Servlet 3.0
In order to use Servlet 3.0 based multipart parsing, you need to mark the DispatcherServlet
with a "multipart-config"
section in web.xml
, or with ajavax.servlet.MultipartConfigElement
in programmatic Servlet registration, or in case of a custom Servlet class possibly with ajavax.servlet.annotation.MultipartConfig
annotation on your Servlet class. Configuration settings such as maximum sizes or storage locations need to be applied at that Servlet registration level as Servlet 3.0 does not allow for those settings to be done from the MultipartResolver.
Once Servlet 3.0 multipart parsing has been enabled in one of the above mentioned ways you can add the StandardServletMultipartResolver
to your Spring configuration:
----------------------------------------------------------------------------------------------------------------------------------------------
Upload a file please Please upload a file
spring-mvc.xml
Controller
----------------------------------------------------------------------------------------------------------------------------------------------
@Controllerpublic class FileUploadController { @PostMapping("/form") public String handleFormUpload(@RequestParam("name") String name, @RequestParam("file") Part file) { InputStream inputStream = file.getInputStream(); // store bytes from uploaded file somewhere return "redirect:uploadSuccess"; }}
----------------------------------------------------------------------------------------------------------------------------------------------