Uploading files using Jsp/servlet

Source: Internet
Author: User
Tags file size return save file window
Js|servlet
Uploading files using Jsp/servlet is becoming a common task. The following is a simple routine that uses a free component of Jspsmart. You can download it at the Jspsmart site.

1. Html File
<body>
<script language= "javascript" src= "Jsfunction.js" >
</script>
<script language= "JavaScript" >
<!--
function Checkform () {

if (document.uploadform.adsimage.value== "") {
Alert ("You must choose what image file to upload!");
return false;
}
Document.uploadform.submit ();
}
function Cancelupload () {
Window.close ();
Window.opener.focus ();
}
-->
</script>
<form name= "Uploadform" method= "POST"
action= "uploadimage.jsp" enctype= "Multipart/form-data"
Target=_self>
<center>
<table border= "1" width= "65%" >
<tr>
&LT;TD colspan=2 nowrap align=center> advertising photo upload </td>

</tr>
<tr>
&LT;TD width= "15%" nowrap > Advertising Pictures:</td>
&LT;TD width= "50%" nowrap><input type= "file"
Name= "Adsimage" size= "></td>"
</tr>
<tr>
&LT;TD align= "center" colspan=2 >
<input type= "button" value= "Upload" >

<input type= "button" value= "Cancel" >
</td>

</tr>
</form>
</table>
<!--
<a href= "Javascript:window.close ();" >close this window</a>
-->
<script language= "JavaScript" >
<!--
This.focus ();
-->
</script>
</body>

2. JSP File
<%@ page language= "java" import= "com.jspsmart.upload.*,
Java.sql.*,java.util.* "%>
<%--Use Smartupload bean--%>
<jsp:usebean id= "mysmartupload" scope= "page"
class= "Com.jspsmart.upload.SmartUpload"/>

<%

Initialization
Mysmartupload.init (config);
Mysmartupload.service (Request,response);

Set restriction
Mysmartupload.setallowedfileslist ("gif,bmp,jpeg,jpg");
Mysmartupload.settotalmaxfilesize (51200);



Upload
try{
Mysmartupload.upload ();
}
catch (Exception e) {
Out.println ("<font color=red>upload File fail!</font>
<a href=
"JavaScript:window.history.back ();" >back</a><br> ");
Out.println ("<UI>Notes:</UI>");

OUT.PRINTLN ("<ul>you must set correct File
Name.</ul> ");
Out.println ("<ul>you file size must be less than
50k.</ul> ");
Out.println ("<ul>you can only upload.") Gif. jpeg. jpg and
. bmp files.</ul> ");

Return

}
Save file to disk
Mysmartupload.getfiles (). GetFile (0). SaveAs ("/" +
"FileName");

 

%>



3. Servlet File (can do it yourself)


Source: Blue Ocean Villa Visual Basichttp://ctsvb.126.com


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.