Spring MVC Multi-file upload size limit and exception handling
The beautiful Life of the Sun Vulcan (http://blog.csdn.net/opengl_es)
This article follows "Attribution-non-commercial use-consistent" authoring public agreement
Reprint Please keep this sentence: Sun Vulcan's Beautiful Life-this blog focuses on Agile development and mobile and IoT device research: IOS, Android, HTML5, Arduino, Pcduino , Otherwise, the article from this blog refused to reprint or re-reproduced, thank you for your cooperation.
Add the following bean configuration to the default profile web-inf/xxx-servlet.xml in spring MVC
<bean id= "Multipartresolver" class= "Org.springframework.web.multipart.commons.CommonsMultipartResolver" > <property name= "defaultencoding" ><value>utf-8</value></property><property name= " Maxuploadsize "> <!--upload file size limited to 31m,31*1024*1024--><value>32505856</value></property ><property name= "Maxinmemorysize" ><value>4096</value></property></bean>
Then when using the Martpart file Upload component in the HTML page form <input type= "file" name= "xxx"/> upload file to spring MVC,
If a file is out of size, an exception is thrown, causing the entire WEB app to crash,
How can you capture this spring MVC framework-level exception in your custom controller?
Let's take a look at what the official documents say!
http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#mvc-exceptionhandlers-resolver
The implementation of the Spring Handlerexceptionresolver interface handles uncaught exceptions that occur during the execution of the controller. Handlerexceptionresolver slightly reorganized the exception mappings you defined in Web Application description file. Xml. However, they provide a more flexible way to do this. For example, you can provide information about which processor was executed by an exception throw. Further, programmatically handling exceptions gives you more selectivity in order to respond appropriately before the request is moved to another URL. (The same end result as when you use the Servlet's proprietary exception mapping)
In addition to implementing the Handlerexceptionresolver interface, the only thing that needs to be done is to implement the resolveexception (Exception, Handler) method and return a Modelandview, You can also use pre-provided simplemappingexceptionresolver or create a @ExceptionHandler method.
The contents of the entire controller are posted below,
The following three methods are uploading multi-file processing, processing results rendering, and file size overrun exception handling.
The first is to render the page through the JSP template and replace the variable, this is actually very simple, but a lot of methods after processing, can not replace the variables, including re-deployment, clean up the recompilation, and even rebuild the project.
It turned out that many of the problems were not the problem itself, but that the damned maven, after configuring a domestic maven image and buying a ladder to match, was just a stop.
In summary, the idea of Maven is good, but not applicable at home, because we do not have WIFI available everywhere, even if the network speed is also very naughty, even if the speed can be, no ladder is a baboon!
Solution, one is in a good network environment, with MAVEN to automatically down the package is enough, left to remove Maven.
The second is to build the Maven mirror server of this machine, this can not only be used for javaweb, but also the Gradle of Android Studio can be used, there is time to try, after the text and then fill.
Speaking of Maven had to insert an Apple CocoaPods, such things, in the domestic WIFI is not available everywhere, or try not to touch as well, maybe Apple will be better!
Come to think of it, perhaps the local Maven or CocoaPods mirror server should be the standard of their respective development environment, in a network environment for one-time download to local or update, and no other need to care about these issues.
The second and third is the JSON output, without using Spring MVC's own way, the configuration will be dead loop, do not know where the problem, and 4.2 of the way to see the official documents some trouble, more annotations, the entire application is fine.
Here, using Fastjson to convert a list or object directly to a String and then output, if you want to simplify, you can also self-construct a layer of parent class to process the returned object or list.
@Controller @requestmapping ("/test") public class TestController implements Handlerexceptionresolver {@RequestMapping ("/welcome") public Modelandview HelloWorld () {String message = "<br><div style= ' text-align:center; ' > "+"
Spring MVC Multi-file upload size limit and exception handling