生成文件且通过FTP上传到别的服务器的指定路劲

2024-05-25 17:32

本文主要是介绍生成文件且通过FTP上传到别的服务器的指定路劲,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

1.创建文件路劲

String miguPath = this.getClass().getClassLoader().getResource(File.separator).getPath()+ "template/export/synMigu/";
File file = new File(miguPath);
if (!file.exists()) {if(!file.mkdirs()){log.error("同步**+创建文件夹失败");}}

2.文件名命名

File txtFile = new File(miguPath + fileName + ".txt");


3.创建FTP文件

fileInfoService.createFtpFile(fileInfo,settMonth,fileName,txtFile);

3.1查询数据并写入到txt文件中,列头和列内容分来写入。

   

 public void createFtpFile(FileInfo fileInfo,String settMonth,String fileName,File file) throws IOException {List rows = null;String DM_SCHEMA = propertyManager.get(SystemConfigKey.DM_SCHEMA_KEY);StringBuffer queryCount = new StringBuffer("");StringBuffer queryPage = null;String querySql=fileInfo.getExecuteSql();querySql=querySql.replace(":settleMonth", settMonth);querySql=querySql.replace(":DM_SCHEMA", DM_SCHEMA);queryCount.append("select count(1) from (").append(querySql).append(") a");Integer count = jdbcTemplate.queryForObject(queryCount.toString(),Integer.class);//写入文件列头List<String[]> dataList=getFileHeader(fileInfo.getName());writeTxtForString(dataList,fileName,file);try {if(count <= Constant.PAGE_SIZE){log.info("["+fileInfo.getName() + "]执行sql:" + querySql);rows = jdbcTemplate.queryForList(querySql);writeTxt(rows, fileName ,file);}else {for (int i = 0; i < (count / Constant.PAGE_SIZE) + 1; i++) {queryPage = new StringBuffer("");queryPage.append(querySql).append(" limit ");queryPage.append(i * Constant.PAGE_SIZE).append(",").append(Constant.PAGE_SIZE).append(" ");rows = jdbcTemplate.queryForList(queryPage.toString());writeTxt(rows, fileName, file);log.info("["+fileInfo.getName() + "]执行sql:" + querySql);}}}catch (Exception e){throw e;}}///获取列头public List<String []> getFileHeader(String name){List<String[]> dataList=new ArrayList<>();if(("违约核减").equals(name)){String[] data = {"账期", "企业代码", "业务代码", "渠道结算代码", "业务名称", "金额", "备注"};dataList.add(data);} return dataList;}//写入列头public void writeTxtForString(List rows, String fileName, File file) throws IOException {// 写入txttry {Writer fw = null;if(file == null){file = new File(fileName);}try {fw = new OutputStreamWriter(new FileOutputStream(file,true), "UTF-8");PrintWriter pw = new PrintWriter(fw);for (Object line : rows) {String [] lineArray = (String []) line;StringBuffer linebuffer = new StringBuffer();for (String string:lineArray) {if(string!=null){linebuffer.append(string).append(",");}else{linebuffer.append(",");}}linebuffer.deleteCharAt(linebuffer.length() - 1);linebuffer.append("\r\n");pw.printf(linebuffer.toString(), "\r\n");}pw.flush();pw.close();} catch (Exception e) {throw e;}finally {if(fw!=null) {fw.close();}}} catch (Exception e) {throw e;}}//写入数据库内容public void writeTxt(List rows, String fileName, File file) throws IOException {// 写入txttry {Writer fw = null;if(file == null){file = new File(fileName);}try {fw = new OutputStreamWriter(new FileOutputStream(file,true), "UTF-8");PrintWriter pw = new PrintWriter(fw);for (Object line : rows) {Map<String, Object> lineMap = (Map<String, Object>) line;StringBuffer linebuffer = new StringBuffer();for (Map.Entry<String, Object> entry : lineMap.entrySet()) {if(entry.getValue()!=null && !"".equals(entry.getValue())){linebuffer.append(entry.getValue()).append(",");}else{linebuffer.append(" ,");}}linebuffer.deleteCharAt(linebuffer.length() - 1);linebuffer.append("\r\n");pw.printf(linebuffer.toString(), "\r\n");}pw.flush();pw.close();} catch (Exception e) {throw e;}finally {if(fw!=null) {fw.close();}}} catch (Exception e) {throw e;}}

 

然后将文件通过ftp上传到指定服务器的指定路劲

注:ftpJSch请查看 传送门 ftpJsch工具

try {//连接服务器,通过host,port,用户,密码ftpJSch = FtpJSch.getConnect(host, port, username, password);//将in流上传到ftp指定路劲,并指定文件名ftpJSch.upload(in, ftpPath, fileName + ".txt");ftpJSch.close();fileInfoService.updateSynMigu(fileInfo.getName(), settMonth, fileInfo.getId(),SynStatus.SECCESS.getStatus());LogService.createLog(fileInfo.getName(), "生成文件成功", "FTP生成文件同步到TXT成功");log.info("同步**+ 生成文件成功!id=" + id);if (txtFile.exists()) {if (txtFile.delete()) {log.info("删除本地****平台 生成文件成功!id=" + id);}}return new ResultObject<Object>(true, "生成文件成功!", "");}catch (Exception e){throw e;}

 

这篇关于生成文件且通过FTP上传到别的服务器的指定路劲的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1002163

相关文章

java实现docker镜像上传到harbor仓库的方式

《java实现docker镜像上传到harbor仓库的方式》:本文主要介绍java实现docker镜像上传到harbor仓库的方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地... 目录1. 前 言2. 编写工具类2.1 引入依赖包2.2 使用当前服务器的docker环境推送镜像2.2

mysql中的服务器架构详解

《mysql中的服务器架构详解》:本文主要介绍mysql中的服务器架构,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1、背景2、mysql服务器架构解释3、总结1、背景简单理解一下mysqphpl的服务器架构。2、mysjsql服务器架构解释mysql的架

Golang如何对cron进行二次封装实现指定时间执行定时任务

《Golang如何对cron进行二次封装实现指定时间执行定时任务》:本文主要介绍Golang如何对cron进行二次封装实现指定时间执行定时任务问题,具有很好的参考价值,希望对大家有所帮助,如有错误... 目录背景cron库下载代码示例【1】结构体定义【2】定时任务开启【3】使用示例【4】控制台输出总结背景

springboot项目打jar制作成镜像并指定配置文件位置方式

《springboot项目打jar制作成镜像并指定配置文件位置方式》:本文主要介绍springboot项目打jar制作成镜像并指定配置文件位置方式,具有很好的参考价值,希望对大家有所帮助,如有错误... 目录一、上传jar到服务器二、编写dockerfile三、新建对应配置文件所存放的数据卷目录四、将配置文

Linux如何快速检查服务器的硬件配置和性能指标

《Linux如何快速检查服务器的硬件配置和性能指标》在运维和开发工作中,我们经常需要快速检查Linux服务器的硬件配置和性能指标,本文将以CentOS为例,介绍如何通过命令行快速获取这些关键信息,... 目录引言一、查询CPU核心数编程(几C?)1. 使用 nproc(最简单)2. 使用 lscpu(详细信

python3如何找到字典的下标index、获取list中指定元素的位置索引

《python3如何找到字典的下标index、获取list中指定元素的位置索引》:本文主要介绍python3如何找到字典的下标index、获取list中指定元素的位置索引问题,具有很好的参考价值,... 目录enumerate()找到字典的下标 index获取list中指定元素的位置索引总结enumerat

Java实现删除文件中的指定内容

《Java实现删除文件中的指定内容》在日常开发中,经常需要对文本文件进行批量处理,其中,删除文件中指定内容是最常见的需求之一,下面我们就来看看如何使用java实现删除文件中的指定内容吧... 目录1. 项目背景详细介绍2. 项目需求详细介绍2.1 功能需求2.2 非功能需求3. 相关技术详细介绍3.1 Ja

MySQL MCP 服务器安装配置最佳实践

《MySQLMCP服务器安装配置最佳实践》本文介绍MySQLMCP服务器的安装配置方法,本文结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下... 目录mysql MCP 服务器安装配置指南简介功能特点安装方法数据库配置使用MCP Inspector进行调试开发指

在Windows上使用qemu安装ubuntu24.04服务器的详细指南

《在Windows上使用qemu安装ubuntu24.04服务器的详细指南》本文介绍了在Windows上使用QEMU安装Ubuntu24.04的全流程:安装QEMU、准备ISO镜像、创建虚拟磁盘、配置... 目录1. 安装QEMU环境2. 准备Ubuntu 24.04镜像3. 启动QEMU安装Ubuntu4

Python pip下载包及所有依赖到指定文件夹的步骤说明

《Pythonpip下载包及所有依赖到指定文件夹的步骤说明》为了方便开发和部署,我们常常需要将Python项目所依赖的第三方包导出到本地文件夹中,:本文主要介绍Pythonpip下载包及所有依... 目录步骤说明命令格式示例参数说明离线安装方法注意事项总结要使用pip下载包及其所有依赖到指定文件夹,请按照以