js| Introduction | upload | Download
Third, File upload Chapter
㈠ form Requirements
There are two requirements for the form form for uploading files:
1, Method application post, that is, method= "post."
2, add attributes: Enctype= "Multipart/form-data"
Here is an example of the form form used to upload a file:
<form method= "POST" enctype= "Multipart/form-data"
action= "/jspsmartupload/upload.jsp" >
<input type= "FILE" name= "MYFILE" >
<input type= "SUBMIT" >
</FORM>
Examples of ㈡ uploads
1, upload page upload.html
This page provides the form, lets the user select to upload the file, clicks "Uploads" the button to carry on the upload operation.
Page source code is as follows:
<!--
FileName: upload.html
Author: vertical and horizontal software production center rain also Strange (zhsoft88@sohu.com)
;
<! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en"
<title> File Upload </title>
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312"
<body>
<p> </p>
<p align= "center" > upload file Selection </p>
< FORM method= "POST" action= "jsp/do_upload.jsp"
enctype= "Multipart/form-data"
<input type= "hidden" name = "TEST" value= "good"
<table width= "75%" border= "1" align= "center"
<tr>
<td><div align= "center" >1,
<input type= "FILE" name= "FILE1" size= ""
</div></td>
</tr
<tr>
<td><div align= "center" >2,
<input type= "FILE" name= "FILE2" size= ">"
</div></td>
</tr>
<tr>
<td><div align= "center" >3,
<input Type= "FILE" name= "FILE3" size= "
</div></td>
</tr>
<tr>
<td>< Div align= "center" >4,
<input type= "FILE" name= "FILE4" size= ""
</div></td>
</tr
<tr>
<td><div align= "center"
<input type= "Submit" name= "Submit" value= "Upload it!" "
</div></td>
</tr>
</table>
</form>
</body>
</ Html>
2, upload processing page do_upload.jsp
This page performs file upload operations. The source of the page details the use of the upload method, this is not to repeat.
Page source code is as follows:
<%--
FileName: do_upload.jsp
Author: vertical and horizontal software production center rain also odd (zhsoft88@sohu.com)
--%>
<%@ page contenttype= "text/html; charset=gb2312 "Language=" Java "
Import= "java.util.*,com.jspsmart.upload.*" errorpage= ""%>
<title> File Upload Processing page </title>
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">
<body>
<%
Create a new Smartupload object
Smartupload su = new Smartupload ();
Upload initialization
Su.initialize (PageContext);
Set upload limit
1. Limit the maximum length of each uploaded file.
Su.setmaxfilesize (10000);
2. Limit the length of the total upload data.
Su.settotalmaxfilesize (20000);
3. Set the file allowed to upload (by extension limit), only allow doc,txt files.
Su.setallowedfileslist ("Doc,txt");
4. Set prohibited file upload (by extension limit), no upload with Exe,bat,
Files with jsp,htm,html extensions and files that do not have extensions.
Su.setdeniedfileslist ("exe,bat,jsp,htm,html,,,");
Uploading files
Su.upload ();
Saves all uploaded files to the specified directory
int count = Su.save ("/upload");
Out.println (count+ "File Upload success! <br> ");
Use the Request object to get the value of the parameter
Out.println ("test=" +su.getrequest (). GetParameter ("TEST")
+ "<BR><BR>");
Extract upload file information, while saving files.
for (int i=0;i<su.getfiles (). GetCount (); i++)
{
Com.jspsmart.upload.File File = Su.getfiles (). GetFile (i);
Continue if the file does not exist
if (file.ismissing ()) continue;
Display current File information
Out.println ("<table border=1>");
OUT.PRINTLN ("<TR><TD> table item name (FieldName) </TD><TD>"
+ file.getfieldname () + "</TD></TR>");
Out.println ("<TR><TD> file length (size) </TD><TD>" +
File.getsize () + "</TD></TR>");
OUT.PRINTLN ("<TR><TD> filename (filename) </TD><TD>"
+ file.getfilename () + "</TD></TR>");
Out.println ("<TR><TD> file name extension (fileext) </TD><TD>"
+ file.getfileext () + "</TD></TR>");
Out.println ("<TR><TD> file Full name (filepathname) </TD><TD>"
+ file.getfilepathname () + "</TD></TR>");
Out.println ("</TABLE><BR>");
Save a file
File.saveas ("/upload/" + myfile.getfilename ());
Save to a directory with the root directory of the Web application as the root of the file
File.saveas ("/upload/" + myfile.getfilename (),
Su. Save_virtual);
Save to the operating system's root directory as the file root directory
File.saveas ("c:\\temp\\" + myfile.getfilename (),
Su. save_physical);
}
%>
</body>