Forum Stats

  • 3,734,173 Users
  • 2,246,901 Discussions
  • 7,857,150 Comments

Discussions

pdf file transfer using ftp in java

1001638
1001638 Member Posts: 36


Hi,

I am tried to get a pdf file from ftp. I am using the following code. This code works fine with .txt files but not with .pdf files. The pdf file generated is empty.

import java.awt.Desktop;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import org.apache.commons.net.ftp.FTPClient;

public class FTPConn {
       public static void main(String[] args) throws FileNotFoundException {
        FTPClient f = new FTPClient();

        try {
            f.connect(ip);
            f.login("username", "password");

            File dir = new File("dir");

            dir.mkdir();

            File myFile = new File(dir, "myfile19.pdf");

            myFile.createNewFile();

            FileOutputStream fout = new FileOutputStream(myFile);

            String content= fout.toString();

            boolean success = f.retrieveFile("/location on ftp/ftp.pdf", fout);

            System.out.println("Content :"+content);

            if (success) {

                fout.flush();

                fout.close();

            } else {

                System.out.println("Reterive failure");

            }

            Desktop d = Desktop.getDesktop();

            d.open(myFile);

        }catch(Exception e){

            e.printStackTrace();

        }

    }

This discussion has been closed.