首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > JAVA > Java Web开发 >

100分,求JSP上传EXCEL文件代码,可以弹出文件选择框,并限制选择文件只能是1.xls!该如何处理

2012-03-28 
100分,求JSP上传EXCEL文件代码,可以弹出文件选择框,并限制选择文件只能是1.xls!100分,求JSP上传EXCEL文件

100分,求JSP上传EXCEL文件代码,可以弹出文件选择框,并限制选择文件只能是1.xls!
100分,求JSP上传EXCEL文件代码,可以弹出文件选择框,并限制选择文件只能是1.xls!


请各位赐教!

[解决办法]
File f1=new File( "J:\\Tomcat 5.5\\webapps\\ROOT\\test ",tempfilename);


tempfilename 改成你想设的文件名,试试吧
[解决办法]
前两天我写的一个上传图片的jsp,本来是一个读取excel的jsp
也是东拼西凑,希望能给你帮助

<%@ page contentType= "text/html; charset=GB2312 " %>
<%@page import= "java.util.* "%>
<%@page import= "java.util.Date "%>
<%@page import= "java.sql.* "%>
<%@page import= "javax.sql.* "%>
<%@page import= "sun.jdbc.rowset.* "%>
<%@page import= "java.io.* "%>
<%@page import= "javax.servlet.* "%>
<%@page import= "org.gqforce.* "%>
<%@page import= "javax.servlet.http.*,com.jspsmart.upload.*,jxl.* "%>
<%
String conPath=request.getContextPath();
%>
<html>
<SCRIPT LANGUAGE= "JavaScript " src= " <%=conPath%> /js/common.js "> </SCRIPT>
<SCRIPT LANGUAGE= "JavaScript " src= " <%=conPath%> /js/translate.js "> </SCRIPT>
<script>
function showimg()
{
document.Myform.Myimg.src=document.Myform.filename.value;
}
function checkinput()
{
var strFileFormat = form1.file.value.match(/^(.*)(\.)(.{1,8})$/)[3];//检查上传文件格式
if(strFileFormat== "jpg "||strFileFormat== "gif "||strFileFormat== "jpeg "||strFileFormat== "bmp "||strFileFormat== "JPG "||strFileFormat== "GIF "||strFileFormat== "JPEG "||strFileFormat== "BMP ") //用js方法限制上传类型,其实可以用upper()方法优化
{
return true;
}
else
{
alert( "只能上传.bmp,.jpg,jpeg和.gif,请重新选择! ");
return false;
}

}

</script>
<form method= "post " enctype= "multipart/form-data " name= "form1 " action = ' '>
<table border= "0 " cellspacing= "0 " cellpadding= "0 " align= "center " width= "100% " >
<tr>
<td>
<table width= "90% " id= "uploadTable " border= "0 " height= "100% " cellspacing= "0 " cellpadding= "0 " align= "center ">
<tr>
<td align= "center " height= "27 ">
<input type= "file " name= "file " class= "inputfile " id= "fileInput " style= "width:210 " required = "true " msg= "上传文件 ">
<!-- <input type= "button " name= "Submit " value= "+ " onclick= "addSelfRow() "> -->
</td> <td> <input type= 'button ' onclick= "checkinput();submit(); " value= '提交 '> </td>
</tr>
</table>
</td>
</tr> </table>
</form>
</html>
<%
String path=request.getContextPath();
com.jspsmart.upload.SmartUpload smartUpload=new com.jspsmart.upload.SmartUpload();

smartUpload.initialize(pageContext);
String fileName= " ",fileNameStr= " ";
Vector fileNames = new Vector();


try{

smartUpload.setAllowedFilesList( "jpg,JPG,bmp,BMP,jpeg,JPEG,gif,GIF "); //这里可以限制上传类型
boolean isfile=true;
smartUpload.upload();
Request req=smartUpload.getRequest();
System.out.println(smartUpload.getSize());
System.out.println( smartUpload.getFiles().getCount());
com.jspsmart.upload.File myfile=null;
if(smartUpload.getFiles().getCount()> 0){
for(int i=0;i <smartUpload.getFiles().getCount();i++){
myfile = smartUpload.getFiles().getFile(i);
fileName=myfile.getFileName();
out.println(fileName);
fileNames.add(fileName);
fileNameStr+= " ' "+fileName+ " ', ";
smartUpload.save(request.getRealPath( "/ ")+ "\\uploadfile\\ ");
}
fileNameStr = fileNameStr.substring(0,fileNameStr.length()-1);
%>
<script>

var a=new Array( <%=fileNameStr%> );
window.returnValue= a;
window.close();
</script>
<%
}
}catch(Exception ee){
//out.println(ee);
//out.println( " <script> alert( '上传失败!请确认无误后,重新上传! ');parent.sb(0, ' '); </script> ");
}finally{
try{
}catch(Exception e){
out.println(e);
}
}

%>

热点排行