I want to upload an image today. I heard that struts2 is used to upload the image. I have been reading the document and tested the common-fileupload experiment successfully. Record it:
The dopost () method in servlet is pasted as follows:
/** General steps: * 1. prepare the fileitem factory; * 2. hand over the factory to servletfileupload and ask him to parse the front-end form entries; * 3. process and parse each specific item in the returned list */@ suppresswarnings ("unchecked") @ overrideprotected void dopost (httpservletrequest request, httpservletresponse response) throws servletexception, ioexception {/* fileitemfactory is the fileitem factory, but it is only an interface. * two implementations are diskfileitemfactory and defaultfileitemfactory. * But the API says: "deprecated. use diskfileitemfactory in Stead. "*/fileitemfactory itemfactory = new diskfileitemfactory ();/* The fileupload class is used to process file uploads, while the servletfileupload class is its subclass, * You can determine and parse the front-end form type and form item information */servletfileupload upload = new servletfileupload (itemfactory); List <fileitem> List = NULL; try {// parserequest parse the form items in the front end into the fileitem list = upload. parserequest (request); iterator <fileitem> it = List. iterator (); While (it. hasnext () {/** the core is fileitem. If the form attribute is Nctype = multipart/form-data, * It will be used to describe every common form item and upload item of the Form in the POST request; * it can be considered that each input corresponds to a fileitem; you can see the encapsulation of fileitem attributes (added to the client ): * User <input type = "text" name = "username"> <br> // common form item * image 1 <input type = "file" name = "img1"/> <br/> // upload item * 1. getcontenttype () // Normal Form item null. the upload item must be of the JPG type, JPEG is image/JPEG, PNG is image/PNG, and GIF is image/GIF: * 2. getfieldname () // name = "username" and name = "img1"; * 3. getstring () // is the user name entered by the user, as shown in the form Enter "root", and then getstring () will get root; * 3. getinputstream () // determined based on the output: the normal item is of the bytearrayinputstream type, and the upload item is of the fileinputstream type * 4. getname () // The Normal Form item is null. the upload item is the file name after the directory name is uploaded, for example, "Export tubackground.jpg" * 5. getoutputstream () // output stream, not deeply viewed * 6. getsize () // size: the number of bytes that the user inputs and the size of the uploaded file. * 7. isformfield () // This is to determine whether it is a common item [True] or an upload item [false]; */fileitem item = it. next ();/* filter, only the image type in the form item is saved */If ("image/JPEG ". equals (item. getcontenttype () | "Image/GIF ". equals (item. getcontenttype () | "image/PNG ". equals (item. getcontenttype () {inputstream instream = item. getinputstream (); byte [] B = new byte [1024]; int RB = instream. read (B); file = new file ("/home/Nie/images/", item. getname (); outputstream outstream = new fileoutputstream (File); While (rb! =-1) {outstream. write (B); RB = instream. read (B);} outstream. flush (); instream. close (); outstream. close () ;}} catch (fileuploadexception e) {e. printstacktrace ();}}
To sum up, there are three important things:
1. fileitemfactory [diskfileitemfactory ];
2. fileupload [servletfileupload ];
3. fileitem
Then I/O can be used;