Java 使用ftp上载文件/目录
我已经在ubntu机器上安装了openssh服务器。 我想使用JavaAPI将文件和目录上传到ftp服务器。 到目前为止,我使用了两个不同的供应商JavaAPI的scape,zehon来上传文件/目录 我能够将文件/目录从本地计算机上载到本地ftp服务器。 但我无法使用api从本地计算机上传到远程ftp服务器。 但有了命令行工具,如scp、ftp或sftp,我可以将文件/目录从任何本地机器上传到远程ftp服务器 我不清楚我是否没有正确安装/配置openssh服务器。 或者可能是我不能正确使用zehon/jscapejava api 你能给我一个测试代码,用JavaAPI上传文件和目录吗 这是我用来将文件从本地机器上传到远程ftp服务器的代码。 我正在使用jscape中的sftp.jarJava 使用ftp上载文件/目录,java,ftp,Java,Ftp,我已经在ubntu机器上安装了openssh服务器。 我想使用JavaAPI将文件和目录上传到ftp服务器。 到目前为止,我使用了两个不同的供应商JavaAPI的scape,zehon来上传文件/目录 我能够将文件/目录从本地计算机上载到本地ftp服务器。 但我无法使用api从本地计算机上传到远程ftp服务器。 但有了命令行工具,如scp、ftp或sftp,我可以将文件/目录从任何本地机器上传到远程ftp服务器 我不清楚我是否没有正确安装/配置openssh服务器。 或者可能是我不能正确使用ze
Ftp ftp = new Ftp(hostname,username,password);
ftp.connect();
ftp.setDir(destFolder);
ftp.upload(new File("local file path...");
ftp.disconnect();
提前感谢。通过使用Apache提供的commons-net-3.0.jar,您可以通过FTP与服务器通信 FTPUtils类有一些核心方法,比如连接、断开连接、上传和下载方法。 FTPMain具有上传文件的主要方法 FTPUtils:
package com.ftpclient.test;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
public class FTPUtils {
public static void ftpConnect(FTPClient ftpclient, String host, String username, String password) throws IOException {
System.out.println("FTPUtils :: Logging in FTP..");
try{
ftpclient.connect(host);
if (!ftpclient.login(username, password)) {
throw new IOException("Supplied wrong credentials to FTP Server");
}
if (ftpclient.getReplyCode() != 0) {
System.out.println(ftpclient.getReplyString());
}
}catch(IOException ioe){
ioe.printStackTrace();
System.out.println("FTP Client is not able to Connect to host");
throw new IOException("FTP Client is not able to Connect to host");
}
System.out.println("FTPUtils :: FTP Login Successful..");
}
/**
* disconnect to FTP server
*
* @param ftpclient is Object which is having details of FTP server like IP, user name and password
* @throws IOException throws Exception
*/
public static void ftpDisConnect(FTPClient ftpclient) throws IOException {
System.out.println("FTPUtils :: FTP Logging out..");
ftpclient.logout();
ftpclient.disconnect();
System.out.println("FTPUtils :: FTP Disconnected Successfully..");
}
/**
* download's file from source path to destination path by using FTP Client.
*
* @param ftpclient is Object which is having details of FTP server like IP, user name and password
* @param sourcePath is String from where to download's file
* @param destinationPath is String to where to download's file.
* @return boolean true if download's with out any fail else false
* @throws IOException will throw any problem with file system
*/
public static boolean downloadFile(FTPClient ftpclient, String sourcePath, String destinationPath) throws IOException {
System.out.println("FTPUtils :: RemoteFile download starts ..FTP SOURCE " + sourcePath + " DESTINATION " + destinationPath);
FileOutputStream fos = null;
boolean result = false;
try{
ftpclient.setFileTransferMode(FTP.BINARY_FILE_TYPE);
ftpclient.setFileType(FTP.BINARY_FILE_TYPE);
File fDestination = new File(destinationPath);
fos = new FileOutputStream(fDestination);
result = ftpclient.retrieveFile(sourcePath, fos);
if (result) {
System.out.println("FTPUtils :: RemoteFile download Completed..FTP " + sourcePath);
}
}catch(IOException ioe){
ioe.printStackTrace();
System.out.println("FTP is not able to Download the files from host");
throw new IOException("FTP is not able to Download the files from host");
}finally{
fos.close();
}
return result;
}
/**
* @param ftpclient
* @param sourcePath
* @param destinationPath
* @throws IOException
*/
public static void uploadFile(FTPClient ftpclient, String sourcePath, String destinationPath) throws IOException {
FileInputStream fis = null;
try {
//
// Create an InputStream of the file to be uploaded
//
fis = new FileInputStream(sourcePath);
//
// Store file to server
//
ftpclient.storeFile(destinationPath, fis);
}catch(IOException ioe){
ioe.printStackTrace();
System.out.println("FTP is not able to upload the files from host");
throw new IOException("FTP is not able to upload the files from host");
}finally{
fis.close();
}
}
}
FTPMain:
import java.io.IOException;
import org.apache.commons.net.ftp.FTPClient;
public class FTPMain {
public static void main(String[] str){
FTPClient ftpclient = new FTPClient();
try {
FTPUtils.ftpConnect(ftpclient, "ipaddress", "username", "password");
FTPUtils.uploadFile(ftpclient, "sourcePath", "destinationPath");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
通过使用Apache提供的commons-net-3.0.jar,您可以通过FTP与服务器通信 FTPUtils类有一些核心方法,比如连接、断开连接、上传和下载方法。 FTPMain具有上传文件的主要方法 FTPUtils:
package com.ftpclient.test;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.commons.net.ftp.FTP;
import org.apache.commons.net.ftp.FTPClient;
public class FTPUtils {
public static void ftpConnect(FTPClient ftpclient, String host, String username, String password) throws IOException {
System.out.println("FTPUtils :: Logging in FTP..");
try{
ftpclient.connect(host);
if (!ftpclient.login(username, password)) {
throw new IOException("Supplied wrong credentials to FTP Server");
}
if (ftpclient.getReplyCode() != 0) {
System.out.println(ftpclient.getReplyString());
}
}catch(IOException ioe){
ioe.printStackTrace();
System.out.println("FTP Client is not able to Connect to host");
throw new IOException("FTP Client is not able to Connect to host");
}
System.out.println("FTPUtils :: FTP Login Successful..");
}
/**
* disconnect to FTP server
*
* @param ftpclient is Object which is having details of FTP server like IP, user name and password
* @throws IOException throws Exception
*/
public static void ftpDisConnect(FTPClient ftpclient) throws IOException {
System.out.println("FTPUtils :: FTP Logging out..");
ftpclient.logout();
ftpclient.disconnect();
System.out.println("FTPUtils :: FTP Disconnected Successfully..");
}
/**
* download's file from source path to destination path by using FTP Client.
*
* @param ftpclient is Object which is having details of FTP server like IP, user name and password
* @param sourcePath is String from where to download's file
* @param destinationPath is String to where to download's file.
* @return boolean true if download's with out any fail else false
* @throws IOException will throw any problem with file system
*/
public static boolean downloadFile(FTPClient ftpclient, String sourcePath, String destinationPath) throws IOException {
System.out.println("FTPUtils :: RemoteFile download starts ..FTP SOURCE " + sourcePath + " DESTINATION " + destinationPath);
FileOutputStream fos = null;
boolean result = false;
try{
ftpclient.setFileTransferMode(FTP.BINARY_FILE_TYPE);
ftpclient.setFileType(FTP.BINARY_FILE_TYPE);
File fDestination = new File(destinationPath);
fos = new FileOutputStream(fDestination);
result = ftpclient.retrieveFile(sourcePath, fos);
if (result) {
System.out.println("FTPUtils :: RemoteFile download Completed..FTP " + sourcePath);
}
}catch(IOException ioe){
ioe.printStackTrace();
System.out.println("FTP is not able to Download the files from host");
throw new IOException("FTP is not able to Download the files from host");
}finally{
fos.close();
}
return result;
}
/**
* @param ftpclient
* @param sourcePath
* @param destinationPath
* @throws IOException
*/
public static void uploadFile(FTPClient ftpclient, String sourcePath, String destinationPath) throws IOException {
FileInputStream fis = null;
try {
//
// Create an InputStream of the file to be uploaded
//
fis = new FileInputStream(sourcePath);
//
// Store file to server
//
ftpclient.storeFile(destinationPath, fis);
}catch(IOException ioe){
ioe.printStackTrace();
System.out.println("FTP is not able to upload the files from host");
throw new IOException("FTP is not able to upload the files from host");
}finally{
fis.close();
}
}
}
FTPMain:
import java.io.IOException;
import org.apache.commons.net.ftp.FTPClient;
public class FTPMain {
public static void main(String[] str){
FTPClient ftpclient = new FTPClient();
try {
FTPUtils.ftpConnect(ftpclient, "ipaddress", "username", "password");
FTPUtils.uploadFile(ftpclient, "sourcePath", "destinationPath");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
我们一直在使用图书馆,我可以推荐它。用法示例:我已删除try-catch块并检查FtpClient是否已连接
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
..
private FTPClient ftpClient; //needs to be initialized
..
ftpClient.setConnectTimeout(timeout);
ftpClient.setDataTimeout(timeout);
ftpClient.setDefaultTimeout(timeout);
ftpClient.connect(hostname, port);
ftpClient.setSoTimeout(timeout);
ftpClient.login(username, password);
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE); // or FtpClient.ASCII_FILE_TYPE, up to you
ftpClient.changeWorkingDirectory(destFolder);
srcFile = new File("local file path.."); // replace with actual path
FileInputStream fis = new FileInputStream(srcFile);
ftpClient.storeFile(filename, fis);
我们一直在使用图书馆,我可以推荐它。用法示例:我已删除try-catch块并检查FtpClient是否已连接
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
..
private FTPClient ftpClient; //needs to be initialized
..
ftpClient.setConnectTimeout(timeout);
ftpClient.setDataTimeout(timeout);
ftpClient.setDefaultTimeout(timeout);
ftpClient.connect(hostname, port);
ftpClient.setSoTimeout(timeout);
ftpClient.login(username, password);
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE); // or FtpClient.ASCII_FILE_TYPE, up to you
ftpClient.changeWorkingDirectory(destFolder);
srcFile = new File("local file path.."); // replace with actual path
FileInputStream fis = new FileInputStream(srcFile);
ftpClient.storeFile(filename, fis);
sftp或scp不是ftp。openssh只能提供sftp或scp。它不能提供openssh。请改用vsftpd。sftp或scp不是ftp。openssh只能提供sftp或scp。它不能提供openssh。请改为尝试vsftpd。嗨,Mijen,我尝试了您的解决方案,但我仍然面临相同的问题,因为我能够将文件从本地计算机上载到本地ftp服务器,但无法将文件从本地计算机上载到远程ftp服务器。然后,问题不在使用的库中,而是在其他地方。您能否通过shell(即使用普通FTP命令)连接到FTP服务器?当你试图运行我的程序时,你会遇到什么错误?嗨,Mijen,这段代码在部署了这个webapp并安装了openssh服务器的服务器上运行,但是在客户端机器上运行什么代码,以便他可以将文件从本地机器上载到远程ftp服务器。嗨,请尝试回答我在前面评论中提出的问题,如果没有更多信息,很难找到问题的根源。嗨,Mijen,我尝试了你的解决方案,但我仍然面临着同样的问题,因为我能够将文件从本地计算机上载到本地ftp服务器,但无法将文件从本地机上载到远程ftp服务器。然后问题不在使用的库中,而是在其他地方。您能否通过shell(即使用普通FTP命令)连接到FTP服务器?当你试图运行我的程序时,你会遇到什么错误?嗨,Mijen,这段代码在部署了这个webapp并安装了openssh服务器的服务器上运行,但是在客户端机器上运行什么代码,以便他可以将文件从本地机器上载到远程ftp服务器。嗨,请尝试回答我在前面评论中提出的问题,如果没有更多的信息,就很难找到问题的根源。