Actionform File Upload

Source: Internet
Author: User

Here is an example to illustrate how to upload an actionform file: (No jar package is required)

 

Create a form on the page:

<Li> test struts File Upload </LI> <br>
<Form action = "Upload. Do" method = "Post" enctype = "multipart/form-Data"> //Enctype = "multipart/form-Data" is used for uploading. You need to add
Title: <input type = "text" name = "title"> <br>
File: <input type = "file" name = "myfile"> <br> //Type: File
<Input type = "Submit" value = "Submit">
</Form>

 

Create a successfully uploaded page:

On this page, retrieve the name of the uploaded file, including the title:

<Body>
Title: $ {uploadform. Title} <br> // retrieve from actionform
File Name: $ {uploadform. myfile. filename} <br>
</Body>

 

Create actionform (use static ):

 

Import org. Apache. Struts. Action. actionform;
Import org. Apache. Struts. Upload. formfile;

/**
* Upload actionform
* @ Author Administrator
*
*/
Public class uploadactionform extends actionform {

Private String title;
 
// Formfile must be used
Private formfile myfile ;//The file type is declared as formfile, which must be declared as follows: use this to receive files

Public String gettitle (){
Return title;
}

Public void settitle (String title ){
This. Title = title;
}

Public formfile getmyfile (){
Return myfile;
}

Public void setmyfile (formfile myfile ){
This. myfile = myfile;
}

 

Then write the corresponding action:

/**
* Upload action
* @ Author Administrator
*
*/
Public class uploadtestaction extends action {

@ Override
Public actionforward execute (actionmapping mapping, actionform form,
Httpservletrequest request, httpservletresponse response)
Throws exception {
Uploadactionform UAF = (uploadactionform) form ;// Get form data
System. Out. println ("title" + UAF. gettitle ());// Get title
Formfile myfile = UAF. getmyfile ();// When a new formfile is created, the file will be put in and the file will be obtained (call formfile in action to obtain the uploaded file)
If (myfile! = NULL ){
System. Out. println ("filename =" + myfile. getfilename ());// This object can be used to obtain the file name, file type, and size. For details, see the document.

Fileoutputstream Fos = new fileoutputstream ("C: \" + myfile. getfilename ());// New: An output stream with a drive C. Its name is called its own name.
FOS. Write (myfile. getfiledata ());// Write to Hard Disk
FOS. Flush ();
FOS. Close ();
}
Return Mapping. findforward ("success ");
}

}

}

Configure

The test is OK.

 

 

 

 

Configure the cache for the size of files that can be uploaded and the temporary directory:

Implementing this function is configured in struts-config.xml,

</Action-mappings>
 
<Controller maxfilesize = "10 m" nocache = "true"/> // The Controller label has many attributes. For details, see DTD (located in struts. org. apache. struts. in the struts-config-1-2.dtd in resource), nocache = "true" indicates that the cache is cleared each time
 
<Plug-in classname = "com. bjsxt. Struts. utildateconverterinitwit2d-ugin"/>
</Struts-config>

 

 

Summary:

* Page configuration, such:
<Form action = "Upload. Do" method = "Post" enctype = "multipart/form-Data">
Title: <input type = "text" name = "title"> <br>
File: <input type = "file" name = "myfile"> <br>
<Input type = "Submit" value = "Submit">
</Form>
* Use formfile in actionform to receive uploaded files. For more information, see uploadactionform. java.
* Call formfile in action to obtain the data of the uploaded file. Use stream output to complete the upload. For details, see uploadtestaction. java.
* Use the <Controller/> label to configure the upload parameters, for example, <controller maxfilesize = "10 m"/>

 

 

 

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.