Reprinted from: http://blog.csdn.net/u013771277/article/details/47384817
Springmvc file Upload, first two base, 1. Add Enctype= "Multipart/form-data" to form form properties
Emphasis: Form form <form method= "post" ..., method must have, I am here to use is post, as to get line not have tried, no method= "POST" will also report not multipart request error.
2. Configuration file in configuration Multipartresolver
File out-of-bounds will throw an exception before entering the controller, without affecting the configuration within the allowed range
3. Simple Receive method, idea: Multipartfile accept the file and enter it into the fileoutstream by IO binary stream (Multipartfile.getinputstream ()) to save the file, then why do you do it?
The parameters receive the same as Multipartfile receive.
Accept files and parameters named file and ID in the form screenshot of the form. As follows
@RequestMapping (value = "Attendee_uploadexcel.do")
@ResponseBody
public void Uploadexcel (@RequestParam ("file") Multipartfile file, @RequestParam ("id") String ID) throws Exception {
parameter of form form submission test to String type
if (file = = null) return;
String fileName = File.getoriginalfilename ();
String path = Getrequest (). Getservletcontext (). Getrealpath ("/upload/excel");
Gets the actual path of the specified file or folder in the project, Getrequest () This method is to return a httpservletrequest that encapsulates this method in order to handle the encoding problem
FileOutputStream fos = fileutils.openoutputstream (new File (path+ "/" +filename));//Open Fileoutstrean stream
Ioutils.copy (File.getinputstream (), FOS);//convert multipartfile file into binary stream and input to Fileoutstrean
Fos.close ();//
......
}
Other methods, will httpservletrequest req strong turn into Multiparthttpservletrequest req after req.getparameter ("id");
HttpServletRequest request;
Multiparthttpservletrequest multipartrequest = (multiparthttpservletrequest) request;
Multipartfile file = multipartrequest.getfile ("file");
String id = multipartrequest.getparameter ("id"); Receive parameters that are carried by the client incoming file
String fileName = File.getoriginalfilename (); .........