User avatar uploads are uploaded using struts, so you need to follow the way struts uploads files.
Basic steps: 1. To upload a file using an HTML form in a JSP, set the Enctype property of the HTML form to Multipart/form-data and the method property to post. The HTML form contains the <input type= "file" > enables the user to choose to upload the file.
2. Action to add three file upload related properties. Upload a single file property format Attribute one: Type is file, name is page file control name Attribute two: Type is string, name is page file control name +contenttype Attribute three: Type is string, name is page file control name +filename
3. Processing of uploaded files |
1. JSP page
addui.jsp Key Parts
<input type= "File" name= "Headimg"/>
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M00/85/9E/wKioL1eqjKnS-EABAABMe1oKQkw129.png "title=" Addui_ Headimg.png "alt=" Wkiol1eqjkns-eabaabme1okqkw129.png "/>
editui.jsp Key Parts
<s:if test= "User.headimg! = null && user.headimg! =" "> "width=" "height=" "/> <s:hidden name=" user.headimg "/></s:if><" Input type= "file" name= "Headimg"/>
650) this.width=650; "src=" Http://s4.51cto.com/wyfs02/M01/85/9E/wKiom1eqjOTwr5VXAACZaGPRKqE313.png "title=" Editui _headimg.png "alt=" Wkiom1eqjotwr5vxaaczagprkqe313.png "/>
The place to pay attention to here is: in the editing page (editui.jsp), to pay attention to the user's avatar display, so the <s:if> judgment; at the same time, to be aware of the user's avatar address <s:hidden> hidden, Otherwise, the original avatar address will be lost when you save again.
2, Useraction processing
2.1. Add field and property
Private file Headimg;private string headimgcontenttype;private string headimgfilename;public File getheadimg () {return headimg;} public void setheadimg (File headimg) {this.headimg = headimg;} Public String Getheadimgcontenttype () {return headimgcontenttype;} public void Setheadimgcontenttype (String headimgcontenttype) {this.headimgcontenttype = Headimgcontenttype;} Public String Getheadimgfilename () {return headimgfilename;} public void Setheadimgfilename (String headimgfilename) {this.headimgfilename = Headimgfilename;}
2.2. Modify the Add and edit methods
Save New Public string add () {try {if (user != null) {if (headimg != null) {///1, Save Avatar to Upload/userstring filepath = servletactioncontext.getservletcontext (). GetRealPath ("upload/ user/"); String filename = uuid.randomuuid (). toString (). ReplaceAll ("-", "") + Headimgfilename.substring (Headimgfilename.lastindexof (".")); Fileutils.copyfile (Headimg, new file (Filepath,filename));//2, set user Avatar path User.setheadimg ("user/" + filename); }userservice.save (user);}} catch (exception e) {e.printstacktrace ();} return "List";} Save Edit Public string edit () {try {if (user != null) {if (headimg != null) {///1, Save Avatar to Upload/userstring filepath = servletactioncontext.getservletcontext (). GetRealPath ("upload/ user/"); String filename = uuid.randomuuid (). toString (). ReplaceAll ("-", "") + Headimgfilename.substring (Headimgfilename.lastindexOf (".")); Fileutils.copyfile (Headimg, new file (Filepath,filename));//2, set user Avatar path User.setheadimg ("user/" + filename); }userservice.update (user);}} catch (exception e) {e.printstacktrace ();} return "List";}
SSH series: (10) User Management-user avatar Upload