This article is to use the JSP servlet to upload the file function Oh, well crap don't say much we first take a look at this upload instance code.
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>
<TD colspan=2 nowrap align=center> advertising photo upload </td>
</tr>
<tr>
<TD width= "15%" nowrap > Advertising Pictures:</td>
<TD width= "50%" nowrap><input type= "file"
Name= "Adsimage" size= "></td>"
</tr>
<tr>
<TD align= "center" colspan=2 >
<input type= "button" value= "Upload" onclick= "return
Checkform (); ">
<input type= "button" value= "Cancel" onclick= "return
Cancelupload (); " >
</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)
Jsp/servlet uploading files 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.