当前位置:首页 » 文件管理 » javaftp客户端

javaftp客户端

发布时间: 2023-02-10 05:42:37

java ftp 有哪些工具类

java ftp 最常用的是apache commons-net


commons-net项目中封装了各种网络协议的客户端,支持的协议包括:



  • FTP

  • NNTP

  • SMTP

  • POP3

  • Telnet

  • TFTP

  • Finger

  • Whois

  • rexec/rcmd/rlogin

  • Time (rdate) and Daytime

  • Echo

  • Discard

  • NTP/SNTP




其他的还有FTP4J ,jftp

⑵ java ftp

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;

import sun.net.ftp.FtpClient;

/**
*
* @author chenc
* @version 1.0
*
* 2008-02-19
*
*/
public class Ftp {

private String ip = "";

private String username = "";

private String password = "";

private String ftpDir = "";

private String localFileFullName = "";// 待上传的文件全名

private String ftpFileName = ""; // 文件上传到FTP后的名称

FtpClient ftpClient = null;

OutputStream os = null;

FileInputStream is = null;

public Ftp(String serverIP, String username, String password, String ftpDir) {

this.ip = serverIP;
this.username = username;
this.password = password;

if (ftpDir == null) {
this.ftpDir = "/ftpfileload";
} else {
try {
this.ftpDir = "/"
+ new String(ftpDir.getBytes("ISO-8859-1"), "GBK")
.toString();
} catch (UnsupportedEncodingException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}

}

private void createDir(String dir, FtpClient ftpClient) {
System.out.println(this.ftpDir);
ftpClient.sendServer("MKD " + dir + "\r\n");

try {
ftpClient.readServerResponse();
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}

}

private Boolean isDirExist(String dir, FtpClient ftpClient) {
try {
ftpClient.cd(dir);
} catch (Exception e) {
// TODO 自动生成 catch 块
return false;
}
return true;
}

public String upload(String localFileFullName) {

// this.ftpFileName = "aaa.test";

// 获取文件后缀名
String ext = localFileFullName.substring(localFileFullName
.lastIndexOf("."));
// System.out.println(ext);

// 产生新文件名,用系统当前时间+文件原有后缀名
long newFileName = System.currentTimeMillis();
String newFileFullName = newFileName + ext;
// System.out.println("new file name:"+newFileFullName);
this.ftpFileName = newFileFullName;

try {
String savefilename = new String(localFileFullName
.getBytes("ISO-8859-1"), "GBK");
// 新建一个FTP客户端连接
ftpClient = new FtpClient();
ftpClient.openServer(this.ip);
ftpClient.login(this.username, this.password);

// 判断并创建目录
if (!isDirExist(this.ftpDir, ftpClient)) {
createDir(this.ftpDir, ftpClient);
}

ftpClient.cd(this.ftpDir);// 切换到FTP目录
ftpClient.binary();

os = ftpClient.put(this.ftpFileName);
// 打开本地待长传的文件
File file_in = new File(savefilename);
is = new FileInputStream(file_in);
byte[] bytes = new byte[1024];

// 开始复制
int c;
// 暂未考虑中途终止的情况
while ((c = is.read(bytes)) != -1) {
os.write(bytes, 0, c);
}
} catch (Exception e) {
e.printStackTrace();
System.err.println("Exception e in Ftp upload(): " + e.toString());
} finally {
try {
if (is != null) {
is.close();
}
if (os != null) {
os.close();
}
if (ftpClient != null) {
ftpClient.closeServer();
}
} catch (Exception e) {
System.err.println("Exception e in Ftp upload() finally"
+ e.toString());
}
}
return this.ftpFileName;
}

public void delFile(String dir, String filename) {
ftpClient = new FtpClient();
try {
ftpClient.openServer(this.ip);
ftpClient.login(this.username, this.password);
if (dir.length() > 0) {
ftpClient.cd(dir);
}
ftpClient.sendServer("DELE " + filename + "\r\n");
ftpClient.readServerResponse();

} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}

}

我写的一个FTP类,你看看你能不能用。。。。。

⑶ 求用java写一个ftp服务器客户端程序。

import java.io.*;
import java.net.*;public class ftpServer extends Thread{ public static void main(String args[]){
String initDir;
initDir = "D:/Ftp";
ServerSocket server;
Socket socket;
String s;
String user;
String password;
user = "root";
password = "123456";
try{
System.out.println("MYFTP服务器启动....");
System.out.println("正在等待连接....");
//监听21号端口
server = new ServerSocket(21);
socket = server.accept();
System.out.println("连接成功");
System.out.println("**********************************");
System.out.println("");

InputStream in =socket.getInputStream();
OutputStream out = socket.getOutputStream();

DataInputStream din = new DataInputStream(in);
DataOutputStream dout=new DataOutputStream(out);
System.out.println("请等待验证客户信息....");

while(true){
s = din.readUTF();
if(s.trim().equals("LOGIN "+user)){
s = "请输入密码:";
dout.writeUTF(s);
s = din.readUTF();
if(s.trim().equals(password)){
s = "连接成功。";
dout.writeUTF(s);
break;
}
else{s ="密码错误,请重新输入用户名:";<br> dout.writeUTF(s);<br> <br> }
}
else{
s = "您输入的命令不正确或此用户不存在,请重新输入:";
dout.writeUTF(s);
}
}
System.out.println("验证客户信息完毕...."); while(true){
System.out.println("");
System.out.println("");
s = din.readUTF();
if(s.trim().equals("DIR")){
String output = "";
File file = new File(initDir);
String[] dirStructure = new String[10];
dirStructure= file.list();
for(int i=0;i<dirStructure.length;i++){
output +=dirStructure[i]+"\n";
}
s=output;
dout.writeUTF(s);
}
else if(s.startsWith("GET")){
s = s.substring(3);
s = s.trim();
File file = new File(initDir);
String[] dirStructure = new String[10];
dirStructure= file.list();
String e= s;
int i=0;
s ="不存在";
while(true){
if(e.equals(dirStructure[i])){
s="存在";
dout.writeUTF(s);
RandomAccessFile outFile = new RandomAccessFile(initDir+"/"+e,"r");
byte byteBuffer[]= new byte[1024];
int amount;
while((amount = outFile.read(byteBuffer)) != -1){
dout.write(byteBuffer, 0, amount);break;
}break;

}
else if(i<dirStructure.length-1){
i++;
}
else{
dout.writeUTF(s);
break;
}
}
}
else if(s.startsWith("PUT")){
s = s.substring(3);
s = s.trim();
RandomAccessFile inFile = new RandomAccessFile(initDir+"/"+s,"rw");
byte byteBuffer[] = new byte[1024];
int amount;
while((amount =din.read(byteBuffer) )!= -1){
inFile.write(byteBuffer, 0, amount);break;
}
}
else if(s.trim().equals("BYE"))break;
else{
s = "您输入的命令不正确或此用户不存在,请重新输入:";
dout.writeUTF(s);
}
}

din.close();
dout.close();
in.close();
out.close();
socket.close();
}
catch(Exception e){
System.out.println("MYFTP关闭!"+e);

}
}}

⑷ 关于JAVA FTP 客户端程序异常

501 PORT not allowed after EPSV ALL~~~说明还是网络的问题,我对网络不熟~~~比如用ftp工具的时候,有些ftp站点必须采用被动模式才能连接。下面的你看看吧,或许有用,找网管看看应该使用哪种方式访问

***************************************************
3. EPSV命令
EPSV请求服务器在一个数据端口上侦听等待连接,它可以带参数。对它的响应是TCP端口号。响应的格式与EPRT参数的很象。这对实现上有很大的方便。而且响应还留下了网络协议和/或网络地址的空位,可以供以后使用。使用扩展地址进行被动模式的响应码必须是229,对它的解释如下:
2yz 主动完成
x2z 连接
xy9 进行扩展的被动模式

响应的格式如下:
<指示服务器已经进入初扩展的被动模式>
(<d><d><d><TCP端口><d>)
包括在括号内的字符串必须是EPRT打开数据连接的端口。具体如上所未,这里就不多说了。数据连接使用的协议必须和控制连接使用的协议和地址一致,下面是响应的一个例子:
Entering Extended Passive Mode (|||6446|)
标准错误代码500和501对EPSV已经足够了。在EPSV命令没有使用参数时,服务器会基于控制连接所使用的协议选择数据连接使用的网络协议。但是在有代理的情况下,这种机制可能不合适。因此客户也需要能够要求一个指定协议。如果服务器返回说明它在指定端口不支持此协议,客户必须发送ABOR(放弃)命令使服务器关闭连接,然后客户再使用EPSV命令要求使用特定的网络协议,具体格式如下:
EPSV<空格><网络协议>
如果请求的网络协议是服务器支持的,那就必须使用此协议;如果不支持,则返回522。最后,EPSV命令可以使用参数"ALL"通过网络地址翻译器,EPRT命令不再使用。下面是例子:

EPSV<空格>ALL
接收到此命令后,服务器要拒绝除了EPSV以外所有建立连接的命令。在下一节我们将详细说明此命令的功能。
**********************************************************************

⑸ 用java实现sftp的客户端,channel.connect()的时候,抛出异常,收到信息过长,然后就没连上。怎么回事

首先,不太明确你要问什么?
J2EE是java企业级应用,它里面关于安全方面的东西很多!
权限管理?信息安全?授权服务?访问控制?数据机密性?
要是你想全部都了解!
在这里提问是不行的!太庞杂了!
如果 你要问的是 java ftp传输过程中的一些安全注意事项!
这里建议 你使用 apache的开源项目。他们把基于java的ftp操作都封装好了!
安全相关都有保证。
我下面可以给你复制一段 ftp下载远程终端的java代码!你可以参考一下!

需要下载 org.apache.commons.net包
可以到 网站下载!
/**
* Description: 从FTP服务器下载文件
* @param ip FTP服务器的ip地址
* @param port FTP服务器端口,默认为:21
* @param username FTP登录账号
* @param password FTP登录密码
* @param remotePath FTP服务器上的相对路径
* @param fileName 要下载的文件名
* @param localPath 下载后保存到本地的路径
* @return
*/
public static boolean downFile(String ip, int port,String username, String password, String remotePath,String fileName,String localPath,String localfile) {
boolean success = false;
FTPClient ftp = new FTPClient();
try {
int reply;
ftp.connect(ip, port);
//处理中文转码操作
ftp.setControlEncoding("GBK");

FTPClientConfig conf = new FTPClientConfig(FTPClientConfig.SYST_NT);
conf.setServerLanguageCode("zh");

ftp.login(username, password);//登录

reply = ftp.getReplyCode();
if (!FTPReply.isPositiveCompletion(reply)) {
ftp.disconnect();
return success;
}

ftp.changeWorkingDirectory(remotePath);//转移到FTP服务器目录
FTPFile[] fs = ftp.listFiles();
for(int i = 0; i < fs.length; i++){
FTPFile ff = fs[i];
if(ff.getName().equals(fileName)){
String localfilename = localfile;//按规则设置文件名这里你要下载文件,可以自己定义下载之后的文件名
File localFile = new File(localPath+File.separator+localfilename);

OutputStream is = new FileOutputStream(localFile);
//此处retrieveFile的第一个参数由GBK转为ISO-8859-1编码。否则下载后的文件内容为空。
ftp.retrieveFile(new String(ff.getName().getBytes("GBK"),"ISO-8859-1"), is);

is.close();
}
}
ftp.logout();
success = true;
} catch (IOException e) {
e.printStackTrace();
} finally {
if (ftp.isConnected()) {
try {
ftp.disconnect();
} catch (IOException ioe) {
}
}
}
return success;
}

⑹ Java 编写FTP客户端登录的问题。

try {
FtpClient ftp = new FtpClient("192.168.1.101");
ftp.login("Administrator", "123");
System.out.println("-------");
} catch (IOException e) {
System.out.println("************");
e.printStackTrace();
}
我这个也是用的sun的FTPClient
我测试的时候好使啊!
你的ftp配置是不是有问题啊!
lz再好好看看吧!
你也可以试试Apache的FTPClient

⑺ java ftp 和 sftp的区别

