【问题标题】:Ftp file downloaded path has problem?ftp文件下载路径有问题?
【发布时间】:2011-01-25 19:14:49
【问题描述】:

我有通过 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”中。为什么?指导我将文件存储在指定路径中?非常感谢。

【问题讨论】:

  • 我的问题还是没有解决......
  • 是的,我找到了解决方案。在 FileInputStream(destination.getAbsoluteFilePath()) 而不是 'destination.getName()' 中。感谢您的回复。

标签: java download ftp-client


【解决方案1】:

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

getName 将仅返回文件名“ddd.txt”。我假设你是从D:/rvenkatesan/JAVA PROJECTS开始你的应用程序

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-06-26
    • 1970-01-01
    • 2014-03-03
    • 1970-01-01
    • 2012-01-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多