首页 > 编程 > JSP > 正文

使用JSP/Servlet上载文件

2024-09-05 00:20:26
字体:
来源:转载
供稿:网友
使用 jsp/ servlet上载文件正成为一项常用的任务。以下是一个简单的例程,使用了jspsmart的一个免费的组件。你可以在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" >
   
<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 (you can do it yourself)


资料来源:碧波山庄visual basic http://ctsvb.126.com


收集最实用的网页特效代码!

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表