STRUTS2 Study Note ten: File upload

Source: Internet
Author: User

Struts2 upload 1, Struts2 by default Apache commons-fileupload2, STRUTS2 support three types of upload components 3, need to introduce commons-fileupload dependent package * commons-io-1.3.2.jar* COMMONS-FILEUPLOAD-1.2.1.JAR4, the form needs to use the Post submission method, the encoding type needs to be used: MULTIPART/FORM-DATA5, Struts2 action Gets the file name->> rule: Enter the name of the domain + fixed string filename Gets the file data->> rule: Name of the file input field gets the content type->> rule: Enter the name of the domain + Fixed string ContentType6, get input stream, write file with output stream action class
 Packagecom.djoker.struts2;ImportJava.io.BufferedInputStream;ImportJava.io.BufferedOutputStream;ImportJava.io.File;ImportJava.io.FileInputStream;ImportJava.io.FileOutputStream;ImportJava.io.InputStream;ImportJava.io.OutputStream;ImportOrg.apache.struts2.ServletActionContext;Importcom.opensymphony.xwork2.Action; Public classuploadaction {PrivateString Myfilefilename; PrivateFile MyFile; PrivateString Desccontexttype;  PublicString Getmyfilefilename () {returnMyfilefilename; }     Public voidsetmyfilefilename (String myfilefilename) { This. Myfilefilename =Myfilefilename; }     PublicFile Getmyfile () {returnMyFile; }     Public voidsetmyfile (File myFile) { This. MyFile =MyFile; }     PublicString Getdesccontexttype () {returnDesccontexttype; }     Public voidSetdesccontexttype (String desccontexttype) { This. Desccontexttype =Desccontexttype; }         PublicString Execute ()throwsException {System.out.println (myfilefilename); InputStream is=NULL; OutputStream OS=NULL; Try{ is=NewBufferedinputstream (NewFileInputStream (myFile)); OS=NewBufferedoutputstream (NewFileOutputStream (Servletactioncontext.getservletcontext (). Getrealpath ("upload") + "/" +myfilefilename)); byte[] Bytebuffer =New byte[1024]; intLen = 0;  while(len = Is.read (bytebuffer)) > 0) {os.write (Bytebuffer,0, Len); }        } finally {            if(Is! =NULL) {is.close (); }            if(OS! =NULL) {os.close (); }        }        returnaction.success; }}

upload.jsp file
<%@ Page Language="Java"ContentType="text/html; charset=gb18030"pageencoding="GB18030"%><!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=gb18030"><title>Insert Title here</title></Head><Body><formAction= "Uploadaction.action"Method= "POST"enctype= "Multipart/form-data">Select File:<inputtype= "File"name= "MyFile"><BR>File Description:<inputtype= "text"name= "desc"><BR>    <inputtype= "Submit"value= "Upload"></form></Body></HTML>
Struts.xml configuration, configure maximum file upload limit
<?XML version= "1.0" encoding= "UTF-8"?><!DOCTYPE struts Public "-//apache software foundation//dtd struts Configuration 2.1.7//en" "Http://struts.apache. Org/dtds/struts-2.1.7.dtd "><Struts>        <!--when the Struts.xml configuration file changes, it will be loaded immediately, in the production environment is best not to configure -    <constantname= "Struts.configuration.xml.reload"value= "true"></constant>    <!--provide more friendly tips -    <constantname= "Struts.devmode"value= "true"></constant>    <!--setting for a character set -    <constantname= "Struts.i18n.encoding"value= "GB18030"/>    <!--maximum configuration file upload limit -    <constantname= "Struts.multipart.maxSize"value= "9999999999"></constant>    < Packagename= "Struts2"extends= "Struts-default" >        <Global-results>            <result>/success.jsp</result>            <resultname= "Error">/error.jsp</result>        </Global-results>        <Actionname= "Login"class= "Com.djoker.struts2.LoginAction">            <result>/success.jsp</result>            <resultname= "Error">/error.jsp</result>        </Action>        <Actionname= "Uploadaction"class= "Com.djoker.struts2.uploadAction">            <result>/success.jsp</result>        </Action>    </ Package>        <includefile= "Struts-user.xml"></include></Struts>

STRUTS2 Study Note ten: 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.