java 使用 ftp 上传文件/目录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11554939/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
upload file/directory with ftp
提问by ankit
I have setup openssh server in ububntu machine. I want to upload file and dierctory to ftp server using java api's. So, far i have used two different vendors java api's(jscape, zehon) to upload file/directory.
我在 ububntu 机器上设置了 openssh 服务器。我想使用 java api 将文件和目录上传到 ftp 服务器。所以,到目前为止,我已经使用了两个不同供应商的 java api(jscape, zehon) 来上传文件/目录。
I am able to upload file/directory from local machine to local ftp server. But i am not able to upload from local machine to remote ftp server using api's. But with command line tools like scp or ftp or sftp i am able to upload file/directory from any local machine to remote ftp server.
我能够将文件/目录从本地机器上传到本地 ftp 服务器。但是我无法使用 api 从本地机器上传到远程 ftp 服务器。但是使用 scp 或 ftp 或 sftp 等命令行工具,我可以将文件/目录从任何本地机器上传到远程 ftp 服务器。
I am confused whether i have not installed/configured openssh server properly. or may be i am not able to use (zehon/jscape)java api's correctly.
我很困惑我是否没有正确安装/配置 openssh 服务器。或者我可能无法正确使用 (zehon/jscape)java api。
can u please give me a test code to upoad file and directory using java api.
你能给我一个测试代码来使用java api上传文件和目录吗?
this is the code which i have used to upload file from local machine to remote ftp server. I am using sftp.jar from jscape.
这是我用来将文件从本地机器上传到远程 ftp 服务器的代码。我正在使用 jscape 中的 sftp.jar。
Ftp ftp = new Ftp(hostname,username,password);
ftp.connect();
ftp.setDir(destFolder);
ftp.upload(new File("local file path...");
ftp.disconnect();
Thanks in advance.
提前致谢。
回答by Miljen Mikic
We've been using Apache Commons Netlibrary, and I can recommend it. Example of usage: (I've dropped try-catch block and check whether FtpClient is connected)
我们一直在使用Apache Commons Net库,我可以推荐它。使用示例:(我删除了 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);
回答by NPKR
By using commons-net-3.0.jar provided by Apache you can communicate with Server with FTP.
通过使用 Apache 提供的 commons-net-3.0.jar,您可以通过 FTP 与 Server 进行通信。
FTPUtils Class is having core methods like connect,disconnect,upload and download methods. and FTPMain having main method to upload file.
FTPUtils 类具有核心方法,如连接、断开连接、上传和下载方法。和 FTPMain 有上传文件的主要方法。
FTPUtils:
FTP 实用程序:
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:
FTP主站:
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();
}
}
}