首页 > 其他 > 详细

import csv File example

时间:2014-03-24 16:20:37      阅读:652      评论:0      收藏:0      [点我收藏+]

ackage com.hkairport.lfs.handler.foundproperty;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.ResourceBundle;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.myfaces.trinidad.model.UploadedFile;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.hkairport.lfs.common.FileControlHelper;
import com.hkairport.lfs.exception.ApplicationException;
import com.hkairport.lfs.handler.BaseHandler;
import com.hkairport.lfs.service.main.FoundPropertyService;
import com.hkairport.lfs.valueRef.CEFSCSMConstants;

@Scope("request")
@Component
public class ImportCSVFileHandler extends BaseHandler{
private static final Log log = LogFactory.getLog(FoundPropertyHandler.class);

private UploadedFile inputFile;

private ResourceBundle configBundle = ResourceBundle.getBundle(resource_lfs_configs_config_parameters);
private static final String resource_lfs_configs_config_parameters = "com.hkairport.lfs.configs.configParameters"; //$NON-NLS-1$
private static final String str_upload_rep = "upload.repository";
private static final String FORWARD_IMPORTBARCODECSVFILE = "importBarcodeCSVFile";

@Autowired
private FoundPropertyService foundPropertyService = FoundPropertyService.DUMMY;

  public String importDataOfCsvFile() {
l  og.info(getLoginUserInfo());
  BufferedReader reader = null;
 try {
  if (getInputFile()==null) {
  throw new ApplicationException(ApplicationException.MANDATE_CHECK,"CSV File");
  }
  String fileName = inputFile.getFilename();
  String ext = fileName.substring(fileName.lastIndexOf(".")+1);
  if (ext == null || !(ext.toUpperCase().equals("CSV"))) {
  throw new ApplicationException(ApplicationException.CONCURR_DB_UPDATE);//导入的文件不是csv 类型文件时throw error
  }
  String tempPath = configBundle.getString(str_upload_rep);
  File dest = new File(tempPath + inputFile.getFilename());
  dest.createNewFile();
  FileControlHelper.uploadFile(inputFile.getInputStream(), dest);
  List<String[]> list=new ArrayList<String[]>();
  reader = new BufferedReader(new InputStreamReader(new FileInputStream(dest), "ISO-8859-1"));
  String tempString = reader.readLine();
  while (tempString != null) {
  if("".equals(tempString) || tempString.indexOf(",") == -1) {
  // Regards any blank line or invalid line without ","
  break;
  }
  String[] strArray = tempString.trim().split(",");
  if(strArray.length < 2) {
    // Ignore invalid line
  log.warn("Invalid Line: " + tempString);
  } else {
  list.add(strArray);
  }
  tempString = reader.readLine();
  }

  List<String> msglist=foundPropertyService.importdataAndUpdateFoundProperty(list);
  setResultPageMessage(msglist);
  setResultPageReturnButton(FORWARD_IMPORTBARCODECSVFILE);
    dest.deleteOnExit();

  } catch(Exception e) {
  return errorHandling(e);
  } finally {
    try {
      if(reader != null) {
      reader.close();
      }
    } catch (IOException e) {
    log.warn("Fail to close stream");
    }
  }

  return CEFSCSMConstants.RESULT_PAGE;
  }

  public String importBarcodeCSVFile() {
  return FORWARD_IMPORTBARCODECSVFILE;
  }

  public UploadedFile getInputFile() {
  return inputFile;
  }

  public void setInputFile(UploadedFile inputFile) {
  this.inputFile = inputFile;
  }
}

//导入csv文件数据该例子的效果图

bubuko.com,布布扣

import csv File example,布布扣,bubuko.com

import csv File example

原文:http://www.cnblogs.com/bella-life-blog/p/3620530.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!