如何在java中提供FTP地址?

如何在java中提供FTP地址?,java,ftp,apache-commons,Java,Ftp,Apache Commons,我已经写了从FTP服务器下载文件的代码。因为我有我的FTP服务器在本地,我想访问像ftp://localhost/alfresco. 这是alfresco的FTP 我有以下代码 public class FtpTransfer { public static final void main(String[] args) { FTPClient ftp = new FTPClient(); FileOutputStream br = null; try {

我已经写了从FTP服务器下载文件的代码。因为我有我的FTP服务器在本地,我想访问像ftp://localhost/alfresco. 这是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主机地址

尝试从url中删除协议ftp://


请查看。

尝试从您的url中删除协议ftp://

请看。

该方法采用服务器名称,而不是URL。尝试:

ftp.connect("localhost");
此外,你可能需要把露天放在其他地方。如果它是文件路径的一部分

String file = "alfresco/KPUB//Admin//TMM//Pickup//TMM_TO_ARTESIA_06152010220246.xml";
该方法采用服务器的名称,而不是URL。尝试:

ftp.connect("localhost");
此外,你可能需要把露天放在其他地方。如果它是文件路径的一部分

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

也看到


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

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

 }
}

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

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

 }
}

+1.工作顺利,格雷格。谢谢在我的代码中,我给出了文件名。相反,我给出了路径,我需要列出路径中的文件名。有可能吗。如果是这样,请给我你的宝贵建议。再次感谢。它在工作,格雷格。谢谢在我的代码中,我给出了文件名。相反,我给出了路径,我需要列出路径中的文件名。有可能吗。如果是这样,请给我你的宝贵建议。再次感谢。我不确定用户名和密码是否是有效参数。我刚刚下载了最新的commons.net jar,唯一的参数是字符串主机名、InetAddress地址和各种组合中的int-port。我不确定用户名和密码是否是有效参数。我刚刚下载了最新的commons.net jar,唯一的参数是字符串hostname、InetAddress address和int-port的各种组合。另请注意,当ftping到其他域(如mysite.com)时,您可以将ftp.mysite.com或mysite.com传递到connect方法。但不会工作。一旦检索,我下载的文件将驻留在其中。它将位于您运行程序的目录中。许多文件需要以二进制形式传输,因此要将FTP置于二进制模式类型:ftpClient.setFileTypeFTPClient.binary\u file\u type;我们是否可以明确给出检索文件必须驻留的目录。另一个注意,当ftping到其他域(如mysite.com)时,您可以将ftp.mysite.com或mysite.com传递给connect方法。但不会工作。一旦检索,我下载的文件将驻留在其中。它将位于您运行程序的目录中。许多文件需要以二进制形式传输,因此要将FTP置于二进制模式类型:ftpClient.setFileTypeFTPClient.binary\u file\u type;我们能否明确给出检索文件必须驻留的目录。谢谢您的回答。我正在解决这个问题。谢谢大家,谢谢你们的回答。我正在解决这个问题。谢谢大家。