SpringMVC is a simple example of uploading files. springmvc uploads files.
Simple Example of uploading files using SpringMVC
When springMVC is used for system implementation, springMVC's default parser does not include parsing for file upload, which makes it easy for us to upload our own files. However, if you want to use springMVC to process file uploads, you need to add the MultipartResolver statement provided by springMVC in spring applicationContext. In this way, springMVC checks whether the request contains multimedia information every time the client initiates a request. If the request contains multimedia information, it uses MultipartResolver for resolution, springMVC uses a MultipartHttpServletRequest that supports file processing to wrap the current HttpServletRequest, and then uses MultipartHttpServletRequest to process the file. Spring has provided us with a MultipartResolver implementation. We only need to use it, that is, org. springframework. web. multipart. commons. CommsMultipartResolver. Because springMVC's MultipartResolver uses Commons-fileupload at the underlying level, you also need to add support for the Commons-fileupload.jar.
Xml Code
<! -- The id stated here must be multipartResolver --> <bean id = "multipartResolver" class = "org. springframework. web. multipart. commons. CommonsMultipartResolver"> <! -- One of the properties available; the maximum file size in bytes --> <property name = "maxUploadSize" value = "100000"/> </bean>
CommonsMultipartResolver allows the following attributes:
DefaultEncoding: indicates the default encoding format used to parse the request. The default ISO-8859-1 is used according to Servlet specifications when not specified. When the request itself specifies its encoding format, it will ignore the defaultEncoding specified here.
UploadTempDir: Set the temporary directory when uploading files. The default directory is the temporary directory of the Servlet container.
MaxUploadSize: sets the maximum file size that can be uploaded, in bytes. If it is set to-1, there is no limit. The default value is-1.
MaxInMemorySize: sets the maximum value allowed to be written to the memory during file upload, in bytes. The default value is 10240.
The following is a simple example:
. Html file
<Html>
Corresponding action controller:
Java code
@ Controller public class FileUpoadController {@ RequestMapping (value = "/form", method = RequestMethod. POST) public String handleFormUpload (@ RequestParam ("name") String name, @ RequestParam ("file") MultipartFile file) {// MultipartFile is the encapsulation of the currently uploaded file, to upload multiple files at the same time, you can specify multiple MultipartFile parameters if (! File. isEmpty () {byte [] bytes = file. getBytes (); // store the bytes somewhere // You can process the file here. You can save it to a folder of the database or server as needed. return "redirect: uploadSuccess ";}else {return" redirect: uploadFailure ";}}}
Thank you for reading this article. I hope it will help you. Thank you for your support for this site!