本文主要是介绍生成文件且通过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上传到别的服务器的指定路劲的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!