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>