使用 JSP/ Servlet上载文件正成为一项常用的任务以下是一个简单的例程使用了jspsmart的一个免费的组件你可以在JSPSMART站点进行下载
Html File
<html>
<body>
<script LANGUAGE="javascript" SRC="JSFunctionjs">
</script>
<script language="JavaScript">
<!
function checkForm(){
if (documentuploadformadsImagevalue==""){
alert("You must choose what image file to upload!");
return false;
}
documentuploadformsubmit();
}
function cancelUpload(){
windowclose();
windowopenerfocus();
}
//>
</script>
<form name="uploadform" method="post"
action="uploadImagejsp" ENCTYPE="multipart/formdata"
target=_self>
<center>
<table border="" width="%">
<tr>
<td colspan= nowrap align=center>广告图片上载</td>
</tr>
<tr>
<td width="%" nowrap >广告图片:</td>
<td width="%" nowrap><input type="file"
name="adsImage" size=""></td>
</tr>
<tr>
<td align="center" colspan= >
<input type="button" value="upload" onclick="return
checkForm();" >
<input type="button" value="Cancel" onclick="return
cancelUpload();">
</td>
</tr>
</form>
</table>
<!
<a href="Javascript:windowclose();">close this window</a>
>
<script language="JavaScript">
<!
thisfocus();
//>
</script>
</body>
JSP File
<%@ page language="java" import="comjspsmartupload*
javasql*javautil* "%>
<% use SmartUpload bean %>
<jsp:useBean id="mySmartUpload" scope="page"
class="comjspsmartuploadSmartUpload" />
<%
//Initialization
mySmartUploadinit(config);
mySmartUploadservice(requestresponse);
//Set Restriction
mySmartUploadsetAllowedFilesList("gifbmpjpegjpg");
mySmartUploadsetTotalMaxFileSize();
// Upload
try{
mySmartUploadupload();
}
catch(Exception e){
outprintln("<font color=red>Upload File Fail!</font>
<a href=
"JavaScript:windowhistoryback();">back</a><br>");
outprintln("<UI>Notes:</UI>");
outprintln("<UL>You must set correct File
Name</UL>");
outprintln("<UL>You file size must be less than
K</UL>");
outprintln("<UL>You can only upload Gif jpeg jpg and
bmp files</UL>");
return;
}
//save file to disk
mySmartUploadgetFiles()getFile()saveAs("/" +
"FileName");
%>
Servlet File (you can do it yourself)