生成文件且通过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

相关文章

Django HTTPResponse响应体中返回openpyxl生成的文件过程

《DjangoHTTPResponse响应体中返回openpyxl生成的文件过程》Django返回文件流时需通过Content-Disposition头指定编码后的文件名,使用openpyxl的sa... 目录Django返回文件流时使用指定文件名Django HTTPResponse响应体中返回openp

Python极速搭建局域网文件共享服务器完整指南

《Python极速搭建局域网文件共享服务器完整指南》在办公室或家庭局域网中快速共享文件时,许多人会选择第三方工具或云存储服务,但这些方案往往存在隐私泄露风险或需要复杂配置,下面我们就来看看如何使用Py... 目录一、android基础版:HTTP文件共享的魔法命令1. 一行代码启动HTTP服务器2. 关键参

SpringBoot改造MCP服务器的详细说明(StreamableHTTP 类型)

《SpringBoot改造MCP服务器的详细说明(StreamableHTTP类型)》本文介绍了SpringBoot如何实现MCPStreamableHTTP服务器,并且使用CherryStudio... 目录SpringBoot改造MCP服务器(StreamableHTTP)1 项目说明2 使用说明2.1

python生成随机唯一id的几种实现方法

《python生成随机唯一id的几种实现方法》在Python中生成随机唯一ID有多种方法,根据不同的需求场景可以选择最适合的方案,文中通过示例代码介绍的非常详细,需要的朋友们下面随着小编来一起学习学习... 目录方法 1:使用 UUID 模块(推荐)方法 2:使用 Secrets 模块(安全敏感场景)方法

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