The simplest Demo of JSP file upload (such as image)

Source: Internet
Author: User

<% @ 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

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.