Copy codeThe Code is as follows:
<% @ Page contentType = "text/html; charset = gb2312" %>
<Html>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
</Head>
<Body leftmargin = "0" topmargin = "0" bgcolor = "# ffffff">
<Form action = "upfilea. jsp" method = "post" name = "form1"
Enctype = "multipart/form-data">
<Input name = "FileName" type = "FILE" size = "30">
<Input type = "submit" name = "Submit" value = "Upload">
</Form>
</Body>
</Html>
Upload page
<% @ Page contentType = "text/html; charset = gb2312" %>
<% @ Page import = "java. SQL. *" %>
<% @ Page import = "java. util. *" %>
<% @ Page import = "java. io. *" %>
<% @ Page import = "com. jspsmart. upload. *" %>
<%
// Instantiate the Upload bean
Com. jspsmart. upload. SmartUpload mySmartUpload = new
Com. jspsmart. upload. SmartUpload ();
// Initialization
MySmartUpload. initialize (pageContext );
// Sets the maximum upload value.
MySmartUpload. setMaxFileSize (500*1024*1024 );
// Upload a file
MySmartUpload. upload ();
// Obtain all uploaded files cyclically
For (int I = 0; I <mySmartUpload. getFiles (). getCount (); I ++)
{
// Obtain the uploaded file
Com. jspsmart. upload. File myFile = mySmartUpload. getFiles (). getFile (I );
If (! MyFile. isMissing ())
{
// Obtain the name of the uploaded file
String myFileName = myFile. getFileName ();
// Obtain a file name without a suffix
String suffix = myFileName. substring (0, myFileName. lastIndexOf ('.'));
// Get the suffix
String ext = mySmartUpload. getFiles (). getFile (0). getFileExt ();
If (! (Ext. equals ("gif") | ext. equals ("jpg") | ext. equals ("wmv ")
| Ext. equals ("avi") | ext. equals ("swf "))){
Out. print ("<script language = 'javascript '> <! --
Alert ('upload failed, the image format is
GIF or JPG. The video format is WMV or AVI. If falsh is swf, try again! ');
// --> </Script> ");
Out. print ("<script language = 'javascript '> <! --
Window. location. href =
'Uploadb. jsp'
// --> </Script> ");
} Else
{
// Obtain the file size
Int fileSize = myFile. getSize ();
// Save path
String aa = getServletContext (). getRealPath ("/") + "upload /";
Random mynum = new Random (1 );
Int num = mynum. nextInt (10000 );
Num = (int) Math. random () * 10000;
Java. text. SimpleDateFormat formatter = new java. text. SimpleDateFormat
("YyMMddhhmmss ");
Java. util. Date date = new java. util. Date ();
String days = formatter. format (date );
Days = days + num;
// System. out. println ("days =" + days );
// System. out. println ("pre =" + pre );
// Obtain other parameters
Request. setCharacterEncoding ("GB2312 ");
/* If (request. getSession (true). getAttribute ("id ")! = "")
{
Id = request. getSession (true). getAttribute ("id"). toString ();
}
MyFile. saveAs ("upload/" + days + myFile. getFileName ());
String pic = "upload/" + days + myFile. getFileName ();*/
MyFile. saveAs ("upload/" + days + "6." + ext );
String pic = "upload/" + days + "6." + ext;
Session. setAttribute ("pic", pic );
Out. print ("<script
Language = 'javascript '> <! --
Parent. heheForm. pic. value = '"+ pic + "';
// --> </Script> ");
Out. print ("<script language = 'javascript '> <! --
Alert ('upload successful ');
// --> </Script> ");
// Save the uploaded file to the database.
// Save the file on the server
// Write the file to the database
// Response. sendRedirect ("uploada. jsp ");
} Else {out. print ("<script language = 'javascript '> <! --
Alert ('file upload is missing
Failed! '); Location =''
// --> </Script> ");}}
// Corresponds to the previous if
%>
<Html>
<Head> <Body leftmargin = "0" topmargin = "0" bgcolor = "# ffffff">
<Form>
<Input type = "text" name = "pic" value = "<% = session. getAttribute (" pic ") %>"
Size = "30">
</Form>
</Body>
</Html>