上传Excel文件(用jspsmartupload的第三方包)

1.首先我们必须先引入jspsmartupload这个包
2.以下是form表单 upload.jsp:


......................................................

 

enctype="multipart/form-data" onSubmit="return  check_upload(this)">
        信息导入type="file" name="photo">
          
 

注意:上面enctype="multipart/form-data" 和 type="file"
            上传文件要调用javascript代码验证 当多次单击上传按钮时候会有提示  :

 <script  type="text/javascript">
  function check_upload(theform)
{
 //document.write("check_upload");
if(theform.photo.value.length>0){
  if(theform.photo.value.indexOf("/") == -1&&theform.photo.value.indexOf("\\") == -1)
  {
            alert("请确定上传文件的正确性!!!")
            clckcnt--;
            theform.photo.focus()
            return false
  }
  }
 else{
   alert("您没有选择需要导入的文件!!!");
   theform.photo.focus();
  clckcnt--;
  return false
  }
  return true
}
var clckcnt=0;
function clckcntr()
{
clckcnt++;
if(clckcnt > 1)
   {
      if(clckcnt > 2)
     {
  return false;
  }
 alert('文件已经导入......\n\n' + '请等待片刻......\n\n' + '不要重复按提交键,谢谢!');
 return false;
    }
return true;
}
  </script>

3.upload_imp.jsp接受上传的的文件 jspsmartupload这个包初始化

<%


     request.setCharacterEncoding("GBK");
     SmartUpload smartupload=new SmartUpload();

     
     //在jsp中初始化用pageContext而在servlet中用3个参数的 (this.getServletConfig(),request,response)
     smartupload.initialize(pageContext);
     
     try{
      smartupload.setMaxFileSize(10*1024*1024);
      smartupload.setAllowedFilesList("xls");//允许多个类型smartupload.setAllowedFilesList("xls,jpg,doc");
      smartupload.upload();
      
      //若form表单中还有其他表单属性

     /*
           String username=smartupload.getRequest().getParameter("username");
           String password=smartupload.getRequest().getParameter("password");
           ....................
           ......................

    */
      
     }catch(Exception ex)
     {
    %>
    <SCRIPT language=javascript>alert("文件类型或上传文件大小错误!\n\n只允许上传这几种文件类型:xls(EXCEL文件)\n文件最大不能超过10M\n请查证后再上传");
 window.history.back();
 </script>
 
 
    <%
    return;
     }

//获得上传文件个数
int filecount=smartupload.getFiles().getCount();

for(int i=0;i<filecount;i++)
{
     //获取上传的文件
      File myFile=smartupload.getFiles().getFile(i);

     //判断是否选择了文件选择时返回false,没有选择返回true
      if(!myFile.isMissing())
     {
     try
     {
      //上传文件名
      String myFileName=myFile.getFileName();
      //取得不带后缀的文件名
      String frontName=myFileName.substring(0,myFileName.lastIndexOf("."));
      //取得后缀名
      String endName=myFile.getFileExt();
      //取得文件大小
      int size=myFile.getSize();
      
      //获得上传时间的
      Calendar calendar =Calendar.getInstance();
      
      String fileName=tools.Times.getId();
      
      //新名字
      String fileName2=fileName+".xls";
      
      //getRealPath(".")得到项目的全路径
      String aa=request.getRealPath(".")+"/upload/";
      
   String trace=aa+fileName2;
   
   String xlsfile = trace;
      //String xlsfile = trace;
      //myFile.saveAs(trace);
      //out.print(trace);
      myFile.saveAs(trace,smartupload.SAVE_PHYSICAL);

}
catch(Exception e)
{
           e.printStackTrace();

}

%>

以上就上传到服务器的upload文件夹了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值