1

I have an old project and I am very lost.

I am downloading a file from an FTP but it is not downloaded in its entirety, it is generated before it is fully completed.

Every time I give to download it is of a different size and very inferior to its real size

FTPClient ftpClient = new FTPClient();
    ftpClient.connect("xxxx");
    ftpClient.login("xxx", "xxxx");
    ftpClient.enterLocalPassiveMode();
    ftpClient.setFileType(FTP.BINARY_FILE_TYPE);
    ftpClient.changeWorkingDirectory("/");

    InputStream in = ftpClient.retrieveFileStream(consentimiento.getRutaFichero());
    BufferedInputStream objLector = new BufferedInputStream(in);

    FacesContext objContexto = FacesContext.getCurrentInstance();
    HttpServletResponse response = (HttpServletResponse) objContexto.getExternalContext().getResponse();

    String contentType = Utilidades.obtenerTipoDocumento("wav");
    response.setContentType(contentType);
    response.setContentLength(in.available());
    response.setHeader("Content-disposition", "attachment; filename=\"" + consentimiento.getNombreFichero() + "\"");

    BufferedOutputStream objGrabador = new BufferedOutputStream(response.getOutputStream());

    // Traspasar el contenido del fichero al objeto response
    for (int data; (data = objLector.read()) != -1;) {
        objGrabador.write(data);
    }

    objGrabador.flush();
    objGrabador.close();
    objLector.close();

    objContexto.responseComplete();
Jose
  • 1,779
  • 4
  • 26
  • 50
  • So if you use a fixed server local file (so no ftp) and download it via jsf it works? – Kukeltje May 21 '19 at 09:19
  • Where are you actually writing the file? – Steve Smith May 21 '19 at 09:20
  • @stevesmith: op is writing it here: `objGrabador.write(data);` – Kukeltje May 21 '19 at 09:22
  • @Kukeltje Yes, but it doesn't seem to be writing to a file but rather a stream. It seems to be more of an FTP proxy or something? – Steve Smith May 21 '19 at 09:23
  • I make a download, but make the download of the file, before it is complete. – Jose May 21 '19 at 09:24
  • You could try outputing the value of `data` to the screen, and see how it compares to the actual data you are expecting. – Steve Smith May 21 '19 at 09:26
  • 1
    What @SteveSmith sort of means is try reading the file from FTP to your server and then in a second step write the content of the file from your server to the end user. If that works, you narrowed things down. If that does not work, you narrowed things down too. Debug... root cause investigation, – Kukeltje May 21 '19 at 09:45

0 Answers0