欢迎光临
我们一直在努力

使用servlet和jsp上载文件-JSP教程,Jsp/Servlet

建站超值云服务器,限时71元/月

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)

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 使用servlet和jsp上载文件-JSP教程,Jsp/Servlet
分享到: 更多 (0)