Copy Code code as follows:
<%@ page contenttype= "text/html;charset=gb2312"%>
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">
<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= ">"
<input type= "Submit" name= "submit" value= "Upload" >
</form>
</body>
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.*"%>
<%
Instantiating an upload bean
Com.jspsmart.upload.SmartUpload mysmartupload=new
Com.jspsmart.upload.SmartUpload ();
Class
Mysmartupload.initialize (PageContext);
Set the maximum upload value
Mysmartupload.setmaxfilesize (500 * 1024*1024);
Uploading files
Mysmartupload.upload ();
Loop to get all uploaded files
for (int i=0;i <mysmartupload.getfiles (). GetCount (); i++)
{
Get the uploaded file
Com.jspsmart.upload.File myFile = Mysmartupload.getfiles (). GetFile (i);
if (!myfile.ismissing ())
{
Gets the filename of the uploaded file
String Myfilename=myfile.getfilename ();
Get filename with no suffix
String suffix=myfilename.substring (0,myfilename.lastindexof ('. '));
Get suffix Name
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, picture format is
GIF or JPG, video format for WMV or AVI,FALSH for SWF please try again! ');
--></script> ");
Out.print ("<script language= ' JavaScript ' ><!--
Window.location.href =
' Uploadb.jsp '
--></script> ");
}else
{
To get the size of a file
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);
Get another parameter
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 success ');
--></script> ");
The following is to save the uploaded file to the database
Save the file on the server side
Write a file to a database
Response.sendredirect ("uploada.jsp");
}}else{out.print ("<script language= ' JavaScript ' ><!--
Alert (' File upload lost
Defeat! '); location= '
--></script> ");}}
Corresponds to the previous if
%>
<body leftmargin= "0" topmargin= "0" bgcolor= "#ffffff" >
<form>
<input type= "text" name= "pic" value= "<%=session.getattribute (" pic ")%>"
Size= ">"
</form>
</body>