In the article "in-depth springmvc------File Upload Source analysis (the previous)", introduced Springmvc file upload related. In this article, we will further introduce the efficiency of SPRINGMVC uploading files.
Believe that most people in the process of file upload logic will directly get the input stream directly to operate, pseudo-code similar to this:
@RequestMapping (value = "/upload", method = requestmethod.post) public resultview upload (@ Requestparam ("file") multipartfile file) { = file.getinputstream (); ... }
However, for efficiency, I personally recommend using Multipartfile's TransferTo method, which is similar to the following:
@RequestMapping (value = "/upload", method = requestmethod.post) public resultview upload (@ Requestparam ("file") multipartfile file) { File.transferto (new file (destfile)); ... }
Why is it? This has to start from the source, nonsense not much to say, we go directly to the source bar:
1. First look at the getInputStream method of Multipartfile (its implementation class Commonsmultipartfile):
Commonsmultipartfile:
public inputstream getinputstream () IOException { if (!isavailable ()) { throw new Il Legalstateexception ("File has been Moved-cannot is read again" ); } inputstream InputStream = this .fileite M.getinputstream (); return (inputstream! = null ? InputStream: new bytearrayinputstream (new byte [0
Through the source can see, spring is through Commons-fileupload in the Fileitem object to get the input stream, then go to see Fileitem (Its implementation class Diskfileitem) corresponding method:
Diskfileitem:
public InputStream getInputStream ()
throws IOException { if (! IsInMemory ()) { return new
FileInputStream (Dfos.getfile ());
if (cachedcontent = = null = Dfos.getdata (); return new Bytearrayinputstream (cachedcontent); }
The source can be seen: first to see if it exists in memory, if present, the file object in memory wrapped as a stream, if it does not exist, then go to the cache, if the cache exists, get the byte array from the cache and wrap it as an input stream.
Next, let's look at the TransferTo method of commonsmultipartfile in order to form a comparison:
Commonsmultipartfile:
@Override Public voidTransferTo (File dest)throwsIOException, illegalstateexception {if(!isavailable ()) { Throw NewIllegalStateException ("File has already been moved-cannot be transferred again"); } if(Dest.exists () &&!Dest.delete ()) { Throw NewIOException ("Destination file [" + Dest.getabsolutepath () + "] already exists and could not being deleted"); } Try { This. Fileitem.write (dest); if(logger.isdebugenabled ()) {String Action= "Transferred"; if(! This. Fileitem.isinmemory ()) {Action= IsAvailable ()? "Copied": "Moved"; } logger.debug ("Multipart file" + getName () + "' with original filename [" +Getoriginalfilename ()+ "], stored" + getstoragedescription () + ":" +Action+ "to [" + Dest.getabsolutepath () + "]"); } } Catch(Fileuploadexception ex) {Throw Newillegalstateexception (Ex.getmessage ()); } Catch(IOException ex) {Throwex; } Catch(Exception ex) {Logger.error ("Could not transfer to file", ex); Throw NewIOException ("Could not transfer to file:" +ex.getmessage ()); } }
Not much to say, mainly see This.fileItem.write (dest) This sentence, the use of commons-fileupload in the relevant methods:
Diskfileitem:
Public voidWrite (file file)throwsException {if(IsInMemory ()) {FileOutputStream Fout=NULL; Try{fout=Newfileoutputstream (file); Fout.write (Get ()); } finally { if(Fout! =NULL) {fout.close (); } } } Else{File OutputFile=getstorelocation (); if(OutputFile! =NULL) { //Save The length of the fileSize =outputfile.length (); .....
Through the source can see the Transfoto method is very neat, go directly to the in-memory files through the output stream out to the specified file. Wait, is it a bit of a step compared to the getInputStream method above? Yes, one more diagram that clearly shows the difference between the two methods:
In the diagram:
The red line means that the Transferto method is used, the black line represents the getInputStream method, and the Transferto directly writes the in-memory file cache directly to the physical file of the disk. The getInputStream method relays once (the stream is fetched from memory by getInputStream and then output to the disk physical file via OutputStream). In comparison, even from the steps, you can see that the transferto is more efficient.
Well, this is the end of this article!
In-depth SPRINGMVC source code------File Upload Source analysis (the next article)