Struts 1 File Upload

Source: Internet
Author: User

Struts 1 re-encapsulates Apache Commons-fileupload to encapsulate uploaded files as formfile Objects

Define Uploadform:

Private formfilefile;       Upload the file private stringtext;    File notes//omit setter, getter

Jsp:


  

Action to handle the upload operation:

Uploadform uploadform = (uploadform) Form; StringBuffer buffer = new StringBuffer (); if (uploadform.getfile () = null && uploadform.getfile (). GetSize () >0) {//Get folder/web-inf/classes File CLA           sses = new File (GetClass (). getClassLoader (). GetResource (""). GetFile ());      Get folder/upload file Uploadfolder = new file (Classes.getparentfile (). Getparentfile (), "upload");       Uploadfolder.mkdirs ();           Save to/upload folder under File File = new file (Uploadfolder, Uploadform.getfile (). GetFileName ());      Outputstreat ous = null;      InputStream ins = null;             try{byte [] byt = new byte [1024];             int len = 0;             Ins =uploadform.getfile (). getInputStream ();             ous = new FileOutputStream (file);             while (len = Ins.read (byt))!=-1) {ous.write (Byt,0,len);             }}finally{Ins.close ();      Ous.close (); }//Click to download Buffer.append ("File:" + "<a href=upload/>" +fIle.getname () + "target=_blank>" +file.getname () + "</a><br/>");} else{buffer.append ("No file selected! <br/> ");} Buffer.append ("Remarks:" + uploadform.gettext () + "<br/>"), Response.setcharacterencoding ("UTF-8"); Response.getwriter (). Writer (buffer.tostring ());


Struts 1 File Upload

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.