Estou gerando o txt da seguinte forma:
PrintStream fileSaida;
Estabelecimento estabelecimento = totalizacao.getEstabelecimento();
TotalizacaoArquivo totalizar = totalizacao;
saida = new FileOutputStream("endereco_do_arquivo_no_servidor" + totalizacao.getNomeArquivo() + estabelecimento.getId() + ".txt");
//saida = new FileOutputStream("d:\\" + totalizacao.getNomeArquivo() + estabelecimento.getId() + ".txt");
fileSaida = new PrintStream(saida);
//System.out.println("Registro 1");
fileSaida.format("%1$s%2$1d%3$014d%4$-50s%5$-50s%6$-2s%7$02d%8$-4d%9$-50s%10$1d", "RG1", estabelecimento.getTipoInformante().getId(), estabelecimento.getCnpj(), estabelecimento.getRazao_social(), estabelecimento.getEndereco(), estabelecimento.getUf(), estabelecimento.getMes_referencia(), estabelecimento.getAno_referencia(), estabelecimento.getResponsavel(), estabelecimento.getEstruturaArquivo().getId());
fileSaida.println();
//System.out.println("Registro 2");
fileSaida.format("%1$s%2$06d%3$-20s%4$-20s%5$-12s%6$-12s%7$-12s%8$-12s%9$1s%10$-7s%11$-7s%12$-40s%13$-14d%14$-15s%15$-15s%16$-48s%17$2s%18$06d%19$8s%20$1d%21$1d", "RG2",
est.getCnie(), est.getNumero_fabricacao(), est.getNumero_serie_mfd(), est.getLc_externo_1(), est.getLc_externo_2(),
est.getLc_externo_3(),est.getLc_externo_4(), est.getTpdispbasico().getId(), est.getNumero_dp_pt_basico(), est.getLc_mfd(),
est.getRazao_social_nome(), est.getCnpj_cpf(), est.getInsc_estadual(), est.getInsc_municipal(), est.getEndereco(), est.getUf(), est.getNumero_nf(), dt, est.getFinalidadeecf().getId(), est.getRelinttecnica().getId());
fileSaida.println();
//System.out.println("Registro 3");
fileSaida.format("%1$s%2$-14d%3$s%4$06d", "RG3",
totalizar.getEstabelecimento().getCnpj(), totalizar.getIndicadorMov(), totalizar.getQntRegistros());
Download do arquivo txt:
public void doGet(HttpServletRequest request, HttpServletResponse resp) throws ServletException, IOException {
String filename = request.getParameter("arquivo");
File f = new File("endereco_do_arquivo_no_servidor" + filename);
//File f = new File("d:\\" + filename);
int BUFSIZE = 1024;
int length = 0;
ServletOutputStream op = resp.getOutputStream();
ServletContext context = getServletConfig().getServletContext();
String mimetype = context.getMimeType(filename);
//
// Set the response and go!
//
//
resp.setContentType((mimetype != null) ? mimetype : "application/octet-stream");
resp.setContentLength((int) f.length());
resp.setHeader("Content-Disposition", "attachment; filename=\"" + filename + "\"");
//
// Stream to the requester.
//
byte[] bbuf = new byte[BUFSIZE];
DataInputStream in = new DataInputStream(new FileInputStream(f));
while ((in != null) && ((length = in.read(bbuf)) != -1)) {
op.write(bbuf, 0, length);
}
in.close();
op.flush();
op.close();
// }
}
Quando gero o arquivo ele gera corretamente, inclusive com as quebras de linha de um registro para o outro, entretanto,
quando eu faço o download do arquivo o mesmo não quebra as linhas e vem tudo em uma linha só.