upload.htm文件-----------------------------
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<style type="text/css">
<!--
BODY{
font-size:9pt
}
-->
</style>
<SCRIPT language=javascript>
function check_file()
{
var strFileName=form1.FileName.value;
if (strFileName=="")
{
alert("请选择要上传的文件");
return false;
}
}
</SCRIPT>
<script language="javascript">
// javascript 动态添加 input type="file"
var i = 1;
function addFile(dvID, inputNamePrefix)
{
var dv = document.getElementById(dvID);
var file = document.createElement("input");
file.type = "file";
file.id = file.name = inputNamePrefix + i;
dv.appendChild(file);
var btn = document.createElement("input");
btn.type = "button";
btn.id = btn.name = "btn" + i;
btn.value = "删除" ;
btn.onclick = function() {
var b = document.getElementById(btn.id);
dv.removeChild(b.nextSibling); //remove <BR>
dv.removeChild(b.previousSibling); //file
dv.removeChild(b); //btn
}
dv.appendChild(btn);
dv.appendChild(document.createElement("BR"));
i++;
}
</script>
</head>
<body>
<form action="upfile.jsp" method="post" check_file()" enctype="multipart/form-data">
<div ></div>
<input type="button" value="添加文件" dvTitles','file')">
<input type="submit" value="上传文件">
</form>
</body>
</html>
upfile.jsp ----------------------------------------------
<%@ page contentType="text/html;charset=gb2312"%>
<%@ page import="com.jspsmart.upload.*"%>
<jsp:useBean scope="page" />
<HTML>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<BODY>
<%
// 初始化
final String upFileType="zip|rar|doc|txt|jpg|xls";//上传文件类型
final int MAXFILESIZE=2097152;//上传文件大小限制2M
String errMsg=null; //错误信息
boolean err=false; //错误标志
int fileSize=0; //文件大小
mySmartUpload.initialize(pageContext);
mySmartUpload.upload();
//判断将要上传文件的总容量是否超过上限
int count = mySmartUpload.getSize();
if(count>MAXFILESIZE){
out.print ("<script>alert('上传失败!文件大小:"+count/1024+"K超出了限定的范围(最大"+MAXFILESIZE/1024+"K)');this.history.go(-1);</script>");
}
// 循环取得上传所有文件
for(int i=0;i<mySmartUpload.getFiles().getCount();i++)
{
com.jspsmart.upload.File myFile = mySmartUpload.getFiles().getFile(i);
if (!myFile.isMissing())
{
String myFileName=myFile.getFileName();//得到文件名
String fileType=myFile.getFileExt();//得到文件扩展名
fileType=fileType.toLowerCase(); //将扩展名转换成小写
if (upFileType.indexOf(fileType)==-1)
{
err=true;
errMsg="文件"+myFileName+"上传失败!只允许上传以下格式的文件:"+upFileType;
out.print ("<script>alert('"+errMsg+"');this.history.go(-1);</script>");
}
myFile.saveAs("D:\\Tomcat 5.5\\webapps\\jdy\\"+myFileName);
}
}
out.print("<script>alert('上传文件成功!文件大小:"+count/1024+"K');this.history.go(-1);</script>");
%>
</BODY>
</HTML>
Trackback: http://tb.blog.csdn.net/TrackBack.aspx?PostId=1666290