上传文件

jsp页面:

<text id="theFile" type="file" name="theFile"
        cssStyle="height:30px" />
       <button id="expBtn" key="导入" onClick="fnSaveFile()" />

 

js方法:

//导入用例按钮
 function fnSaveFile() {
  Base.submitForm('form1', null, false, 'testAction!impExcel.do');
 }

 

//接受上传的文件,和前台组件的id一致
 private File theFile;
 //文件的名字
 private String theFileFileName;
 //文件的类型
 private String theFileContentType;

 

public File getTheFile() {
  return theFile;
 }

 public void setTheFile(File theFile) {
  this.theFile = theFile;
 }

 public String getTheFileFileName() {
  return theFileFileName;
 }

 public void setTheFileFileName(String theFileFileName) {
  this.theFileFileName = theFileFileName;
 }
 public String getTheFileContentType() {
  return theFileContentType;
 }
 public void setTheFileContentType(String theFileContentType) {
  this.theFileContentType = theFileContentType;
 }

 

/**
  *
  * excel文件导入,并显示到前台表格中
  * @return
  * @throws Exception
  */
 public String impExcel() throws Exception {
  ParamDTO dto = getDto();
  if("".equals(dto.get("atm002"))) {
   setMsg("请先选择功能!");
  }else{
   List list = null;
   if (theFile != null){
    try{
     //获得上传的文件流
     InputStream fin = new FileInputStream(theFile.getPath());
     //判断文件类型,调用相应的工具类的方法
     if (getTheFileFileName().indexOf(".xls") > 0) {
      //获得excel文件中的数据 
      list=ExcelFileUtils.getExcelInputStream2ObjectList(fin, "导入表格中的字段", "实体类的路径", true);
     }else {
      setMsg("文件格式只能为.xls!");
      return "success";
     }
     //将获得的数据设置到前台表格dgUC
     setList("dgUC", list);
     setData("atm001",dto.get("atm001"));
     setMsg("文件导入成功!");
     //删除零时文件
     theFile.delete();
     //关闭流
     fin.close();
    }catch(Exception e){
     setMsg(e.getMessage());
    }
   }else {
    setMsg("请先上传文件!");
   }
  }
  setData("atm001",dto.get("atm001"));
  setData("OneAtm001",dto.get("atm001"));
  setActiveTab("tb_usecase");
  setEnable("saveBtn");
  return "success";
 }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值