java实现ftp的几种方式

有二个种FTP方式:ftp和sftp
ftp的包:commons-net-3.3.jar
sftp包:com.jcraft.jsch_0.1.31.jar

❷ Java实现ftp服务器源代码

/**
* 创建日期:Dec 23, 2008
* 类名:Ftp.java
* 类路径:org
* 修改日志:
*/
package org;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

import sun.net.TelnetInputStream;
import sun.net.TelnetOutputStream;
import sun.net.ftp.FtpClient;

/**
* @author 南山一根葱
* @Description ftp操作
*/
public class Ftp {
/**
* 获取Ftp目录下的列表
*/
public void getftpList() {
String server = "";// 输入的FTP服务器的IP地址
String user = "";// 登录FTP服务器的用户名
String password = "";// 登录FTP服务器的用户名的口令
String path = "";// FTP服务器上的路径
try {
FtpClient ftpClient = new FtpClient();// 创建FtpClient对象
ftpClient.openServer(server);// 连接FTP服务器
ftpClient.login(user, password);// 登录FTP服务器
if (path.length() != 0) {
ftpClient.cd(path);
}
TelnetInputStream is = ftpClient.list();
int c;
while ((c = is.read()) != -1) {
System.out.print((char) c);
}
is.close();
ftpClient.closeServer();// 退出FTP服务器
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
}
/**
* 下载FTP上的文件
*/
public void getFtpFile() {
String server = "";// 输入的FTP服务器的IP地址
String user = "";// 登录FTP服务器的用户名
String password = "";// 登录FTP服务器的用户名的口令
String path = "";// FTP服务器上的路径
String filename = "";// 下载的文件名
try {
FtpClient ftpClient = new FtpClient();
ftpClient.openServer(server);
ftpClient.login(user, password);
if (path.length() != 0)
ftpClient.cd(path);
ftpClient.binary();
TelnetInputStream is = ftpClient.get(filename);
File file_out = new File(filename);
FileOutputStream os = new FileOutputStream(file_out);
byte[] bytes = new byte[1024];
int c;
while ((c = is.read(bytes)) != -1) {
os.write(bytes, 0, c);
}
is.close();
os.close();
ftpClient.closeServer();
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
}
/**
* 上传文件到FTP
*/
public void putFtpFile() {
String server = "";// 输入的FTP服务器的IP地址
String user = "";// 登录FTP服务器的用户名
String password = "";// 登录FTP服务器的用户名的口令
String path = "";// FTP服务器上的路径
String filename = "";// 上传的文件名
try {
FtpClient ftpClient = new FtpClient();
ftpClient.openServer(server);
ftpClient.login(user, password);
if (path.length() != 0)
ftpClient.cd(path);
ftpClient.binary();
TelnetOutputStream os = ftpClient.put(filename);
File file_in = new File(filename);
FileInputStream is = new FileInputStream(file_in);
byte[] bytes = new byte[1024];
int c;
while ((c = is.read(bytes)) != -1) {
os.write(bytes, 0, c);
}
is.close();
os.close();
ftpClient.closeServer();
} catch (IOException ex) {
System.out.println(ex.getMessage());
}
}
}

❸ 如何用Java实现FTP服务器

在主函数中,完成服务器端口的侦听和服务线程的创建。我们利用一个静态字符串变量initDir 来保存服务器线程运行时所在的工作目录。服务器的初始工作目录是由程序运行时用户输入的,缺省为C盘的根目录。
具体的代码如下:
public class ftpServer extends Thread{
private Socket socketClient;
private int counter;
private static String initDir;
public static void main(String[] args){
if(args.length != 0) {
initDir = args[0];
}else{ initDir = "c:";}
int i = 1;
try{
System.out.println("ftp server started!");
//监听21号端口
ServerSocket s = new ServerSocket(21);
for(;;){
//接受客户端请求
Socket incoming = s.accept();
//创建服务线程
new ftpServer(incoming,i).start();
i++;
}
}catch(Exception e){}
}

❹ java实现ftp文件操作的方式有哪些

运用类的办法,编程人员能够长途登录到FTP服务器,罗列该服务器上的目录,设置传输协议,以及传送文件。FtpClient类涵 盖了简直一切FTP的功用,FtpClient的实例变量保留了有关树立"署理"的各种信息。下面给出了这些实例变量:

public static boolean useFtpProxy

这个变量用于标明FTP传输过程中是不是运用了一个署理,因此,它实际上是一个符号,此符号若为TRUE,标明运用了一个署理主机。

public static String ftpProxyHost

此变量只要在变量useFtpProxy为TRUE时才有用,用于保留署理主机名。

public static int ftpProxyPort

此变量只要在变量useFtpProxy为TRUE时才有用,用于保留署理主机的端口地址。

FtpClient有三种不同方式的结构函数,如下所示:

1、public FtpClient(String hostname,int port)

此结构函数运用给出的主机名和端口号树立一条FTP衔接。

2、public FtpClient(String hostname)

此结构函数运用给出的主机名树立一条FTP衔接,运用默许端口号。

3、FtpClient()

此结构函数将创立一FtpClient类,但不树立FTP衔接。这时,FTP衔接能够用openServer办法树立。

一旦树立了类FtpClient,就能够用这个类的办法来翻开与FTP服务器的衔接。类ftpClient供给了如下两个可用于翻开与FTP服务器之间的衔接的办法。

public void openServer(String hostname)

这个办法用于树立一条与指定主机上的FTP服务器的衔接,运用默许端口号。

❺ 如何用java代码实现ftp文件上传

import java.io.File;
import java.io.FileInputStream;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;

public class test {

private FTPClient ftp;
/**
*
* @param path 上传到ftp服务器哪个路径下
* @param addr 地址
* @param port 端口号
* @param username 用户名
* @param password 密码
* @return
* @throws Exception
*/
private boolean connect(String path,String addr,int port,String username,String password) throws Exception {
boolean result = false;
ftp = new FTPClient();
int reply;
ftp.connect(addr,port);
ftp.login(username,password);
ftp.setFileType(FTPClient.BINARY_FILE_TYPE);
reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
return result;
}
ftp.changeWorkingDirectory(path);
result = true;
return result;
}
/**
*
* @param file 上传的文件或文件夹
* @throws Exception
*/
private void upload(File file) throws Exception{
if(file.isDirectory()){
ftp.makeDirectory(file.getName());
ftp.changeWorkingDirectory(file.getName());
String[] files = file.list();
for (int i = 0; i < files.length; i++) {
File file1 = new File(file.getPath()+"\\"+files[i] );
if(file1.isDirectory()){
upload(file1);
ftp.changeToParentDirectory();
}else{
File file2 = new File(file.getPath()+"\\"+files[i]);

❻ 怎么用Java实现FTP上传

package com.sinosoft.sepmis.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

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

/**
* Java自带的API对FTP的操作
* @Title:Ftp.java
* @author: shanhong
*/
public class FtpUtil {
/**
* Description: 向FTP服务器上传文件
* @param url FTP服务器hostname
* @param port FTP服务器端口,如果默认端口请写-1
* @param username FTP登录账号
* @param password FTP登录密码
* @param path FTP服务器保存目录
* @param filename 上传到FTP服务器上的文件名
* @param input 输入流
* @return 成功返回true,否则返回false
*/
public static boolean uploadFile(String url, int port, String username, String password, String path,
String filename, InputStream input) throws Exception
{
boolean success = false;
FTPClient ftp = new FTPClient();
try
{
int reply;

// 连接FTP服务器
if (port > -1)
{
ftp.connect(url, port);
}
else
{
ftp.connect(url);
}

// 登录FTP
ftp.login(username, password);
reply = ftp.getReplyCode();
System.out.println(reply);
if (!FTPReply.isPositiveCompletion(reply))
{
ftp.disconnect();
return success;
}
ftp.changeWorkingDirectory(path);
ftp.storeFile(filename, input);
input.close();
ftp.logout();
success = true;
}
catch (IOException e)
{
success = false;
throw e;
}
finally
{
if (ftp.isConnected())
{
try
{
ftp.disconnect();
}
catch (IOException e)
{
throw e;
}
}
}
return success;
}
public static void main(String agrs[]) {
try {
File file = new File("E:\\1.txt");
FileInputStream in = new FileInputStream(file);
/*
* 使用默认的端口号、用户名、密码以及根目录连接FTP服务器
*/
//返回true上传成功,否则上传失败
// FtpUtil.uploadFile("192.168.61.209", -22, "instiaci", "instiaci", "/db2home/instiaci/personal/shanhz","2.txt",in);
//“sinopipi/IC/tkk"目录要是已经存在的目录
FtpUtil.uploadFile("192.168.61.104", 22, "administrator", "123456", "/sinopipi/IC","6.txt",in);
} catch (Exception e) {
e.printStackTrace();
}
}
}
说明:所需的jar包是commons-net-2.0.jar

❼ 怎么用java实现ftp的登陆

/**
*依赖commons-net-3.4.jar,commons-io-2.4.jar
*/
publicclassFtpUtils{
/**
*上传
*@paramhostFTP地址
*@paramport端口ftp默认22,sftp默认23
*@paramuserftp用户名
*@parampwdftp密码
*@paramdestPathFTP文件保存路径
*@paramfileNameftp保存文件名称
*@paramfile需要上传的文件
*/
publicstaticvoipload(Stringhost,intport,Stringuser,Stringpwd,StringdestPath,StringfileName,Filefile){
FTPClientftp=null;
InputStreamfis=null;
try{
//1.建立连接
ftp=newFTPClient();
ftp.connect(host,port);
//2.验证连接地址
intreply=ftp.getReplyCode();
if(FTPReply.isPositiveCompletion(reply)){
ftp.disconnect();
return;
}
//3.登录
ftp.login(user,pwd);
//设置上传路径、缓存、字符集、文件类型等
ftp.changeWorkingDirectory(destPath);
ftp.setBufferSize(1024);
ftp.setControlEncoding("UTF-8");
ftp.setFileType(FTP.BINARY_FILE_TYPE);
//4.上传
fis=newFileInputStream(file);
ftp.storeFile(fileName,fis);
}catch(SocketExceptione){
e.printStackTrace();
}catch(IOExceptione){
e.printStackTrace();
}finally{
IOUtils.closeQuietly(fis);
try{
if(ftp.isAvailable()){
ftp.logout();
}
if(ftp.isConnected()){
ftp.disconnect();
}
//删除上传临时文件
if(null!=file&&file.exists()){
file.delete();
}
}catch(IOExceptione){
e.printStackTrace();
}
}
}
}

❽ java中怎么实现ftp服务器

我知道apache有个commons
net包,其中的
FTPClient
类可以实现客户端和服务之间的
文件传输
,但是我如果使用这种方式的话,就得将一台服务器上的文件传到我本地,再将这个文件传到另一台服务器上,感觉这中间多了一步操作;