struts1.x Series Tutorial (8): Uploading a single file

Source: Internet
Author: User
Tags file size

The ability to upload files in a Web program typically uses the <input type=file.../> tab to display the user interface for the file name to be uploaded on the client. If readers use Jsp/servlet to process client-uploaded files, it is not easy to implement this feature. In struts, we provide a mechanism for saving client-uploaded files to the server.

A

In struts, a Formfile object represents an uploaded file. Formfile is actually an interface that we can find in the Org.apache.struts.upload package. The Formfile interface has the following five common methods:

1.getInputStream () method

This method is used to obtain the Java.io.InputStream object of the uploaded file, we can read the data of the uploaded file from the InputStream object and write it to the path specified by the server.

2.getFileData () method

This method is used to place the entire contents of the uploaded file into a byte array, and it is convenient to save the uploaded file to the specified path on the server, if the size of the uploaded file is relatively small.

3.getFileName () method

This method is used to obtain the file name of the uploaded file (excluding the file in the client's path portion).

4. GetFileSize () method

This method is used to get the number of bytes uploaded to the file.

5. Destroy () method

Used to destroy all resources associated with the current uploaded file.

The following example shows how to use the

"1th Step" to create a JSP page for uploading files

Create a uploadfile.jsp file in the <samples Engineering directory > directory with the following code:

<%@ page pageencoding= "GBK"%>
<%@ taglib uri= "http://struts.apache.org/tags-html" prefix= "html"%>
<title> Upload a single file (file size cannot exceed 2M) </title>
<body>
<%--must add enctype= "Multipart/form-data" to the
</body>

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.