import csv File example

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"; 
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() {
                  log.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文件数据该例子的效果图

原文地址:https://www.cnblogs.com/bella-life-blog/p/3620530.html