读取FTP上的excel文件,并写入数据库

 

 

   今天遇到一些问题,须要从ftp上读取一些excel文件,并须要将excel中的数据写入到数据库,这样就能够经过管理页面查看这些数据。java

    我将相关工做分为三步,一、从ftp上读取相关文件,并将excel文件下载到本地。二、读取本地下载完成的excel,读取相关信息 三、将读取的信息存储到数据库中。数据库

    一、获取java操做ftp操做,首先要从maven仓库https://mvnrepository.com/artifact/commons-net/commons-net 下载相应的jar包,apache commons net 提供了相应的接口。apache

 

/** * 获取FTPClient对象 * * @param ftpHost * FTP主机服务器 * @param ftpPassword * FTP 登陆密码 * @param ftpUserName * FTP登陆用户名 * @param ftpPort * FTP端口 默认为21 * @return
     */
    public static FTPClient getFTPClient(String ftpHost, String ftpUserName, String ftpPassword, int ftpPort) { FTPClient ftpClient = new FTPClient(); try { ftpClient = new FTPClient(); ftpClient.connect(ftpHost, ftpPort);// 链接FTP服务器
        ftpClient.login(ftpUserName, ftpPassword);// 登录FTP服务器
        if (!FTPReply.isPositiveCompletion(ftpClient.getReplyCode())) { log.info("未链接到FTP,用户名或密码错误。"); ftpClient.disconnect(); } else { log.info("FTP链接成功。"); } } catch (SocketException e) { e.printStackTrace(); log.info("FTP的IP地址可能错误,请正确配置。"); } catch (IOException e) { e.printStackTrace(); log.info("FTP的端口错误,请正确配置。"); } return ftpClient; }
public static void main(String [] args) throws IOException { String ftp_ipadd = "127.0.0.1";//ftp 地址
    String ftp_user = "guest";//ftp 登陆账号
    String ftp_passwd = "guest";//ftp 登陆账号密码
    int ftpport = 21;//ftp端口,默认为21
 FTPClient ftpClient = this.getFTPClient(ftp_ipadd, ftp_user, ftp_passwd, ftpport); log.info(String.valueOf(ftpClient.getReplyCode())); ftpClient.setControlEncoding("UTF-8"); // 中文支持
    ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);//设置文件类型
    ftpClient.enterLocalPassiveMode();//设置ftp 模式,有被动模式和活动模式,这里设置为被动模式
    String datestr = DateUtil.getyesterdayStr();//获取前一天的日期格式为20180921
    ftpClient.changeWorkingDirectory("/data/" + datestr + "/JD/");//设置ftp文件所在的目录
    FTPFile [] files = ftpClient.listFiles(); log.info(files.toString()); for (int i = 0; i < files.length; i++) { log.info(files[i].getName()); File localFile = new File("d:\\download\\" + datestr + "\\" + files[i].getName());//设置本地下载的目录
        File fileparent = localFile.getParentFile();//本地下载目录下的文件夹,若是不存在则建立
        if (!fileparent.exists()) { fileparent.mkdirs(); } OutputStream os = new FileOutputStream(localFile);//输出到本地文件流
        ftpClient.retrieveFile(files[i].getName(), os);//下载文件到本地
 os.close(); } ftpClient.logout();//关闭ftp连接
    }

这里只写了demo 不作代码优化了。服务器

二、读取本地的excel文件,java读取excel主要有两种方式jxl 和 poi, jxl只能读取2003之前的版本,但效率要高于poi,内存占用率也相对低(这里我也没有验证,导入量少基本没感受),poi则提供了两种方式分别支持2003和2007,HSSF方式支持2003,XSSF方式支持2007。这里我使用jxl读取xls结尾的文件,使用XSSF读取xlsx结尾的文件。一样若是想使用两种方法都须要到maven仓库下载相应的jar包。xss

/** * 读取excel文件 * * @param args */
    public static void readExcel(File filePath) { String extString = filePath.getName().substring(filePath.getName().lastIndexOf("."));//读取文件并判断文件类型
 InputStream is = null; try { is = new FileInputStream(filePath); if (".xls".equals(extString)) { jxlExcel(filePath);//这里执行jxl方法读取excel
        } else if (".xlsx".equals(extString)) { xssfExcel(filePath);//这里执行xssf方法读取excel
 } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } /** * 使用jxl方式读取excel 2003 * @param filePath */
    public static void jxlExcel(File filePath) { try { Workbook workbook = Workbook.getWorkbook(filePath); Sheet sheet = workbook.getSheet(0); int rowNums = sheet.getRows();// 获取excel总行数
        int columns = sheet.getColumns(); for (int i = 1; i <= rowNums; i++) { for (int j = 0; j < columns; j++) { log.info(sheet.getCell(i, j).toString()); } } } catch (BiffException e) { // TODO Auto-generated catch block
 e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block
 e.printStackTrace(); } } public static void xssfExcel(File filePath) { try { XSSFWorkbook xssfworkbook = new XSSFWorkbook(new FileInputStream(filePath)); XSSFSheet xssfsheet = xssfworkbook.getSheetAt(0); int rowNums = xssfsheet.getLastRowNum();// 当前sheet总共有多少行
        int columns = xssfsheet.getRow(0).getPhysicalNumberOfCells();// 当前sheet总共有多少列

        for (int i = 1; i <= rowNums; i++) { Row row = xssfsheet.getRow(i); for (int j = 0; j < columns; j++) { log.info(row.getCell(j).toString()); } } } catch (FileNotFoundException e) { // TODO Auto-generated catch block
 e.printStackTrace(); } catch (IOException e) { // TODO Auto-generated catch block
 e.printStackTrace(); } }

三、存入数据库,这里就很少说了。maven

 

关于ftp下载和读取excle其实也是常规的操做,只是须要 认真些就能够,固然能够把相关操做封装为util文件,使用的时候直接调用会更方便些。优化

相关文章
相关标签/搜索