如何在java中给出FTP地址?

发布于 2024-10-09 10:07:03 字数 826 浏览 0 评论 0原文

我已经编写了从 FTP 服务器下载文件的代码。因为我在本地有 FTP 服务器,并且我想像“ftp://localhost/alfresco”一样访问。这是露天的 FTP。

我有以下代码

public class FtpTransfer {
public static final void main(String[] args)
{
    FTPClient ftp = new FTPClient();
    FileOutputStream br = null;
    try
    {
        ftp.connect("ftp://localhost/alfresco");
        ftp.login("admin", "admin");
        String file = "KPUB//Admin//TMM//Pickup//TMM_TO_ARTESIA_06152010220246.xml";

        br = new FileOutputStream("file");
        ftp.retrieveFile("/"+file, br);
        System.out.println("Downloaded...");
    }
    catch(IOException exception) {
        System.out.println("Error : "+exception);
    }
}
}

发生以下异常。

Error : java.net.UnknownHostException: ftp://localhost/alfresco

请告诉我应该如何提供 FTP 主机地址?

I have written the code which downloads the file from FTP server. Since I have my FTP server locally and I want to access like "ftp://localhost/alfresco". It was alfresco's FTP.

I have the following Code

public class FtpTransfer {
public static final void main(String[] args)
{
    FTPClient ftp = new FTPClient();
    FileOutputStream br = null;
    try
    {
        ftp.connect("ftp://localhost/alfresco");
        ftp.login("admin", "admin");
        String file = "KPUB//Admin//TMM//Pickup//TMM_TO_ARTESIA_06152010220246.xml";

        br = new FileOutputStream("file");
        ftp.retrieveFile("/"+file, br);
        System.out.println("Downloaded...");
    }
    catch(IOException exception) {
        System.out.println("Error : "+exception);
    }
}
}

The following exception occurs.

Error : java.net.UnknownHostException: ftp://localhost/alfresco

Please let me know how should I give the FTP Host Address?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

远山浅 2024-10-16 10:07:04
FTPClient f = new FTPClient();
f.connect("localhost");
f.login(username, password);
FTPFile[] files = listFiles(directory);   

另请参阅

FTPClient f = new FTPClient();
f.connect("localhost");
f.login(username, password);
FTPFile[] files = listFiles(directory);   

Also See

小情绪 2024-10-16 10:07:04

下面是一个示例,演示了与服务器的连接、更改当前工作目录、列出目录中的文件以及将文件下载到某个指定的目录。

package test;

import java.io.FileOutputStream;
import java.io.IOException;
import java.net.SocketException;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;

public class FtpTransfer {
 public static final void main(String[] args) throws SocketException, IOException {
  FTPClient ftp = new FTPClient();
  ftp.connect("ftp.somedomain.com"); // or "localhost" in your case
  System.out.println("login: "+ftp.login("username", "pass"));

  ftp.changeWorkingDirectory("folder/subfolder/");
  // list the files of the current directory
  FTPFile[] files = ftp.listFiles();  
  System.out.println("Listed "+files.length+" files.");
  for(FTPFile file : files) {
   System.out.println(file.getName());
  }
   // lets pretend there is a JPEG image in the present folder that we want to copy to the desktop (on a windows machine)
  ftp.setFileType(FTPClient.BINARY_FILE_TYPE); // don't forget to change to binary mode! or you will have a scrambled image!
        FileOutputStream br = new FileOutputStream("C:\\Documents and Settings\\casonkl\\Desktop\\my_downloaded_image_new_name.jpg");

  ftp.retrieveFile("name_of_image_on_server.jpg", br);
  ftp.disconnect();

 }
}

Here is an example demonstrating connection to a server, changing present working directory, listing files in a directory and downloading a file to some specified directory.

package test;

import java.io.FileOutputStream;
import java.io.IOException;
import java.net.SocketException;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;

public class FtpTransfer {
 public static final void main(String[] args) throws SocketException, IOException {
  FTPClient ftp = new FTPClient();
  ftp.connect("ftp.somedomain.com"); // or "localhost" in your case
  System.out.println("login: "+ftp.login("username", "pass"));

  ftp.changeWorkingDirectory("folder/subfolder/");
  // list the files of the current directory
  FTPFile[] files = ftp.listFiles();  
  System.out.println("Listed "+files.length+" files.");
  for(FTPFile file : files) {
   System.out.println(file.getName());
  }
   // lets pretend there is a JPEG image in the present folder that we want to copy to the desktop (on a windows machine)
  ftp.setFileType(FTPClient.BINARY_FILE_TYPE); // don't forget to change to binary mode! or you will have a scrambled image!
        FileOutputStream br = new FileOutputStream("C:\\Documents and Settings\\casonkl\\Desktop\\my_downloaded_image_new_name.jpg");

  ftp.retrieveFile("name_of_image_on_server.jpg", br);
  ftp.disconnect();

 }
}
蛮可爱 2024-10-16 10:07:04

尝试从您的网址中删除协议(“ftp://”)。

请查看示例

Try remove protocol ("ftp://") from your url.

And please, look at the example.

洒一地阳光 2024-10-16 10:07:04

FTPClient.connect() 方法采用服务器的名称,而不是 URL。尝试:

ftp.connect("localhost");

此外,您可能需要将 alfresco 放在其他地方。如果它是文件路径的一部分,

String file = "alfresco/KPUB//Admin//TMM//Pickup//TMM_TO_ARTESIA_06152010220246.xml";

The FTPClient.connect() method takes the name of a server, not a URL. Try:

ftp.connect("localhost");

Also, you may need to put alfresco somewhere else. If it's part of the file path,

String file = "alfresco/KPUB//Admin//TMM//Pickup//TMM_TO_ARTESIA_06152010220246.xml";
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文