加入收藏 | 设为首页 | 会员中心 | 我要投稿 聊城站长网 (https://www.0635zz.com/)- 智能语音交互、行业智能、AI应用、云计算、5G!
当前位置: 首页 > 教程 > 正文

采用JSP/Servlet上载文件

发布时间:2023-05-13 15:17:28 所属栏目:教程 来源:
导读:使用 jsp/ servlet上载文件正成为一项常用的任务。以下是一个简单的例程,使用了jspsmart的一个免费的组件。你可以在jspsmart站点进行下载。

1. html file

<html>

<body>

<script language="javascr
使用 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)
 
收集最实用的网页特效代码!
 
 

(编辑:聊城站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!