日期:2014-05-16 浏览次数:20388 次
现在还有一个错误就是文件名是中文的话,就乱码。还在改进.
upload.htm
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<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" name="form1" onSubmit="return check_file()" enctype="multipart/form-data">
<input name="FileName" type="FILE" size="20">
<input type="submit" name="Submit" value="上传">
</form>
-->
<form action="upfile.jsp" method="post" name="form1" onSubmit="return check_file()" enctype="multipart/form-data">
<div id="dvTitles"></div>
<input type="button" value="添加单个文件" onclick="addFile('dvTitles','file')">
<input type="submit" name="Submit" value="上传已添加文件">
</form>
</body>
</html>
upfile.jsp:
<%@ page contentType="text/html;charset=gbk" import="com.jspsmart.upload.*" pageEncoding="GB18030"%>
<jsp:useBean id="mySmartUpload" scope="page" class="com.jspsmart.upload.SmartUpload" />
<HTML>
<BODY>
<%
// 初始化
final String upFileType="zip|rar|doc|txt|jpg|xls";??????? //上传文件类型
final int MAXFILESIZE=20480000;??????? //上传文件大小限制
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>");
//response.sendRedirect("http://127.0.0.1:8080/javastudy/upload.htm");
}
// 循环取得上传所有文件
else{
??? 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();//得到文件名
??????? System.out.println(myFileName);
??????? //if(myFileName.length()>0){??? //取得不带后缀的文件名
?????????? //String subFileName=myFileName.substring(0,myFileName.lastIndexOf('.'));
??????? //}
??????? String fileType=myFile.getFileExt();//得到文件扩展名
?????????? fileType=fileType.toLowerCase();?? //将扩展名转换成小写
??????? if (upFileType.indexOf(fileType)==-1){
??????? err=true;
??????? errMsg="文件"+myFileName+"上传失败!只允许上传以下格式的文件:"+upFileType;
??????? }
??????? //得到单个文件大小
??????? //fileSize