This article turns from: http://amcucn.iteye.com/blog/264457. Thanks to the author
Recently in the work, need to use to upload images of the function, but they usually learn only use struts upload function, but because the project did not use struts, but spring MVC, and finally had to find another way. Through Google and Baidu, see a number of relevant introduction. After their own trial, finally take advantage of the spring MVC upload files to complete the image upload. If you just want to upload a picture, you can do so by restricting the extension to the name. The following is a key part of the code.
About the configuration of spring I'm not here to introduce you, the following method is based on the spring MVC pattern.
Front desk section:
Actually the foreground part is very simple, is the common form form file upload form to be possible. Such as:
HTML code
- <form action= "comm_addproduct.do?method=saveproduct" < span class= "attribute" >method= "post" enctype= "Multipart/form-data" >
- <input name= "imgfile" id=< Span class= "Attribute-value" > "imgfile" type= "file" />
- </form>
The key point here is that the enctype= "Multipart/form-data" attribute must be in the form form. As for why this attribute is needed, I think it is clear to those who have done the file upload. I'm not going to say much. Do not search for a bit! The front desk here is very simple, just a demo.
After the current form is submitted to the background, we focus on how the background handles the uploaded files. The key code is as follows:
Java code
- /**
- * Upload the image file and save it to the specified path
- */
- public void AddImage (httpservletrequest request,string path1,string path2) {
- //transition to Multiparthttprequest (where the focus is)
- Multiparthttpservletrequest multipartrequest = (multiparthttpservletrequest) request;
- //Get the 1th picture (file uploaded according to the name of the foreground)
- Multipartfile imgFile1 = Multipartrequest.getfile ("Imgfile");
- //Define an array to hold the file types that can be uploaded
- List filetypes = new ArrayList ();
- Filetypes.add ("JPG");
- Filetypes.add ("JPEG");
- Filetypes.add ("BMP");
- Filetypes.add ("gif");
- //Save first picture
- if (! ( Imgfile1.getoriginalfilename () = =Null | | " . Equals (Imgfile1.getoriginalfilename ()))) {
- /* The method that is called below is primarily used to detect if the uploaded file is within the allowed type range, and according to the path name passed in
- * Automatically create folders and file names, return file files we can use to do other uses, such as to get the file name path after saving
- * I'm not going to do a long introduction here.
- */
- File file1 = this.getfile (imgFile1, filetypes,path1,path2);
- }
- }
- <p>
- </p><p> in fact, the above code is relatively simple, the focus is to convert our common request object to <span style="WHITE-SPACE:PRE;" >multiparthttprequest object, with this object, we can get the user uploaded files. Get the user uploaded files after,</span></p>
- <p><span style="WHITE-SPACE:PRE;" > We can do something we want to do. In the above we have also done some things, that is to determine whether the user upload file type is our definition of the </span></p>
- <p> the type within the array, as to how to determine if it is a type that is allowed to upload, I will give it in the following method. In fact, the following code can be written in a method, but for reuse, I have written separately. Perhaps my approach is not the best. That's the equivalent of giving everyone a direction! </p>
- <p> </p>
- <p> Let's take a look at the following two methods, which are two of the most important things to do. First, to determine whether the user uploaded files are within the scope of our defined types, and second, save the file to the specified path, this path is created by ourselves. </p>
- <p>
- </p><pre name="code" class="java" >/**
- * Read the file through the incoming page, save to the local disk after processing, and return an already created file
- * @param imgfile files read from the page
- * @param the category name of the TypeName product
- * @param brand name of brandname product
- * @param filetypes allowed file name extensions collection
- * @return
- */
- private File getFile (multipartfile imgfile,string typename,string brandname,list filetypes) {
- String fileName = Imgfile.getoriginalfilename ();
- //Get the extension of the upload file type, first get the location, then intercept from the next position of the. To the end of the file, and finally get the extension
- String ext = filename.substring (Filename.lastindexof (".") +1,filename.length ());
- //Lowercase conversions for extensions
- ext = Ext.tolowercase ();
- File file = null;
- If (filetypes.contains (EXT)) { //If the extension belongs to a type that is allowed to be uploaded, the file is created
- File = This.creatfolder (typeName, brandname, fileName);
- try {
- Imgfile.transferto (file); //Save the uploaded file
- } catch (IllegalStateException e) {
- E.printstacktrace ();
- } catch (IOException e) {
- E.printstacktrace ();
- }
- }
- return file;
- }
- /**
- * Detect and create a level, Level two folder, file name
- Here I am using the two strings passed in to do the first level folder and the level two folder name
- In this way we can do it according to the user's choice to save to the appropriate folder
- */
- private File Creatfolder (String typename,string brandname,string fileName) {
- File file = null;
- TypeName = Typename.replaceall ("/", "" "); //Remove "/"
- TypeName = Typename.replaceall ("", ""); //Replace half-width spaces
- TypeName = Typename.replaceall ("", ""); //Replace full-width spaces
- brandname = Brandname.replaceall ("/", "" "); //Remove "/"
- brandname = Brandname.replaceall ("", ""); //Replace half-width spaces
- brandname = Brandname.replaceall ("", ""); //Replace full-width spaces
- File Firstfolder = new File ("c:/" + typeName); //First-level folders
- If (Firstfolder.exists ()) { //If the first-level folder exists, the level Two folder is detected
- File Secondfolder = new File (Firstfolder,brandname);
- If (Secondfolder.exists ()) { //If the Level two folder also exists, create the file
- FILE = new file (Secondfolder,filename);
- }else { //If the Level two folder does not exist, create a Level two folder
- Secondfolder.mkdir ();
- FILE = new file (Secondfolder,filename); //After you create a level two folder, then close the file
- }
- }else { //If the level does not exist, create a level folder
- Firstfolder.mkdir ();
- File Secondfolder = new File (Firstfolder,brandname);
- If (Secondfolder.exists ()) { //If the Level two folder also exists, create the file
- FILE = new file (Secondfolder,filename);
- }else { //If the Level two folder does not exist, create a Level two folder
- Secondfolder.mkdir ();
- FILE = new file (Secondfolder,filename);
- }
- }
- return file;
- }</pre>
- <p>
- </p><p> The above code basically implements the function we want. Of course, there are still many problems, the current has only reached a preliminary function, and did not write very rigorous. I'll cover the following articles about how to use JavaScript to implement thumbnails in the foreground! </p>
- <p> </p>
- <p> </p>
- <p> </p>
- <p> </p>
Uploading image files using spring MVC