Action
/** Back-end merchandise management action*/ Public classAdminproductactionextendsActionsupportImplementsModeldriven<product> { //model-driven objects to be used PrivateProduct product=NewProduct (); PublicProduct Getmodel () {returnproduct; }//Required parameters for file upload PrivateFile upload;//upload the file to match the name in the JSP PrivateString Uploadfilename;//the file name of the upload to receive files PrivateString Uploadcontexttype;//The MIME type of the file that received the file upload //set//How to save a product PublicString Save ()throwsioexception{//Call service to complete the saved OperationDate date=NULL; Product.setpdate (date); if(Upload! =NULL){ //get the absolute path to the disk on the fileString Realpath=servletactioncontext.getservletcontext (). Getrealpath ("/products"); //Create a fileFile diskfile=NewFile (realpath+ "//" +uploadfilename); //File Uploadfileutils.copyfile (upload, diskfile); Product.setimage ("products/" +uploadfilename); } //saving data to a databaseProductservice.save (product); //page Jump return"Savesuccess"; } }
Jsp
<form id= "Useraction_save_do" name= "Form1" action= "${pagecontext.request.contextpath}/adminproduct_save.action " method=" post "enctype=" Multipart/form-data"class=" ta_01 "> product Image: </td>
class= "ta_01" bgcolor= "#ffffff" colspan= "3" > <input type= "file" name= " upload"/> </td > </tr></form>
Java Upload file