1

I am writing a program to download file from SFT URL

sftp://demo.wftpserver.com:2222/upload/10MB.zip

but the java URL class doesn't support SFTP as I read multiple post on stackoverflow.

The problem is I have a written a generic URL downloader which downloads file from all other protocol but only sftp is the problem.

Is there anyway I can use URL class itself to download the file or if i have to do it using some other way can I use that to download file from all the sources.

Program I am using

try
{
    bis = new BufferedInputStream(url.openStream());
    String fileName = DownloadSourceUtils.getUniqueFileName(DownloadSourceConstants.DOWNLOAD_LOCALTION, url.getFile());

    File directory = new File(DownloadSourceConstants.DOWNLOAD_LOCALTION);
    if (! directory.exists()){
        directory.mkdir();
        // If you require it to make the entire directory path including parents,
        // use directory.mkdirs(); here instead.
    }

    fos = new FileOutputStream(DownloadSourceConstants.DOWNLOAD_LOCALTION+File.separator+fileName);

    byte dataBuffer[] = new byte[1024];
    int bytesRead;
    while ((bytesRead = bis.read(dataBuffer, 0, 1024)) != -1) 
    {
        fos.write(dataBuffer, 0, bytesRead);
    }
    fos.flush();
    output = url + " downloaded successfully";
    return output;
} 
catch (IOException e)
{
    output = e.getMessage();
    return output;
}
finally
{
    if(bis != null)
        bis.close();
    if(fos != null)
        fos.close();
}
Bakudan
  • 19,134
  • 9
  • 53
  • 73
Abhishek
  • 519
  • 1
  • 6
  • 24

0 Answers0