解析excel表获取数据,并从远程服务器下载和上传文件
2017-12-08 10:58
295 查看
package com.primecredit.dayend; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.MalformedURLException; import java.text.DecimalFormat; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.List; import java.util.Map; import jcifs.smb.SmbException; import jcifs.smb.SmbFile; import jcifs.smb.SmbFileInputStream; import jcifs.smb.SmbFileOutputStream; import org.apache.log4j.Logger; import org.apache.poi.ss.usermodel.Cell; import org.apache.poi.ss.usermodel.Row; import org.apache.poi.ss.usermodel.Sheet; import org.apache.poi.ss.usermodel.Workbook; import org.apache.poi.xssf.usermodel.XSSFCell; import org.apache.poi.xssf.usermodel.XSSFWorkbook; import com.primecredit.common.LoggerServiceFactory; import com.primecredit.common.Parameter; import com.primecredit.connection.ConnectionManager; import com.primecredit.exception.LoanWebException; import com.primecredit.los.DAOManager.ProposalManager; /** * @author hongk * @date 2017-7-31 * @description ********************** */ public class FinOverdueCustomerImageDataController extends BaseController{ protected static Logger logger=LoggerServiceFactory.getInstance().getLogger("dayend"); private ConnectionManager connMgr = null; public FinOverdueCustomerImageDataController(ConnectionManager connMgr){ this.connMgr = connMgr; } /** * 解析excel * @param inputStream * @return */ public static List<Map<String, String>> readExcel(InputStream inputStream){ List<Map<String, String>> listMaps = new ArrayList<Map<String,String>>(); Workbook workbook = null; try { logger.debug("read Excel begin"); workbook = new XSSFWorkbook(inputStream); Sheet xssfsheet = workbook.getSheetAt(0); int rowstart = xssfsheet.getFirstRowNum(); int rowEnd = xssfsheet.getLastRowNum(); for(int i = rowstart + 1; i <= rowEnd; i++){//行 Row row = xssfsheet.getRow(i); if(null == row) continue; int cellStart = row.getFirstCellNum(); int cellEnd = row.getLastCellNum(); Map<String, String> map = new HashMap<String, String>(); for(int j = cellStart; j <= cellEnd; j++){//列 Cell cell = row.getCell(j); if(null == cell) continue; String cellValue = ""; switch (cell.getCellType()){ case XSSFCell.CELL_TYPE_NUMERIC: // 数字 cellValue = new DecimalFormat("#").format(cell.getNumericCellValue());//电话号或是长数字类型转化成纯数字 break; case XSSFCell.CELL_TYPE_STRING: // 字符串 cellValue = cell.getStringCellValue(); break; case XSSFCell.CELL_TYPE_BOOLEAN: // Boolean break; case XSSFCell.CELL_TYPE_FORMULA: // 公式 break; case XSSFCell.CELL_TYPE_BLANK: // 空值 break; case XSSFCell.CELL_TYPE_ERROR: // 故障 break; default: break; } if(j == 0){ map.put("customerName", cellValue); }else if(j == 1){ map.put("loanType", cellValue); }else if(j == 2){ map.put("loanNo", cellValue); }else{ map.put("bankNo", cellValue); } } listMaps.add(map); } logger.debug("read Excel end"); } catch (IOException e) { e.printStackTrace(); logger.error("read Excel error:" + e.getMessage(), e); } return listMaps; } /** * @description ********************** * @param mapLists */ public static void traverseLoanTypeAndLoanNo(List<Map<String, String>> mapLists){ ConnectionManager connMgr = new ConnectionManager(); FinOverdueCustomerImageDataController controller = new FinOverdueCustomerImageDataController(connMgr); for (Map<String, String> map : mapLists) { String customerName = map.get("customerName"); String loanType = map.get("loanType"); String loanNo = map.get("loanNo"); String proposalNo = controller.getProposalNobyLoanTypeAndLoanNo(loanType, loanNo); String bankNo = map.get("bankNo"); String IMAGEDATADir = "smb://muser:muser!17@192.168.21.220/BatchesPro/" + proposalNo + "_" + customerName + "/";//合同所在目录 String uploadPath = "smb://muser:muser!17@192.168.21.16/recover/M7/M7/" + bankNo;//上传目录 Collection<? extends SmbFile> fileList; try { fileList = list(new SmbFile(IMAGEDATADir), true);//获取对应目录下的文件 if(fileList != null && fileList.size() > 0){ for(SmbFile file:fileList){ if(file.getName().contains(Parameter.IMAGEDATA)){ String imageDataPath = file.getPath(); UploadIMAGEDATA(imageDataPath , file.getName(), uploadPath);//从文件服务器中读取小额合同,上传到财务公共盘 } } } } catch (SmbException e) { e.printStackTrace(); } catch (MalformedURLException e) { e.printStackTrace(); } } } /** * @description ********************** * @param loanType * @param loanNo * @return */ public String getProposalNobyLoanTypeAndLoanNo(String loanType, String loanNo){ String proposalNo = ""; ProposalManager pManager = new ProposalManager(connMgr); try { logger.debug("get ProposalNo by LoanType And LoanNo begin"); proposalNo = pManager.getProposalNobyLoanTypeAndLoanNo(loanType, loanNo); logger.debug("get ProposalNo by LoanType And LoanNo end"); } catch (LoanWebException e) { logger.error("get ProposalNo by LoanType And LoanNo:" + e.getMessage(), e); e.printStackTrace(); } return proposalNo; } /** * @description ********************** * 通过SMB协议上传文件到远程服务器 * @param imageDataFile * @param fullName * @param uploadPath * @return */ public static boolean UploadIMAGEDATA(String imageDataPath,String fileName,String uploadPath){ boolean flag = false; InputStream is = null; OutputStream os = null; try { logger.debug(fileName + "贷款合同开始上传."); SmbFile imageDataFile = new SmbFile(imageDataPath); imageDataFile.connect(); is = new BufferedInputStream(new SmbFileInputStream(imageDataFile)); //判断文件夹是否存在 SmbFile remoteFile = new SmbFile(uploadPath); if(!remoteFile.exists()){ remoteFile.mkdirs(); } remoteFile.connect(); SmbFile uploadFile = new SmbFile(remoteFile + File.separator + fileName); os = new BufferedOutputStream(new SmbFileOutputStream(uploadFile)); byte[] buffer = new byte[1024]; int len = 0; while((len = is.read(buffer)) != -1){ os.write(buffer, 0, len); } os.flush(); flag = true; logger.debug(fileName + "贷款合同上传成功!"); } catch (FileNotFoundException e) { flag = false; logger.error("File Not Found Exception" + e.getMessage(), e); } catch (IOException e) { flag = false; e.printStackTrace(); logger.error("IO Exception" + e.getMessage(), e); }finally{ try { if(os != null){ os.close(); } } catch (IOException e) { e.printStackTrace(); } try { if(is != null){ is.close(); } } catch (IOException e) { e.printStackTrace(); } } return flag; } /** * 获取对应目录下所有的文件 * @param dir * @param recursive * @return * @throws SmbException */ public static Collection<? extends SmbFile> list(SmbFile dir, boolean recursive) throws SmbException{ List<SmbFile> fileList = new ArrayList<SmbFile>(); if(dir.isFile()){ fileList.add(dir); }else if(dir.isDirectory()){ SmbFile[] tempList = dir.listFiles(); for(SmbFile temp: tempList){ if(recursive){ if(temp.isFile()){ fileList.add(temp); }else if(temp.isDirectory()){ fileList.addAll(list(temp, recursive)); } }else{ fileList.add(temp); } } } return fileList; } public static void main(String[] args) { try { InputStream is = new FileInputStream(new File("D:/M7_IMAGEDATA/QH_M7_INFO_20179-201710.xlsx")); List<Map<String, String>> mapLists = readExcel(is);//解析excel traverseLoanTypeAndLoanNo(mapLists);//从文件服务器读取文件,上传到远程服务器指定文件夹 } catch (FileNotFoundException e) { e.printStackTrace(); } } }
相关文章推荐
- Xcode9学习笔记74 - 读取和解析Plist属性列表文件(获取远程服务器信息并显示返回数据)
- C#上传(修改表单数据),下载文件到服务器
- 在SecureCRT上,使用命令行从远程服务器上传或者下载文件
- 从服务器用xutils获取json,并用gson解析,包括用xutils下载文件
- Android连接服务器,从服务器获取数据,以及从服务器下载文件(单,多线程)
- struts2 + ajax(由前台的form提交数据到后台,再根据form所调用返回获取的后台json格式的数据返回到前端,然后前端用jquery对json数据进行解析)==》》涉及文件上传的部分
- tp的模版解析错误,在服务器上的文件竟然获取到我我本地的数据文件(url没写错的)
- 文件上传到服务器并获取文件中的数据
- 32. mac上传下载文件到远程服务器scp
- JCIFS实现远程服务器共享文件上传下载
- C# 中WinForm 应用程序实现远程文件管理(上传、下载)的种种问题——远程服务器返回错误
- 利用WebService实现远程服务器文件的上传和下载
- java利用jcraft实现和远程服务器交互,实现上传下载文件
- [Java Web]服务器获取远程文件并Zip打包给客户端下载
- CRT + LRZSZ 进行远程linux系统服务器文件上传下载
- ios开发进阶之网络04 数据解析 文件下载上传
- java使用Linux命令scp从远程服务器上传下载文件
- 利用WebService实现远程服务器文件的上传和下载
- 服务器学习之mac终端与远程服务器上传下载文件到本地
- Android与与服务器交换数据、上传、下载、读取文件