当前位置: 首页 > 图文教程 > 网络编程 > JSP > 使用JSP/Servlet上载文件

JSP
我认为JSP有问题(上)
我认为JSP有问题(下)
jsp“抓”网页代码的程序
关于在bean里面打印html的利弊看法
bean里面如何打印到html页面
jdbc3中的RowSet 接口规范
Apusic Application Server1.0中jsp源代码泄漏漏洞
Unify的eWave ServletExec拒绝服务漏洞
通过提交超长的GET请求导致IBM HTTP Server远程溢出
在HTTP请求中添加特殊字符导致暴露JSP源代码文件
Resin 1.2 重要源代码暴露漏洞
多中WEB服务器的通用JSp源代码暴露漏洞
Tomcat 暴露JSP文件内容
IBM WebSphere Application Server 暴露JSP文件内容
JRun 2.3.x 范例文件暴露站点安全信息
BEA WebLogic 暴露源代码漏洞
IBM WebSphere Application Server 3.0.2 存在暴露源代码漏洞
Tomcat 3.1 存在暴露网站路径问题
Sun Java Web Server 能让攻击者远程执行任意命令
Netscape 修复 JAVA 安全漏洞

使用JSP/Servlet上载文件


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-09-03   浏览: 60 ::
收藏到网摘: n/a

使用 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" onclick="return
checkForm();" >
&nbsp;&nbsp;&nbsp;
<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)