SPRINGMVC default parser inside is not added to the file upload parsing, use Springmvc file upload parser to handle file upload need to use SPRINGMVC provide multipartresolver declaration, And because Commonsmultipartresolver implements the Multipartresolver interface, we can configure this in the SPRINGMVC configuration file:
XML code
- <Bean id="Multipartresolver"
- class="Org.springframework.web.multipart.commons.CommonsMultipartResolver">
- <property name= "defaultencoding" value="utf-8" />
- <property name= "maxuploadsize" value="10485760000" />
- <property name= "maxinmemorysize" value="40960" />
- </Bean>
First introduce the file upload required package, Commons-logging-*.jar Commons-io-*.jar Commons-fileupload-*.jar
Create a new JSP page.
HTML code
- <%@ page language="java" contenttype="text/html; Charset=utf-8 "
- pageencoding="UTF-8"%>
- <! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <Meta http-equiv= "content-type" content= "text/html; Charset=utf-8 ">
- <title> File Upload </title>
- </head>
- <body>
- <%--<form action="user/fileupload" method="POST" enctype=" Multipart/form-data ">--%>
- <form action="user/fileupload" method="POST" enctype="Multipart/form-data " >
- <input type="file" name="fileUpload" />
- <input type="Submit" value= "upload" />
- </form>
- </body>
- </html>
Springmvc upload the file form There are many, here I introduce two kinds.
First, look at the controller.
Java code
- Package Gd.hz.springmvc.controller;
- Import Java.io.File;
- Import java.io.IOException;
- Import Org.springframework.stereotype.Controller;
- Import org.springframework.web.bind.annotation.RequestMapping;
- Import Org.springframework.web.bind.annotation.RequestMethod;
- Import Org.springframework.web.bind.annotation.RequestParam;
- Import Org.springframework.web.multipart.commons.CommonsMultipartFile;
- Import Org.springframework.web.servlet.ModelAndView;
- @Controller ("Usercontroller")
- @RequestMapping ("user")
- Public class Usercontroller {
- //process file upload one
- @RequestMapping (value = "FileUpload", method = Requestmethod.post)
- Public Modelandview FileUpload (
- @RequestParam ("fileUpload") commonsmultipartfile file) {
- //Get file type
- System.out.println (File.getcontenttype ());
- //Get file size
- System.out.println (File.getsize ());
- //Get file name
- System.out.println (File.getoriginalfilename ());
- //Determine if the file exists
- if (!file.isempty ()) {
- String path = "d:/" + file.getoriginalfilename ();
- File LocalFile = new file (path);
- try {
- File.transferto (LocalFile);
- } catch (IllegalStateException e) {
- E.printstacktrace ();
- } catch (IOException e) {
- E.printstacktrace ();
- }
- }
- return new Modelandview ("datasuccess");
- }
- }
Class Commonsmultipartfile provides us with a number of methods for handling files. For example, file size, upload file name, file type, specific usage you can view the spring documentation. Transferto is the output of the file to the specified place.
File upload the second method, this method is more commonly used:
Java code
- Package Gd.hz.springmvc.controller;
- Import Java.io.File;
- Import java.io.IOException;
- Import Java.util.Iterator;
- Import Javax.servlet.http.HttpServletRequest;
- Import Org.springframework.stereotype.Controller;
- Import org.springframework.web.bind.annotation.RequestMapping;
- Import Org.springframework.web.bind.annotation.RequestMethod;
- Import Org.springframework.web.multipart.MultipartFile;
- Import Org.springframework.web.multipart.MultipartHttpServletRequest;
- Import Org.springframework.web.multipart.commons.CommonsMultipartResolver;
- @Controller ("Usercontroller")
- @RequestMapping ("user")
- Public class Usercontroller {
- //process file upload two
- @RequestMapping (value = "FileUpload2", method = Requestmethod.post)
- Public String fileUpload2 (httpservletrequest request)
- throws IllegalStateException, IOException {
- //Set up and down Fang Wen
- Commonsmultipartresolver multipartresolver = new Commonsmultipartresolver (
- Request.getsession (). Getservletcontext ());
- //Check if the form has enctype= "Multipart/form-data"
- if (Multipartresolver.ismultipart (Request)) {
- Multiparthttpservletrequest multirequest = (multiparthttpservletrequest) request;
- Iterator<string> iter = Multirequest.getfilenames ();
- While (Iter.hasnext ()) {
- //inherited by Commonsmultipartfile, with the method above.
- Multipartfile file = Multirequest.getfile (Iter.next ());
- if (file! = null) {
- String fileName = "Demoupload" + file.getoriginalfilename ();
- String path = "d:/" + fileName;
- File LocalFile = new file (path);
- File.transferto (LocalFile);
- }
- }
- }
- return "datasuccess";
- }
- }
Multiparthttpservletrequest provides a more flexible way to get multiple files and file names that can be traversed to get each file.
Springmvc File Upload