1

我有通过 ftp 下载文件的 java 代码,下载文件后,它转到默认路径。指定的目标路径没有下载的文件。为什么?我的代码是,

  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();
   }
  }

  }
        }

       }

下载的文件“ddd.txt”不在“D:/rvenktesan/Software”中。它位于“D:rvenkatesan/JAVA PROJECTS”中。为什么?指导我将文件存储在指定路径中?预先感谢。

4

1 回答 1

1

您的问题是FileOutputStream(destination.getName() ) ); 将其更改为:FileOutputStream(destination.getAbsolutePath() ) );

getName 将仅返回文件名“ddd.txt”。我假设您正在启动您的应用程序D:/rvenkatesan/JAVA PROJECTS

于 2011-01-25T11:14:49.000 回答