Struts uploading photos to the server

Source: Internet
Author: User

 There are probably a lot of people who have already contacted the features of struts uploading photos. I remember that I had done a function of uploading the jar package, which was done with SPRINGMVC, first doing a function similar to uploading, and then interpreting the jar package after adding the jar package, because I want to get the class name, property name and Chinese comment in the jar package. However, there is no way to manually copy all the jar packages to the project, so think about uploading the way to achieve. Now is to do upload photos. Save the user's photo to the server's folder, if it exists, upload it directly, create it if it does not exist, and then upload it.

Front Code:
<pre name= "code" class= "HTML" ><span style= "FONT-SIZE:14PX;" ><form name= "WebForm" method= "post" action= "Personbaseinfo_upload_employee_newemploy.action" enctype= " Multipart/form-data "><table id=" Sreacharea "width=" 100% "border=" 0 "cellspacing=" 1 "cellpadding=" 0 "class=" MODULE_TABLE_BG "><tr><td class=" Content_title_right "rowspan=" 6 "width=" 10% "> Photos: &LT;/TD&GT;&LT;TD class= "Content_content" width= "40%" rowspan= "6" align= "Center" ><input name= "pic" type= "file" style= "Display: None "><input type=" hidden "id =" Sguid "value=" ${personphotoinfo.sguid} "Name=" Personphotoinfo.sguid "/>< Input type= "hidden" id = "Spersonguid" value= "${personphotoinfo.spersonguid}" name= "Personphotoinfo.spersonguid"/ ><a href= "Javascript:void (Sdialog ());" >c:if test= "${personphotoinfo.sfilepath! = null}" ></img></c:if><c:if test= "${personPhotoinfo.sfilepath = = null} "></img></c:if> </a><input type= "Submit" value= "Upload"/></td></tr></table ></form></span>
js function:
<span style= "FONT-SIZE:14PX;" >//Upload Photo response function Sdialog () {var dataForm = document.forms[' WebForm '];d ataForm.pic.click ();} </span>
Background image upload code:
<span style= "FONT-SIZE:14PX;" >/** * Features: File upload * @author gaoying* @return Jump page String * @throws IOException * @throws parseexception */public string UPL Oad () {personphotoinfo = new personphotoinfo (); try {//server path String Serverpath = Servletactioncontext.getservletcon    Text (). Getrealpath ("/");    SYSTEM.OUT.PRINTLN ("Server path:" + Serverpath);    System.out.println ("Print:" + Serverpath + "uploadimage"); Determine if there is a Uploadimage folder on the server, and if not, create if (! ( New file (Serverpath + "Uploadimage"). Isdirectory ()) {//mkdir Create a directory; Mkdirs to create a multi-tier directory new File (serverpath+ "Uploa    Dimage "). mkdir ();       }//The path of the server String Realpath = Servletactioncontext.getservletcontext (). Getrealpath ("/uploadimage");        System.out.println ("Upload to server address Realpath:" +realpath);  Rename file Exfilename = new files (picfilename);    Picfilename is the original file name//system.out.println ("renamed file name:" + renamefile (exfilename));  NewFileName = RenameFile (exfilename); Rename the upload file//specific address + textPart name Filesavepath = realpath+ "\ \" +renamefile (NewFileName);    File saved path name + filename System.out.println ("File saved path name + FileName:" + filesavepath); Column such as------E:\workspace\tomcat\webapps\sems\\005.jpg if (pic! = null) {File savefile = new Fil                      E (New File (Realpath), newfilename.tostring ());            if (!savefile.getparentfile (). exists ()) Savefile.getparentfile (). Mkdirs ();            Fileutils.copyfile (pic, savefile);       Actioncontext.getcontext (). Put ("message", "File Upload succeeded"); }//Save photo information to Database Personphotoinfo.setsfileiname (newfilename.tostring ());//File Store name P  Ersonphotoinfo.setsfilepath (Filesavepath);      File storage Path Personphotoinfo.setsguid (Uuidhexgenerator.getuuid ());  Personphotoinfo.setsfileinitialname (Picfilename);    Upload file formerly known as Personphotoinfo.setsfileextension (picfilename.substring (Picfilename.lastindexof ('. '))); Personphotoinfo.setspersonguid (PersonbaseinFo.getsguid ());        Personnel ID personphotoinfo.setsuploaddate (new Date ()); Personphotoinfomanager.savepersonphotoinformation (personphotoinfo);}  catch (Exception e) {e.printstacktrace (); } return SUCCESS; } </span>
File Rename code:
<span style= "FONT-SIZE:14PX;"  >/** * <p>description: Upload file Rename </p> * @param file file name * @return files * @author       : gaoying * @update       : * @date         : 2015-7-26 */public file renamefile (file file) {String BODY = ""; String ext = "";D ate date = new Date (), int pot = File.getname (). LastIndexOf ("."); if (pot! =-1) {    BODY = date.gettime () + "";    ext = file.getname (). substring (pot);} else{    BODY = (new Date ()). GetTime () + "";    ext = "";} String NewName = body + ext;file = new file (File.getparent (), newName); return file;} </span>

In this way, the entire struts upload image to the server function is complete. In fact, the whole process of important code for a few words, there is the time to upload photos must be careful to rename the file before uploading. Also, the front desk is the browse input upload file and submit button, but my front desk processing let the upload text box hidden, so click on the location of the user photo can be directly uploaded. I wanted to get rid of the submit button, but there was no way to trigger the event. If you have a good idea, welcome to leave a message below, we discuss and exchange together.


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Struts uploading photos to the server

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.