<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <HTML> <body> <form action="load.jsp" method="post" enctype="mutipart/form-data" name="myform">请选择上传文件: <input TYPE="file" NAME="picture" /> <input TYPE="button" value="提 交" οnclick="check(myform);" /> <input TYPE="button" value="取 消" οnclick="window.close();" /></form> </body> </HTML> <mce:script language="javascript"><!-- function isEmpty(str){ for(var i=0;i<str.length;i++){ if(str.charAt(i)!==" "){ return false; } } return true; } function check(form){ var fileName = form.picture.value; if(isEmpty(fileName)){ alert("请选择文件名"); form.picture.focus(); return false; } //lastIndexOf如果没有搜索到则返回为-1 if(fileName.lastIndexOf(".")!=-1) { var fileType = (fileName.substring(fileName.lastIndexOf(".")+1,fileName.length)).toLowerCase(); var suppotFile = new Array(); suppotFile[0] = "gif"; suppotFile[1] = "bmp"; suppotFile[2] = "jpg"; for(var i =0;i<suppotFile.length;i++){ if(suppotFile[i]==fileType){ return true; }else{ continue; } } alert("不支持文件类型"+fileType); return false; }else { alert("文件只支持JIF,BMP,JPG"); } } // --></mce:script> 转自http://blog.csdn.net/zsy_gemini/archive/2007/10/11/1820870.aspx