Struts2 Learning (struts2) file upload and download (1)

Source: Internet
Author: User

One, Struts2 file upload:

Second, the size of the configuration file and the type of files allowed to upload:

Three, large file upload:

If you do not configure the size of the upload file, struts2 default allows uploading files up to 2M; 2097152Byte; example implementation:

Com.cy.action.FileUploadAction.java:

 Packagecom.cy.action;ImportJava.io.File;Importorg.apache.commons.io.FileUtils;ImportCom.opensymphony.xwork2.ActionSupport; Public classFileuploadactionextendsactionsupport{Private Static Final LongSerialversionuid = 1L; PrivateFile struts;//file, corresponding to the name of input type=file in fileupload.jsp    PrivateString Strutsfilename;//file name    PrivateString Strutscontenttype;//File Type         PublicFile getstruts () {returnstruts; }     Public voidsetstruts (File struts) { This. Struts =struts; }     PublicString Getstrutsfilename () {returnStrutsfilename; }     Public voidsetstrutsfilename (String strutsfilename) { This. Strutsfilename =Strutsfilename; }     PublicString Getstrutscontenttype () {returnStrutscontenttype; }     Public voidSetstrutscontenttype (String strutscontenttype) { This. Strutscontenttype =Strutscontenttype; } @Override PublicString Execute ()throwsException {System.out.println ("File name:" +strutsfilename); System.out.println ("File type:" +Strutscontenttype); String FilePath= "e:/" +Strutsfilename; File SaveFile=NewFile (FilePath);        Fileutils.copyfile (struts, savefile); returnSUCCESS; }        }

Struts.xml:

<?XML version= "1.0" encoding= "UTF-8"?><!DOCTYPE struts Public "-//apache software foundation//dtd struts Configuration 2.0//en" "Http://struts.apache.or G/dtds/struts-2.0.dtd "><Struts>        <!--configuration allows uploading files up to 20000000Byte, approx. 20Mb -    <constantname= "struts.multipart.maxSize"value= "20000000"></constant>        < Packagename= "Manage"extends= "Struts-default">        <Actionname= "Upload"class= "Com.cy.action.FileUploadAction">            <resultname= "Input">fileupload.jsp</result>            <resultname= "Success">success.jsp</result>                        <!--Configure the file types allowed to upload the file size allowed to upload, maximum 81101byte,= 79.2KB -            <Interceptor-refname= "FileUpload">                <paramname="Allowedtypes" >Image/bmp,image/x-png,image/gif,image/jpg,image/jpeg</param>                <paramname="MaximumSize" >81101</param>            </Interceptor-ref>                        <Interceptor-refname= "Defaultstack"></Interceptor-ref>        </Action>            </ Package>    </Struts>

FILEUPLOAD.JSP:

<%@ Page Language="Java"ContentType="text/html; Charset=utf-8"pageencoding="UTF-8"%><%@taglib prefix="s"URI="/struts-tags" %><!DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd "><HTML><Head><Metahttp-equiv= "Content-type"content= "text/html; charset=utf-8"><title>Insert Title here</title></Head><Body>    <!--Failure message is returned when a file upload error is displayed -    <S:fielderror></S:fielderror>    <formAction= "Upload"Method= "POST"enctype= "Multipart/form-data">Select File:<inputtype= "File"name= "Struts" /><BR/>        <inputtype= "Submit"value= "Submit" />    </form></Body></HTML>

SUCCESS.JSP:

< Body > File Upload Successful!  </body>
View Code

Test results:

1) Configure the types of files that are allowed to be uploaded, and the file size;

2) configuration of large file upload, the above example is configured to about 20M;

If you do not configure a large file upload, upload more than 2M will be an error:

------------

Struts2 Learning (struts2) file upload and download (1)

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.