Springmvc file upload, first two basis, 1. Form form Properties plus enctype= "Multipart/form-data"
Emphasis: Form form's <form method= "post" ..., method must have, I am here to use is post, as for get row not tried, no method= "POST" also will report not multipart request error.
2. Configure Multipartresolver in configuration file
A file exceeding the limit throws an exception before entering the controller, which does not affect the configuration within the allowable range
3. A simple way of receiving, thinking: Multipartfile accept the file and through the IO binary stream (Multipartfile.getinputstream ()) input to Fileoutstream Save the file, then do what you do
The parameter receives the same as the Multipartfile receive.
Accept the files and parameters named file and ID in the form forms screenshot. As follows
@RequestMapping (value = "Attendee_uploadexcel.do")
@ResponseBody
public void Uploadexcel (@RequestParam ("file") Multipartfile file, @RequestParam ("id") String ID) throws Exception {
Form form Submit parameter test is 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, encapsulating this method in order to deal with the coding problem
FileOutputStream fos = fileutils.openoutputstream (new File (path+ "/" +filename));//Open Fileoutstrean stream
Ioutils.copy (File.getinputstream (), FOS);//convert multipartfile file into a binary stream and enter the Fileoutstrean
Fos.close ()//
......
}
Other methods, will httpservletrequest req strong into Multiparthttpservletrequest req after req.getparameter ("id");
HttpServletRequest request;
Multiparthttpservletrequest multipartrequest = (multiparthttpservletrequest) request;
Multipartfile file = multipartrequest.getfile ("file");
String id = multipartrequest.getparameter ("id");
String fileName = File.getoriginalfilename (); .........