SpringMVC uploads files using FileUpload and springmvc files
Import FileUpload and common-io Jar packages
Note: 1. Struts2 requires other methods: When struts2 is used for filtering, the reqeust type is changed from HttpServletRequest to MultiPartRequestWrapper.
1. Test. jsp:
<form action="<%=basePath%>self/testFileUpload.do" method="post" enctype="multipart/form-data"> <input type="text" name="num"> <input name="img1" type="file" accept="image/gif,image/jpg,image/png,image/jpeg"/> <input name="img2" type="file" accept="image/gif,image/jpg,image/png,image/jpeg"/> <input type="submit" name="test"> <input type="reset" name="test1"> </form>
2. controller:
@ RequestMapping (value = "/testFileUpload") public void testFileUpload (HttpServletRequest request, HttpServletResponse response) throws Exception {String num = ""; String img1 = ""; string img2 = ""; String fileName = ""; try {String t = request. getCharacterEncoding (); boolean isMultipart = ServletFileUpload. isMultipartContent (request); // determines whether it is a Form file type. if (isMultipart) {DiskFileItemFactory factory = new DiskFileIte MFactory (); ServletFileUpload upload = new ServletFileUpload (factory); upload. setHeaderEncoding (request. getCharacterEncoding (); // It is very important to solve the problem of garbled file names uploaded in linux. // upload. setFileSizeMax (3*1024*1024); // 3 MList items = upload. parseRequest (request); if (items. size () = 0) {System. out. println ("no data");} Iterator <Object> formItem = items. iterator (); while (formItem. hasNext () {FileItem item = (FileItem) formItem. next (); // If it is a form element if (item. isFormField () {if (item. getFieldName (). equals ("num") {num = item. getString (request. getCharacterEncoding ()). trim () ;}}// if it is not a form element, if (! Item. isFormField () {String name = item. getName (); String type = item. getContentType (); long sizeInBytes = item. getSize (); try {String fullName = item. getName (). trim (); if (fullName! = "") {FileName = System. currentTimeMillis () + fullName. substring (fullName. indexOf (". "); if (item. getFieldName (). equals ("img1") {fileName = "img1-" + System. currentTimeMillis () + fullName. substring (fullName. indexOf (". ");} if (item. getFieldName (). equals ("img2") {fileName = "img2-" + System. currentTimeMillis () + fullName. substring (fullName. indexOf (". ");} String tomcatRoot = FileOperator. getRootPath (); String path = tomcatRoot; if (FileOperator. fileUploadEx (path, fileName, item. getSize (), item. getInputStream () {if (item. getFieldName (). equals ("img1") {System. out. println ("testimg1");} if (item. getFieldName (). equals ("img2") {System. out. println ("testimg2") ;}}} catch (Exception e) {e. printStackTrace () ;}}} else {System. out. println ("non-multipart/form-data submission method") ;}} catch (Exception e) {e. printStackTrace ();}}
SpringMVC uses commons fileupload to upload files. The set obtained by parseRequest (request) is empty. Please wait for a while.
Now that you have used Spring MVC, there is no need to write the uploaded code by yourself.
Next, upload the MultipartFile
Public String upload (@ RequestParam (value = "image") MultipartFile image)
Then you can do this.
Image. transferTo (new File ("where to save "));
There are other methods for image. Just take a look.
Configure a bean in XML
<Bean id = "multipartResolver"
Class = "org. springframework. web. multipart. commons. CommonsMultipartResolver">
<Property name = "maxUploadSize" value = "104857600"/>
<Property name = "maxInMemorySize" value = "4096"/>
<Property name = "defaultEncoding" value = "UTF-8"/>
</Bean>
A spring mvc implements a file upload instance. If you have an example, you 'd better have a comment.
Is Spring used? If you don't just find an upload control, such as COS, it's easy to do. You have a lot of Baidu...
Below we use Html5 multi-File Upload in our system