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 に答える