<% @ Page contentType = "text/html; charset = gb2312" %>
<% @ Page import = "com. jspsmart. upload. *" %>
<% @ Page import = "java. util. Calendar, java. io. File" %>
<%
Request. setCharacterEncoding ("gb2312 ");
Try {
SmartUpload su = new SmartUpload (); // upload a file component
Long file_max_size = 4000000;
Su. initialize (pageContext); // upload Initialization
Su. upload ();
// Save all uploaded files to the specified directory
String temp = "upload/images ";
// Create a folder and use the absolute path
String uploadPath = request. getRealPath ("/") + temp;
// Create a folder if the folder does not exist
If (! New File (uploadPath). isDirectory ()){
New File (uploadPath). mkdirs ();
}
Files files = su. getFiles ();
// Obtain the uploaded file
Com. jspsmart. upload. File file = su. getFiles (). getFile (0 );
If (file. isMissing ()){
System. out. println ("missing ");
}
String postfix = "." + file. getFileExt ();
// Alias for the uploaded file
System. out. println ("Suffix:" + postfix );
If (! Postfix. equals (". jpg ")){
%>
<Script type = "text/javascript">
Alert ("this image format is not supported ");
</Script>
<%
} Else {
String fileName = "test ";
// This is the path for obtaining the image.
String strtemp = uploadPath + "/" + fileName + postfix;
System. out. println (strtemp );
File. saveAs (strtemp); // The parameter is the path (complete)
%>
<Script>
Alert ("image uploaded successfully! ");
</Script>
<%
}
} Catch (Exception e ){
System. out. println (e. getMessage ());
%>
<Script type = "text/javascript">
Alert ("Image Upload Failed! ");
</Script>
<%
}
%>
</Script>
<! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN">
<Html>
<Head>
<Title> homepage </title>
<Meta http-equiv = "pragma" content = "no-cache">
<Meta http-equiv = "cache-control" content = "no-cache">
<Meta http-equiv = "expires" content = "0">
<Meta http-equiv = "keywords" content = "keyword1, keyword2, keyword3">
<Meta http-equiv = "dashboard" content = "This is my page">
</Head>
<Body>
<Form action = "ImageUpload. jsp" method = "post" enctype = "multipart/form-data">
<Table> www.2cto.com
<Tr>
<Td width = "120" align = "center"> path: </td>
<Td width = "180"> <input type = "file" name = "file" class = "from_len21" contenteditable = "false"> </td>
</Tr>
<Tr>
<Td>
<Input type = "submit" value = "OK"/>
</Td>
</Tr>
</Table>
</Form>
</Body>
</Html>
Excerpted from the column of wandering hacker