The file upload function is widely used in B/S-based development models. The implementation of JSP file upload is as follows: Use the getinputstream () method of the servletrequest class to obtain the data stream sent from a client to the server, and then process the data stream for analysis, obtain the parameters and data transferred from the file upload to the server, and store the file data as a file or insert it into the database.
The implementation method looks simple, but it is quite troublesome to develop and implement a stable and reliable file upload class. Currently, many third-party class libraries implement File Upload functions, such as the famous smartupload and Apache Jakarta fileupload. Here we will introduce cos, which is a simple and practical Company of o'reilly and is doing very well.
In.
Cos is easy to use. The following describes cos usage in conjunction with an instance. The instance involves two files: upload.htm and upload. jsp. You can submit multiple files on the upload.htm page. The specific code is as follows:
<HTML>
<Head>
<Title> File Upload </title>
<Meta http-equiv = "Content-Type" content = "text/html; chartset =" gb2312 ">
</Head>
<Body>
<H2> File Upload </H2>
<Form enctype = "multipart/form-Data" method = "Post" Action = "Upload. jsp">
<P> Upload File 1: <input type = "file" name = "file1" size = "20" maxlength = "20"> <br>
File Description: <input type = "text" name = "file1" size = "30" maxlength = "50"> </P>
<P> Upload File 2: <input type = "file" name = "file2" size = "20" maxlength = "20"> <br>
File description: <input type = "text" name = "file2" size = "30" maxlength = "50"> </P> <input type = "Submit" value = "Upload"> </P>
</Form>
</Body>
</Html> note that in the form attribute, the method must be "Post"; otherwise, files cannot be uploaded.
The upload. JSP code is as follows:
<% @ Page pageencoding = "gb2312" %>
<% @ Page contenttype = "text/heml; charset = gb2312" %>
<% @ Request. setcharacterencoding ("gb2312"); %> <% @ page import = "Java. Io. *" %>
<% @ Page import = "Java. util. *" %>
<% @ Page import = "com. oreilly. servlet. multipartrequest" %> <%
// Store the uploaded file under C:/upload
String savedirectory = "C: // upload ";
File uploadpath = new file (savedirectory );
If (! Uploadpath. exists ()){
Uploadpath. mkdir ();
}
// The size of the uploaded file is limited to 5 MB.
Int maxpostsize = 5*1024*1024; // file description
String filedescription [] = {null, null}; // Upload File Name
String filename = NULL; // Number of uploaded files
Int COUNT = 0; // upload a file
Multipartrequest multi = new multipartrequest (request, savedirectory, maxpostsize, "GBK ");
%> <HTML>
<Head>
<Title> File Upload </title>
</Head>
<Body>
<%
// Obtain the file description
If (multi. getparameter ("file1 ")! = NULL ){
Filedescription [0] = multi. getparameter ("file1 ");
} Else {
Filedescription [0] = "";
}
If (multi. getparameter ("file2 ")! = NULL ){
Filedescription [1] = nulti. getparameter ("file2 ");
} Else {
Filedescription [1] = "";
} // Obtain the names of all uploaded files
Enumeration filesname = multi. getfilenames ();
While (filesname. hasmoreelements (0 ){
String name = (string) filesname. nextelement ();
Filename = multi. getfilesystemname (name );
File F = multi. GetFile (name );
String contenttype = multi. getcontenttype (name );
If (filename! = NULL ){
Count ++;
%> <Font color = "red"> file <% = count %> you uploaded: </font> <br>
File Name: <% = filename %> <br>
File Type: <% = contenttype %> <br>
File Description: <= filedescription [count-1] %> <br>
<%
} // End if
} // End while
%>
You have uploaded <font color = "red"> <% = count %> </font> files
</Body>
</Html>
Cos encapsulates the file upload function. You only need to instantiate the multipartrequest class to upload the file to the specified directory. However, note that the constructor is used to input the Chinese encoding method, otherwise, the Chinese file name may contain garbled characters.