读取FTP上的excel文件,并写入数据库 您所在的位置:网站首页 excel存入数据库出错 读取FTP上的excel文件,并写入数据库

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

2024-01-23 05:45| 来源: 网络整理| 查看: 265

 

 

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

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

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

 

/** * 获取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 不做代码优化了。

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

/** * 读取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


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有