Question

I have java code for file download through ftp, after download the file, it goes to default path. The specified destination path is not having the downloaded file. Why? my code is,

  public class ftpUpload1
  {    

   public static void main(String a[]) throws IOException
{
          ftpUpload1 obj = new ftpUpload1();
          URL url1 = new URL("ftp://vbalamurugan:vbalamurugan@192.168.6.38/ddd.txt" );
 File dest = new File("D:/rvenkatesan/Software/ddd.txt");
       obj.ftpDownload(dest, url1);

     public void ftpDownload(File destination,URL url) throws IOException
 { 
 BufferedInputStream bis = null;
 BufferedOutputStream bos = null;
 try
 {
  URLConnection urlc = url.openConnection();



bis = new BufferedInputStream( urlc.getInputStream() );
   bos = new BufferedOutputStream( new 
                  FileOutputStream(destination.getName() ) );

   int i;
   //read byte by byte until end of stream
   while ((i = bis.read())!= -1)
   {
    // bos.write(i);
    bos.write(i);
   }
   System.out.println("File Downloaded Successfully");
  }
  finally
  {
   if (bis != null)
    try
   {
     bis.close();
   }
   catch (IOException ioe)
   {
    ioe.printStackTrace();
   }
   if (bos != null)
    try
   {
     bos.close();
   }
   catch (IOException ioe)
   {
    ioe.printStackTrace();
   }
  }

  }
        }

       }

The downloaded file "ddd.txt" not in the "D:/rvenktesan/Software". It is located in "D:rvenkatesan/JAVA PROJECTS". Why? guide me to store the file in specified path? Thanks in adcance.

Was it helpful?

Solution

You problem is FileOutputStream(destination.getName() ) ); change this to: FileOutputStream(destination.getAbsolutePath() ) );

getName wil return the filename "ddd.txt" only. I assume you are starting your app from D:/rvenkatesan/JAVA PROJECTS

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top