Struts has a default file blocker, and the general configuration MaximumSize is available. Knowing the principle, we can write a class that inherits it, implementing its own configuration to upload the file size in a way. Then scrutiny the page to upload the file, found some problems. Three parameters are required in the action configuration: File uploadfile,string uploadfilefilename, String uploadfilecontenttype However, there is really no configuration for the next two parameters on the page, A control that has only one file. Finally, the completion of the upload, when the two properties are stored in the value, it is unclear, only look at the source code, find the answer. Fileuploadinterceptor This interceptor is required to configure the file upload Interceptor, inside the interception method inside, there is a intercept () method, responsible for file upload interception. The study of the code can be seen, the upload file is mainly the following request in effect:multipartrequestwrapper multiwrapper = (multipartrequestwrapper) request;Enumeration fileparameternames = Multiwrapper.getfileparameternames ();file[] files = multiwrapper.getfiles (InputName);if (Files! = null && files.length > 0) { list<file> acceptedfiles = new ArrayList (files.length); list<string> acceptedcontenttypes = new ArrayList (files.length); list<string> acceptedfilenames = new ArrayList (files.length); String contenttypename = inputname + "ContentType"; String filenamename = inputname + "FileName"; For (int index = 0; index < files.length; ++index) { If (this. Acceptfile (Action, Files[index], Filename[index], Contenttype[index], InputName, Validation)) { Acceptedfiles.add (Files[index]);Acceptedcontenttypes.add (Contenttype[index]);Acceptedfilenames.add (Filename[index]); } } If (!acceptedfiles.isempty ()) { map<string, object> params = Ac.getparameters (); params.put (InputName, Acceptedfiles.toarray (new file[acceptedfiles.size () ));params.put (ContentTypeName, Acceptedcontenttypes.toarray (new string[acceptedcontenttypes.size () )); params.put (Filenamename, Acceptedfilenames.toarray (new string[acceptedfilenames.size () )); }}above the yellow bottom is why the action of UploadFile, Uploadfilefilename, Uploadfilecontenttype will be assigned value. Because, when the request is by default the file name attribute of our form form, and then the corresponding file name, content information, and so on to the file name related variables, are the default plus "ContentType" "filename", so the above problem is solved. at this point, why does the information in the getparameters in actioncontext be stored in the corresponding setter, getter entry for the action? after a period of root-searching, we see an interceptor, Parametersinterceptor (there is also a excludeparams parameter to filter the parameters that do not need to be assigned, and the order parameter to initialize the values in the action in order). This class has the following description:* This interceptor sets all parameters on the value stack.*typically resulting in the values submitted in a form* Request being applied to a action in the value stack.As can be seen from the description, the information submitted by the form can be obtained through this interceptor, and all parameters in the same Actioncontext GetParameters method will be assigned. At this time, if scrutiny, why the value in the Valuestack will be Setter/getter method know and assign value? --The final parameter assignment is the SetValue method of the called Valuestack, which is used internally by the OGNL expression engine, although the interior is very complex, but we only need to know when the OGNL expression engine sets the request parameter to Valuestack , is from the stack top to the bottom of the stack to find the object of the corresponding setter method, if the parameter being assigned to the Valuestack found an object has setter method to assign the value of the parameter to the object, if not found, continue to look at the bottom of the stack until it is found, If the bottom of the stack is found or not found, there is no successful assignment. The following is the description in Valuestack, and when an El expression is encountered, the corresponding Getter/setter method is found to obtain the corresponding value. concrete deeper realization, still not clear, continue to look. (The Great God can guide)* Valuestack allows multiple beans to being pushed in and dynamic EL expressions to be evaluated against it. When evaluating an expression, the stack is searched down the stack, from the latest objects pushed on to the earlies T, looking for a bean with a getter or setter for the given property or a method of the given name (depending on the Expre Ssion being evaluated).
Struts File upload blocker analysis