生成文件且通过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 {
        // 写入txt
        try {
            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 {
        // 写入txt
        try {
            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;
                }

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值