Java Upload file

Source: Internet
Author: User

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

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.