From 9d08163be61c5a825ca6d1845d47150ab8ab87e4 Mon Sep 17 00:00:00 2001 From: gleimar Date: Fri, 2 Jun 2017 18:37:49 +0000 Subject: [PATCH] fixes bug#9119 git-svn-id: http://desenvolvimento.rjconsultores.com.br/repositorio/sco/Integracion/IntegracaoReceitaDespesa/trunk/IntegracaoReceitaDespesa@69593 d1611594-4594-4d17-8e1d-87c2c4800839 --- .../BGMApplication.java | 39 ++++++++++++++++++- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/src/com/rjconsultores/integracaoreceitadespesa/BGMApplication.java b/src/com/rjconsultores/integracaoreceitadespesa/BGMApplication.java index ee9bcd800..f700550f7 100644 --- a/src/com/rjconsultores/integracaoreceitadespesa/BGMApplication.java +++ b/src/com/rjconsultores/integracaoreceitadespesa/BGMApplication.java @@ -6,6 +6,7 @@ import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; +import java.nio.channels.FileChannel; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; @@ -70,7 +71,7 @@ public class BGMApplication { return props; } - public String executaExportacao(Date dataInicio, Date dataFinal, String diretorio, Integer empresaId, Integer puntoVentaId, Connection con) throws IllegalArgumentException{ + public String executaExportacao(Date dataInicio, Date dataFinal, String diretorio, Integer empresaId, Integer puntoVentaId, Connection con,String pathGravacaoExternaArquivos) throws IllegalArgumentException{ if (empresaId == null){ throw new IllegalArgumentException("o parâmetro empesaId é obrigatório!"); } @@ -100,6 +101,9 @@ public class BGMApplication { log.debug("gerando arquivo..."); Arquivo.GravaArquivo(file.getAbsolutePath(), totalbus.getDespesasReceitas(puntoVentaId, empresaId, cal.getTime())); + + this.copiarArquivoExternamente(file, pathGravacaoExternaArquivos); + // adiciona um dia para iteração cal.add(Calendar.DAY_OF_MONTH, 1); files.add(file); @@ -121,7 +125,38 @@ public class BGMApplication { } } } - + private void copiarArquivoExternamente(File arquivoBGM,String pathGravacaoExternaArquivos){ + if (pathGravacaoExternaArquivos == null){ + log.info("Path externo para gravação de arquivos não configurado"); + + return; + } + + if (!pathGravacaoExternaArquivos.endsWith("/")){ + pathGravacaoExternaArquivos = pathGravacaoExternaArquivos +"/"; + } + + File destino = new File(pathGravacaoExternaArquivos + arquivoBGM.getName()); + + try { + this.copyFileUsingChannel(arquivoBGM, destino); + } catch (IOException e) { + log.error(String.format("Erro ao gravar o arquivo %s no diretorio %s", arquivoBGM.getName(),pathGravacaoExternaArquivos) ,e); + } + } + private void copyFileUsingChannel(File source, File dest) throws IOException { + + FileChannel sourceChannel = null; + FileChannel destChannel = null; + try { + sourceChannel = new FileInputStream(source).getChannel(); + destChannel = new FileOutputStream(dest).getChannel(); + destChannel.transferFrom(sourceChannel, 0, sourceChannel.size()); + }finally{ + sourceChannel.close(); + destChannel.close(); + } + } private void excluirArquivosZip(String diretorio){ File pasta = new File(diretorio); File[] arquivos = pasta.listFiles();