struts1.2FormFile小例

导入txt文本
public void importFile(DAProcParamForm form) throws CustomException
 {
  BufferedReader readerBuff = null;
  try
  {
   FormFile file = form.getImportFile();
   StringBuffer buff = new StringBuffer();
   String fileName = file.getFileName();
   if(!fileName.endsWith(".txt"))   // 判断是否导入txt文本
   {
    form.setImportMsg("alert('导入文件请采用txt格式'); return;");
   }
   else
   {
    readerBuff = new BufferedReader(new InputStreamReader(file.getInputStream()));
    String content = readerBuff.readLine();
    
    while(content != null){
     buff.append(content);
     content = readerBuff.readLine();
    }
    String param = buff.toString();
    if(file.getFileSize() > 3995) // 自动截取
    {
     param = buff.toString().substring(0, 3990);
    }
       form.setImportContent(param);
   }
  }
  catch (FileNotFoundException e)
  {
   log.error("导入文件不存在"+e.getMessage());
   e.printStackTrace();
   throw new CustomException(CustomException.TYPE_ERROR,
     "导入文件不存在",e.getMessage());
  }
  catch (IOException e)
  {
   log.error("导入文件出错"+e.getMessage());
   e.printStackTrace();
   throw new CustomException(CustomException.TYPE_ERROR,
     "导入文件出错",e.getMessage());
  }
  finally{
   if(readerBuff != null){
    try
    {
     readerBuff.close();
    }
    catch (IOException e)
    {
     e.printStackTrace();
    }
   }
  }
 }

 

DAProcParamForm  里面加上这个字段

 private FormFile importFile;

 

页面

  <html:form action="/dap/mdcfg/param" enctype="multipart/form-data" target="contentIfrm">
        <html:hidden property="act" value="importfile"/>
     选择文件:<html:file property="importFile" styleId="importFile"></html:file>
     <a hrer="#" class="btn"><html:submit> 导 入</html:submit></a>
     <a hrer="#" class="btn"><input type="button" οnclick="delParam()" value="删 除"/></a>
    </html:form>

===========================================================================

补充:

import java.io.InputStreamReader;
import java.util.List;

import org.apache.struts.upload.FormFile;

import au.com.bytecode.opencsv.CSVReader;

 

判断导入的是什么文件 截取后缀来判断即可

csv文件

CSVReader reader = new CSVReader(new InputStreamReader(file.getInputStream(), "GBK"), ',');

String[] column;

 while ((column = reader.readNext()) != null) {

         //这里面做些对 某行 某列 字段处理的事情。 可以把符合条件的数据放到list里面

   

}

EXCEL文件

List listbill = new ExcelFileDeal().getFileInfo(file.getInputStream());

listbile循环处理即可。

 

 

 

 

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值