There are many ways to upload files, before writing an example of using Diskfileitemfactory to implement file uploads, this project used a new way through multiparthttpservletrequ EST to get the file stream, here is one of my demo:
output mode one:
<span style= "White-space:pre" ></span>// Packaging the request Multiparthttpservletrequest multipartrequest = (multiparthttpservletrequest) request;// Gets the Multipartfile file object by name Multipartfile Headfile = Multipartrequest.getfile ("Headfile"); Logger.debug ("-------------- ------------------------------->file operation starting ... ") Logger.debug ("----------------->headfile: "+ Headfile.getname () + "\ttype:" +headfile.getcontenttype () + "\tsize:" +headfile.getsize ());//Get file stream FileInputStream Headin = (FileInputStream) headfile.getinputstream (); FileOutputStream headOut = new FileOutputStream ("D:/headfile.jpeg"); byte[] bytes = new byte[1024];d o{headin.read (bytes , 0,1024); headout.write (bytes);} while (headin.available () > 0); headin.close (); Headout.close (); Logger.debug ("---------------------------------- ----------->file operation success!!! ");
The file is uploaded to the specified directory! used friends may ask, why not use another simpler way? Don't worry, let's look at the output method that Multipartfile comes with. output mode two:
<span style= "White-space:pre" ></span>//the request is packaged multiparthttpservletrequest MultipartRequest = ( Multiparthttpservletrequest) request;//Gets the Multipartfile file object by name Multipartfile Headfile = Multipartrequest.getfile (" Headfile "); Logger.debug ("--------------------------------------------->file operation starting ... "); Logger.debug ("----------------->headfile:" +headfile.getname () + "\ttype:" +headfile.getcontenttype () + "\tsize:" +headfile.getsize ());//Direct output to the execution directory under Headfile.transferto ("D:/little bear.jpeg");
See, the same, the file upload ╮(╯▽╰)╭ is very simple!below we look at Multipartfile this interface source code:
Interface Multipartfile {<span style= "White-space:pre" ></span>public byte[] getBytes (); <span style= " White-space:pre "></span>public String getcontenttype () <span style=" White-space:pre "></span> Public Java.io.InputStream getInputStream (); <span style= "White-space:pre" ></span>public String getName ( ); <span style= "White-space:pre" ></span>public String getoriginalfilename (); <span style= "White-space :p re "></span>public long GetSize (); <span style=" White-space:pre "></span>public boolean IsEmpty (); <span style= "White-space:pre" ></span>public void TransferTo (Java.io.File dest);}
A number of useful methods are defined in the Multipartfile interface.
L, use the GetSize () method to obtain the file length, which determines the file size allowed for uploading.
2, use the IsEmpty () method to determine whether the upload file is an empty file, to determine whether to reject empty files.
3. Use the getInputStream () method to read the file as a Java.io.InputStream stream object.
4. Use the getContentType () method to obtain the file type, which determines which file types are allowed to be uploaded.
5. Use the Transferto (dest) method to write the uploaded file to the file specified on the server.
Using these methods, we can limit the uploaded files, extrapolate not explain, just throw a brick ╮(╯▽╰)╭
The source of the fall: http://blog.csdn.net/cl05300629
Using spring to process file uploads