0

I need to download a file from ftp which uses basic authentication.

I tried setting the Authenticator class like it is pointed out here: Authenticated HTTP proxy with Java but I cant seem to get it to authenticate. Every time I download the file, it always says invalid username or password.

Am I doing something wrong? How can I be sure that the authenticator is called?

Here is the code I am using to download the file:

Authenticator.setDefault(
        new Authenticator() {
            @Override
            public PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(
                      username, password.toCharArray());
             }
        }
);

PrinterJob job = PrinterJob.getPrinterJob();
for (PrintService printer : PrintServiceLookup.lookupPrintServices(null, null))
{
    if(printer.getName().equalsIgnoreCase(targetPrinters[i]))
    {
        job.setPrintService(printer);
    }
}

PDDocument doc;
URL url = new URL("ftp", "10.16.20.73", 21, file);
doc = PDDocument.load(url);
doc.silentPrint(job);
i++;
Community
  • 1
  • 1
Syakur Rahman
  • 2,056
  • 32
  • 40

1 Answers1

0

For those who might be wondering, I used a different approach in the end to access the ftp files.

I put the username and password in the url string, might not be the best way but it worked for my case.

This is the url string i used:

ftp://user:password@somewhere.com/pub/somefile.xxx
Syakur Rahman
  • 2,056
  • 32
  • 40