JSP smartupload implements the upload function code

Source: Internet
Author: User

CopyCode The 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 = 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>
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.