`
除了你无可取代
  • 浏览: 149536 次
  • 性别: Icon_minigender_1
  • 来自: 悲鸣洞穴
社区版块
存档分类
最新评论

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" onclick="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循环处理即可。

 

 

 

 

 

 

 

分享到:
评论
Global site tag (gtag.js) - Google Analytics