  • 文件传送协议FTP(File Transfer Protocol)是TCP/IP协议簇中的一个成员,也是现在因特网上最广泛的文件传送协议。FTP协议包括两个部分,一个是FTP客户端,另一个是FTP服务器。当然,FTP服务器是用来存储文件资源的,FTP客户端通过访问FTP服务器来获得资源的。

  • 安全文件传送协议SFTP(Secure File Transfer Protocol)可以为文件传送提供安全的加密/解密技术。基本语法和FTP差不多。SFTP是SSH的一部分,在SSH软件包中,已经包含了一个SFTP(Secure File Transfer Protocol)的安全文件传输子系统,SFTP本身没有单独的守护进程,它必须使用sshd守护进程(端口号默认是22)来完成相应的连接操作。由于这种传输方式使用了加密/解密技术,文件传送相对来说是很安全的,但是是有代价的,它的传输效率比FTP要低得多。

引自:网页链接

⑻ java中怎么实现ftp服务器

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

⑼ java开发ftp客户端对服务器文件状态如何识别

我们当时是这样处理的:

上传一个文件,比如名字为:aaa.rar
当文件上传完的时候就会上传一个aaa.fin的空文件做标识,表明这个文件上传成功。

下载的时候首先检查aaa.fin是否存在。然后下载!

建议你在文件目录下放一个list.xml(存放当前上传成功的文件名,下载前在里面检索它是否存在)

⑽ 怎样用java开发ftp客户端

package zn.ccfccb.util;
import hkrt.b2b.view.util.Log;
import hkrt.b2b.view.util.ViewUtil;
import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;

import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPFile;
import zn.ccfccb.util.CCFCCBUtil;
import zn.ccfccb.util.ZipUtilAll;

public class CCFCCBFTP {

/**
* 上传文件
*
* @param fileName
* @param plainFilePath 明文文件路径路径
* @param filepath
* @return
* @throws Exception
*/
public static String fileUploadByFtp(String plainFilePath, String fileName, String filepath) throws Exception {
FileInputStream fis = null;
ByteArrayOutputStream bos = null;
FTPClient ftpClient = new FTPClient();
String bl = "false";
try {
fis = new FileInputStream(plainFilePath);
bos = new ByteArrayOutputStream(fis.available());
byte[] buffer = new byte[1024];
int count = 0;
while ((count = fis.read(buffer)) != -1) {
bos.write(buffer, 0, count);
}
bos.flush();
Log.info("加密上传文件开始");
Log.info("连接远程上传服务器"+CCFCCBUtil.CCFCCBHOSTNAME+":"+22);
ftpClient.connect(CCFCCBUtil.CCFCCBHOSTNAME, 22);
ftpClient.login(CCFCCBUtil.CCFCCBLOGINNAME, CCFCCBUtil.CCFCCBLOGINPASSWORD);
// Log.info("连接远程上传服务器"+"192.168.54.106:"+2021);
// ftpClient.connect("192.168.54.106", 2021);
// ftpClient.login("hkrt-CCFCCBHK", "3OLJheziiKnkVcu7Sigz");
FTPFile[] fs;
fs = ftpClient.listFiles();
for (FTPFile ff : fs) {
if (ff.getName().equals(filepath)) {
bl="true";
ftpClient.changeWorkingDirectory("/"+filepath+"");
}
}
Log.info("检查文件路径是否存在:/"+filepath);
if("false".equals(bl)){
ViewUtil.dataSEErrorPerformedCommon( "查询文件路径不存在:"+"/"+filepath);
return bl;
}
ftpClient.setBufferSize(1024);
ftpClient.setControlEncoding("GBK");
// 设置文件类型(二进制)
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
ftpClient.storeFile(fileName, fis);
Log.info("上传文件成功:"+fileName+"。文件保存路径:"+"/"+filepath+"/");
return bl;
} catch (Exception e) {
throw e;
} finally {
if (fis != null) {
try {
fis.close();
} catch (Exception e) {
Log.info(e.getLocalizedMessage(), e);
}
}
if (bos != null) {
try {
bos.close();
} catch (Exception e) {
Log.info(e.getLocalizedMessage(), e);
}
}
}

}

/**
*下载并解压文件
*
* @param localFilePath
* @param fileName
* @param routeFilepath
* @return
* @throws Exception
*/
public static String fileDownloadByFtp(String localFilePath, String fileName,String routeFilepath) throws Exception {
FileInputStream fis = null;
ByteArrayOutputStream bos = null;
FileOutputStream fos = null;
FTPClient ftpClient = new FTPClient();
String SFP = System.getProperty("file.separator");
String bl = "false";
try {
Log.info("下载并解密文件开始");
Log.info("连接远程下载服务器"+CCFCCBUtil.CCFCCBHOSTNAME+":"+22);
ftpClient.connect(CCFCCBUtil.CCFCCBHOSTNAME, 22);
ftpClient.login(CCFCCBUtil.CCFCCBLOGINNAME, CCFCCBUtil.CCFCCBLOGINPASSWORD);
// ftpClient.connect(CMBCUtil.CMBCHOSTNAME, 2021);
// ftpClient.login(CMBCUtil.CMBCLOGINNAME, CMBCUtil.CMBCLOGINPASSWORD);
FTPFile[] fs;

ftpClient.makeDirectory(routeFilepath);
ftpClient.changeWorkingDirectory(routeFilepath);
bl = "false";
fs = ftpClient.listFiles();
for (FTPFile ff : fs) {
if (ff.getName().equals(fileName)) {
bl = "true";
Log.info("下载文件开始。");
ftpClient.setBufferSize(1024);
// 设置文件类型(二进制)
ftpClient.setFileType(FTPClient.BINARY_FILE_TYPE);
InputStream is = ftpClient.retrieveFileStream(fileName);
bos = new ByteArrayOutputStream(is.available());
byte[] buffer = new byte[1024];
int count = 0;
while ((count = is.read(buffer)) != -1) {
bos.write(buffer, 0, count);
}
bos.flush();
fos = new FileOutputStream(localFilePath+SFP+fileName);
fos.write(bos.toByteArray());
Log.info("下载文件结束:"+localFilePath);
}
}
Log.info("检查文件是否存:"+fileName+" "+bl);
if("false".equals(bl)){
ViewUtil.dataSEErrorPerformedCommon("查询无结果,请稍后再查询。");
return bl;
}
return bl;
} catch (Exception e) {
throw e;
} finally {
if (fis != null) {
try {
fis.close();
} catch (Exception e) {
Log.info(e.getLocalizedMessage(), e);
}
}
if (bos != null) {
try {
bos.close();
} catch (Exception e) {
Log.info(e.getLocalizedMessage(), e);
}
}
if (fos != null) {
try {
fos.close();
} catch (Exception e) {
Log.info(e.getLocalizedMessage(), e);
}
}
}
}

// 调用样例:
public static void main(String[] args) {
try {
// 密钥/res/20150228
// ZipUtilAll.unZip(new File(("D:/123/123.zip")), "D:/123/");
// ZipDemo1232.unZip(new File(("D:/123/123.zip")), "D:/123/");
// 明文文件路径
String plainFilePath = "D:/req_20150204_0011.txt";
// 密文文件路径
String secretFilePath = "req_20150204_00134.txt";
// 加密
// encodeAESFile(key, plainFilePath, secretFilePath);
fileDownloadByFtp("D://123.zip","123.zip","req/20150228");
ZipUtilAll.unZip("D://123.zip", "D:/123/李筱/");
// 解密
plainFilePath = "D:/123.sql";
// secretFilePath = "D:/test11111.sql";
// decodeAESFile(key, plainFilePath, secretFilePath);
} catch (Exception e) {
e.printStackTrace();
}
}

}

热点内容
java返回this 发布:2025-10-20 08:28:16 浏览:712
制作脚本网站 发布:2025-10-20 08:17:34 浏览:974
python中的init方法 发布:2025-10-20 08:17:33 浏览:686
图案密码什么意思 发布:2025-10-20 08:16:56 浏览:837
怎么清理微信视频缓存 发布:2025-10-20 08:12:37 浏览:743
c语言编译器怎么看执行过程 发布:2025-10-20 08:00:32 浏览:1085
邮箱如何填写发信服务器 发布:2025-10-20 07:45:27 浏览:314
shell脚本入门案例 发布:2025-10-20 07:44:45 浏览:194
怎么上传照片浏览上传 发布:2025-10-20 07:44:03 浏览:882
python股票数据获取 发布:2025-10-20 07:39:44 浏览:840