through the first two blog study, presumably everyone has a basic understanding of SPRINGMVC. Today we mainly come to learn about SPRINGMVC two kinds of file upload way.
First of all, the first one, through the way of byte stream to achieve file upload. First create a upload.jsp page
<body>
then configure it in the Spring--servlet.xml configuration file.<!--inject a file upload parser--><bean id= "Multipartresolver" class= " Org.springframework.web.multipart.commons.CommonsMultipartResolver "><!--parser has three properties--><!--encoded-- <property name= "defaultencoding" value= "Utf-8"/>< the maximum value of the!--file--><property name= "maxuploadsize" value= " 10485760000 "/><!--the maximum value of the cache--><property name=" maxinmemorysize "value=" 40960 "/></bean>
Create a new Java class Uploadcontroller.java @Controller @requestmap Ping ("/file") public class uploadcontroller{@RequestMapping ("/upload")// SPRINGMVC give us the definition of the Commonsmultipartfile data type to receive the front end of the file (the name of the front-end filename file)//@RequestParam ("file") and then give it a comment, Tell Springmvc this is the data public String AddUser (@RequestParam ("file") of the file type to receive, Commonsmultipartfile file, HttpServletRequest Request) throws Ioexception{system.out.println ("FileName----------->" + file.getoriginalfilename ());// Then there is an ordinary Java method of reading the file if (!file.isempty ()) {try{//First new one fileoutputstram file output stream Fileoutputstram OS = new Fileoutputstram ("D"/app "+ New Date (). GetTime () + file.getoriginalfilename ());//Then get the input stream of the file Intputstram in = File.getinputstream (); int b = 0;//then a byte-by-byte read file while ((B=in.read ())! =-1) {os.write (b);} Then turn the stream off Os.flush (); Os.close (); In.close ();} catch (FileNotFoundException e) {e.printstacktrace ();}} return "/success";} }
But there are 500 errors when running
What is the cause of this? The reason why the jar package was not introduced is the original. File upload requires a set of jar packages, this is the jar package we originally referenced
We will find the jar package without uploading the download stream. File uploads in addition to the jar packages required to introduce SPRINGMVC, the following jar packages need to be introduced.
Note: Jar Package: http://download.csdn.net/detail/huanjileaimeidan/8245243
http://download.csdn.net/detail/huanjileaimeidan/8245251
***************************************** There should be a split line here **********************************************
File Upload optimization:
Upload a file with the above method is a bit slow, below we introduce another way to upload files, is through the SPRINGMVC packaged parser to upload.
This is an interface, inheriting the HttpServletRequest, the following is the above three methods:
The first method GetFile, directly get a file, through what to get the file, is the name of the JSP page inside, the return value is a multipartfile
The second method is a getfilemap, or it may get a multi-file
The third method is to get a file name method, return an iterator type, get the name of all the file (since it is an iterator, we can iterate over each of the file )。
We later learn SPRINGMVC preferred or This method, relatively fast. OK, let's take a concrete look at how to operate.
First, the created upload.jsp page does not move. Uploadcontroller.java This class to write a new method.
@RequestMapping ("/upload2") public String upload2 (httpservletrequest request,httpservletresponse response) throws IllegalStateException, Ioexception{commonsmultipartresolver multipartresolver = new Commonsmultipartresolver ( Request.getsession (). Getservletcontext ());//Determine if the request contains a multipart type of data if (Multipartresolver.ismultipart ( Request) {//To cast the request to multiparthttpservletrequest type multiparthttpservletrequest multirequest = ( Multiparthttpservletrequest) request;//uses an iterator to determine how many files there are, and a way to traverse the file iterator<string> iter = Multirequest.getfilenames (); while (Iter.hasnext ()) {///The role of an iterator is to take a file and take it to multipartfile file = Multirequest.getfile (String) Iter.next ()) (if (file! = null) {//Get file after output file//First define file name String filename = "Demoupload" + File.getoriginalfilename ();//define the output path string path = "d:/" + filename;//initialization file LocalFile = "New File" (path);// Use STRINGMVC to give us a way to write it to local File.transferto (LocalFile);}} return "/success";}There should be a split line ***********************************************
All right, this is done by uploading the file with the parser that comes with SPRINGMVC. We found through the comparison, upload files through the parser not only the code is much simpler than through the word stream upload, but also the efficiency of uploading much faster. Upload files in two ways, and don't know if you understand. Compare the pros and cons of the two, in the actual project has a choice of trade-offs. Well, today's blog about Springmvc uploading files is written here. Recently in the use of SPRINGMVC+EJB do Itoo project, the combination of SPRINGMVC and EJB feel particularly much, will slowly update the corresponding blog. Welcome everyone in the bottom of the message, and I communicated discussions, we together learn progress!
File upload of "Springmvc"