STRUTS2 Basic Learning (vi)-uploading and downloading of documents

Source: Internet
Author: User
<span id="Label3"></p>I. Uploading of documents 1. Single File upload<p><p>Struts2 uses interceptors to complete file uploads, and the underlying use of FileUpload open source Components.</p></p> <p><p>Client Considerations:</p></p> <p><p>(1) method= "<strong>post</strong>"</p></p> <p><p>(2) enctype= "<strong>multipart/form-data</strong>"</p></p> <p><p>(3) <intput type= "<strong>file</strong>" Name= "photo" ></p></p> <p><p></p></p> <p><p>When an action writes a file upload, you need to define three Properties.</p></p> <p><p>(1) file Type filename, The property name matches the name attribute of file in the Form.</p></p> <p><p>(2) string type, Property name: Name Property name + FileName Eg:photofilename.</p></p> <p><p>(3) string type, Property name: Name Property name + ContentType Eg:photocontenttype.</p></p> <p><p>finally, the GetXXX and Setxxx methods are provided for the above three attribute Names. The file is then copied via the Fileutils copyfile method, which uploads the file to the Server.</p></p> <p><p></p></p> <p><p>index.jsp</p></p> <pre><span style="color: #0000ff"><span style="color: #0000ff"><</span></span><span style="color: #800000"><span style="color: #800000">S:form</span></span><span style="color: #ff0000"><span style="color: #ff0000">Action</span></span><span style="color: #0000ff"><span style="color: #0000ff">= "uploadone"</span></span><span style="color: #ff0000"><span style="color: #ff0000">Method</span></span><span style="color: #0000ff"><span style="color: #0000ff">= "post"</span></span><span style="color: #ff0000"><span style="color: #ff0000">enctype</span></span><span style="color: #0000ff"><span style="color: #0000ff">= "multipart/form-data"</span></span><span style="color: #0000ff"><span style="color: #0000ff">></span></span> <span style="color: #0000ff"><span style="color: #0000ff"><</span></span><span style="color: #800000"><span style="color: #800000">S:textfield</span></span><span style="color: #ff0000"><span style="color: #ff0000">name</span></span><span style="color: #0000ff"><span style="color: #0000ff">= "username"</span></span><span style="color: #ff0000"><span style="color: #ff0000">label</span></span><span style="color: #0000ff"><span style="color: #0000ff">= "user name"</span></span><span style="color: #0000ff"><span style="color: #0000ff">/></span></span> <span style="color: #0000ff"><span style="color: #0000ff"><</span></span><span style="color: #800000"><span style="color: #800000">S:file</span></span><span style="color: #ff0000"><span style="color: #ff0000">name</span></span><span style="color: #0000ff"><span style="color: #0000ff">= "photo"</span></span><span style="color: #ff0000"><span style="color: #ff0000">label</span></span><span style="color: #0000ff"><span style="color: #0000ff">= "photo"</span></span><span style="color: #0000ff"><span style="color: #0000ff">/></span></span> <span style="color: #0000ff"><span style="color: #0000ff"><</span></span><span style="color: #800000"><span style="color: #800000">S:submit</span></span><span style="color: #ff0000"><span style="color: #ff0000">value</span></span><span style="color: #0000ff"><span style="color: #0000ff">= "upload"</span></span><span style="color: #0000ff"><span style="color: #0000ff">/></span></span> <span style="color: #0000ff"><span style="color: #0000ff"></</span></span><span style="color: #800000"><span style="color: #800000">S:form</span></span><span style="color: #0000ff"><span style="color: #0000ff">></span></span></pre><p><p>Uploadaction.java</p></p><pre class="brush: java; auto-links: true; collapse: false; first-line: 1; gutter: true; html-script: false; light: false; ruler: false; smart-tabs: true; tab-size: 4; toolbar: true;">public class Uploadaction extends Actionsupport{private string username;private File photo;private string photofilename ;//upload file name private string photocontenttype;//the MIME type of uploaded files public string Uploadone () throws ioexception{ System.out.println ("username =" + username + "\nphotoname =" + photofilename + "\nphotocontenttype =" + photocontenttype );//upload string storedir = Servletactioncontext.getservletcontext (). getrealpath ("/upload"); Fileutils.copyfile (photo,new File (storedir,photofilename)); return SUCCESS;} Public String getphotofilename () {return photofilename;} public void Setphotofilename (String Photofilename) {this.photofilename = photofilename;} Public String getphotocontenttype () {return photocontenttype;} public void Setphotocontenttype (String Photocontenttype) {this.photocontenttype = photocontenttype;} Public String getusername () {return username;} public void Setusername (String Username) {this.username = username;} Public File Getphoto () {return photo;} public void Setphoto (File photo) {this.photo = photo;}} </pre><p><p>Results:</p></p><p><p></p></p><p><p></p></p>2. Problems with file uploads<p><p>The total file upload size by default is 2M, if more than 2M, the program will report an exception, you can use <s:actionError> to view specific Information.</p></p><p><p><strong>(1) fix the total size setting, find the constant</strong></p></p><p><p>Struts.multipart.parser=jakarta default File Upload parser, which is the FileUpload component<br>Temporary file storage directory for struts.multipart.savedir= file upload<br>struts.multipart.maxsize=2097152 file Upload maximum value (total size), default is 2M<br>Set constants in Struts.xml to modify the default total size of file uploads</p></p><pre class="brush: xml; auto-links: true; collapse: false; first-line: 1; gutter: true; html-script: false; light: false; ruler: false; smart-tabs: true; tab-size: 4; toolbar: true;"><pre class="brush: xml; auto-links: true; collapse: false; first-line: 1; gutter: true; html-script: false; light: false; ruler: false; smart-tabs: true; tab-size: 4; toolbar: true;"><!--configure the size of the upload file--><constant name= "struts.multipart.maxSize" value= "5000000"/></pre></pre><p><p></p></p><p><p><strong>(2) You can also configure the interceptor to set up file upload</strong></p></p><pre class="brush: xml; auto-links: true; collapse: false; first-line: 1; gutter: true; html-script: false; light: false; ruler: false; smart-tabs: true; tab-size: 4; toolbar: true;"><pre class="brush: xml; auto-links: true; collapse: false; first-line: 1; gutter: true; html-script: false; light: false; ruler: false; smart-tabs: true; tab-size: 4; toolbar: true;"><action name= "uploadone" class= "com.kiwi.action.UploadAction" method= "uploadone" ><interceptor-ref name= " FileUpload "><!--configuration allows uploading of file types, multiple with", "separating--><param name=" allowedtypes ">image/bmp,image/png,image/gif, Image/jpeg,image/jpg,image/x-png,image/pjpeg</param><!--configure the file size allowed to upload, per Byte--><param name= " MaximumSize ">102400</param></interceptor-ref><interceptor-ref name=" DefaultStack "/>< Result Name= "success" >/success.jsp</result></action></pre></pre><p><p></p></p><p><p><strong>(3) Modify the resource file information that displays the error message</strong></p></p><p><p>The first step: create a new resource file such as message_zh_cn.properties, placed under SRC to add the following information in the resource file:</p></p><p><p>Struts.messages.error.uploading= upload error: {0}</p></p><p><p>struts.messages.error.file.too.large= upload file is too large: {0} "{1}" "{2}" {3}</p></p><p><p>struts.messages.error.content.type.not.allowed= upload file type not allowed: {0} "{1}" "{2}" {3}</p></p><p><p>struts.messages.error.file.extension.not.allowed= upload file suffix: {0} ' {1} ' ' {2} ' {3}</p></p><p><p>Step two: Load the resource file in the Struts.xml file</p></p><pre class="brush: xml; auto-links: true; collapse: false; first-line: 1; gutter: true; html-script: false; light: false; ruler: false; smart-tabs: true; tab-size: 4; toolbar: true;"><pre class="brush: xml; auto-links: true; collapse: false; first-line: 1; gutter: true; html-script: false; light: false; ruler: false; smart-tabs: true; tab-size: 4; toolbar: true;"><constant name= "struts.custom.i18n.resources" value= "message"/></pre></pre><p><p></p></p>3. Multi-file Upload<p><p>On the JSP page to provide multiple sets of file upload input box, select multiple file Upload.</p></p><p><p>index.jsp</p></p> <pre><span style="color: #0000ff"><span style="color: #0000ff"><</span></span><span style="color: #800000"><span style="color: #800000">S:form</span></span><span style="color: #ff0000"><span style="color: #ff0000">Action</span></span><span style="color: #0000ff"><span style="color: #0000ff">= "uploadone"</span></span><span style="color: #ff0000"><span style="color: #ff0000">Method</span></span><span style="color: #0000ff"><span style="color: #0000ff">= "post"</span></span><span style="color: #ff0000"><span style="color: #ff0000">enctype</span></span><span style="color: #0000ff"><span style="color: #0000ff">= "multipart/form-data"</span></span><span style="color: #0000ff"><span style="color: #0000ff">></span></span> <span style="color: #0000ff"><span style="color: #0000ff"><</span></span><span style="color: #800000"><span style="color: #800000">S:textfield</span></span><span style="color: #ff0000"><span style="color: #ff0000">name</span></span><span style="color: #0000ff"><span style="color: #0000ff">= "username"</span></span><span style="color: #ff0000"><span style="color: #ff0000">label</span></span><span style="color: #0000ff"><span style="color: #0000ff">= "user name"</span></span><span style="color: #0000ff"><span style="color: #0000ff">/></span></span> <span style="color: #0000ff"><span style="color: #0000ff"><</span></span><span style="color: #800000"><span style="color: #800000">S:file</span></span><span style="color: #ff0000"><span style="color: #ff0000">name</span></span><span style="color: #0000ff"><span style="color: #0000ff">= "photo"</span></span><span style="color: #ff0000"><span style="color: #ff0000">label</span></span><span style="color: #0000ff"><span style="color: #0000ff">= "photo 1"</span></span><span style="color: #0000ff"><span style="color: #0000ff">/></span></span> <span style="color: #0000ff"><span style="color: #0000ff"><</span></span><span style="color: #800000"><span style="color: #800000">S:file</span></span><span style="color: #ff0000"><span style="color: #ff0000">name</span></span><span style="color: #0000ff"><span style="color: #0000ff">= "photo"</span></span><span style="color: #ff0000"><span style="color: #ff0000">label</span></span><span style="color: #0000ff"><span style="color: #0000ff">= "photo 2"</span></span><span style="color: #0000ff"><span style="color: #0000ff">/></span></span> <span style="color: #0000ff"><span style="color: #0000ff"><</span></span><span style="color: #800000"><span style="color: #800000">S:file</span></span><span style="color: #ff0000"><span style="color: #ff0000">name</span></span><span style="color: #0000ff"><span style="color: #0000ff">= "photo"</span></span><span style="color: #ff0000"><span style="color: #ff0000">label</span></span><span style="color: #0000ff"><span style="color: #0000ff">= "photo 3"</span></span><span style="color: #0000ff"><span style="color: #0000ff">/></span></span> <span style="color: #0000ff"><span style="color: #0000ff"><</span></span><span style="color: #800000"><span style="color: #800000">S:submit</span></span><span style="color: #ff0000"><span style="color: #ff0000">value</span></span><span style="color: #0000ff"><span style="color: #0000ff">= "upload"</span></span><span style="color: #0000ff"><span style="color: #0000ff">/></span></span> <span style="color: #0000ff"><span style="color: #0000ff"></</span></span><span style="color: #800000"><span style="color: #800000">S:form</span></span><span style="color: #0000ff"><span style="color: #0000ff">></span></span></pre><p><p>Uploadaction.java</p></p><pre class="brush: java; auto-links: true; collapse: false; first-line: 1; gutter: true; html-script: false; light: false; ruler: false; smart-tabs: true; tab-size: 4; toolbar: true;">public class UploadAction2 extends Actionsupport{private String username;private file[] photo;private string[] photofilename;//uploaded file name private string[] photocontenttype;//uploaded files MIME type public String uploadone () throws ioexception{/ /upload string storedir = Servletactioncontext.getservletcontext (). getrealpath ("/upload"); If (photo! = null && Photo.length > 0) {for (int i = 0;i < Photo.length;i++) {fileutils.copyfile (photo[i],new File (storedir, photofilename[i]));}} Return SUCCESS;} Public String getusername () {return username;} public void Setusername (String Username) {this.username = username;} Public file[] Getphoto () {return photo;} public void Setphoto (file[] photo) {this.photo = photo;} Public string[] getphotofilename () {return photofilename;} public void Setphotofilename (string[] Photofilename) {this.photofilename = photofilename;} Public string[] getphotocontenttype () {return photocontenttype;} public void Setphotocontenttype (string[] Photocontenttype) {this.photocontenttype = Photocontenttype;}} </pre><p><p> Struts2 Basic Learning (vi)-uploading and downloading of files </p> </p></span>

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.