use jsp and servlet to upload file to server is becoming a
common task now.
below is a simple example use the jspsmart free component which
implements it.
you can download the free package from jspsmart
1. html file
<html>
<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>广告图片上载</td>
</tr>
<tr>
<td width="15%" nowrap >广告图片:</td>
<td width="50%" nowrap><input type="file"
name="adsimage" size="20"></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 (you can do it yourself)
