JSch - buscar arquivo no servidor - resolvido

JSch

Estou buscando arquivo via servidor, ele baixa, na pasta src/main/resource

Como retornar este arquivo para o frontend ?

image

public ArquivoDTO buscarPorIdNota(final String idNotaFiscal) {
        try {
            final NotaFiscalArquivo entidade = repository.buscarPorIdNotaFiscal(idNotaFiscal);
            final String pasta = service.buscar(entidade.getArquivo());
            final File file = new File(this.getClass().getResource(pasta).getFile());
            return ArquivoDTO.builder().arquivo(Files.readAllBytes(file.toPath())).contentType(entidade.getArquivo().getContentType()).nome(entidade.getArquivo().getNome()).principal("NAO").tamanho(entidade.getArquivo().getTamanho()).build();
        } catch (final IOException e) {
            log.error(e.getMessage(), e);
            throw new GeralException("Erro ao baixar o arquivo no servidor !");
        }

    }
    @Transactional
    public String buscar(final Arquivo arquivo) {
        String arquivoPasta = null;
        if (StringUtils.isNotEmpty(arquivo.getUrl())) {
            try {
                final Pair<ChannelSftp, Session> retorno = conectar();
                final Session session = retorno.getRight();
                final ChannelSftp sftpChannel = retorno.getLeft();
                arquivoPasta = "/" + arquivo.getNome();
                sftpChannel.get(arquivo.getUrl(), arquivoPasta);
                desconectar(session, sftpChannel);
            } catch (final SftpException e) {
                log.error(e.getMessage(), e);
                throw new GeralException("Erro ao baixar o arquivo no servidor !");
            }
        }
        return arquivoPasta;
    }
1 curtida