导航:首页 > IDC知识 > ftp服务器java

ftp服务器java

发布时间:2020-12-17 23:17:31

1、求用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);

}
}}

2、如何测试java开发ftp服务器

http的话就用httpclient。open后,可以返回一个InputStream。这个就是你要读到文件流。原理的话,参考你版用浏览器打开这权个链接显示的内容。这个返回的是一个HTML网页,需要你解析出里面的文字(一般来说取body中间的内容就行)其实对于这种文件一般用FTP来下载的。楼上写的那个不对,哈哈。需要的话自己最好去查一下,怎么用,我有代码,不过告诉你的话也不太好?URLurl=newURL("http://你的地址");URLConnectionconnection=url.openConnection();InputStreamis=connection.getInputStream();BufferedReaderbr=newBufferedReader(newInputStreamReader(is,"gb2312"));下面就是解析这个字符串来,自己来吧

3、如何搭建ftp服务器 java

可以用Socket ,归根结底还是对IO流进行处理, 根据协议处理不同的命令,比如客户端发送“获取服务端目录结构” 的命令,服务端接收后向客户端写出目录结构数据,客户端根据协议解析并显示

4、java怎么打开FTP服务器上的文件

可以用文件流的方式直接写到服务器,也可以先在本地生成再调用ftp接口上传到服务器

5、java中怎么实现ftp服务器

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

6、如何用Java实现FTP服务器

1.使用的FileZillaServer开源免费软件,安装过后建立的本地FTP服务器。2.使用的apache上下载专FTP工具包,引用到工程目录属中。3.IDE,Eclipse,JDK6上传和下载目录的实现原理:对每一个层级的目录进行判断,是为目录类型、还是文件类型。如果为目录类型,采用递归调用方法,检查到最底层的目录为止结束。如果为文件类型,则调用上传或者下载方法对文件进行上传或者下载操作。贴出代码:(其中有些没有代码,可以看看,还是很有用处的)!

7、请问有java上传文件到ftp服务器的demo吗,感激不尽

/**
* 依赖commons-net-3.4.jar, commons-io-2.4.jar
*/
public class FtpUtils {
/**
* 上传
* @param host FTP地址
* @param port 端口ftp默认22,sftp默认23
* @param user ftp用户名
* @param pwd ftp密码
* @param destPath FTP文件保存路径
* @param fileName ftp保存文件名称
* @param file 需要上传的文件
*/
public static void upload(String host, int port,String user, String pwd, String destPath, String fileName, File file){
FTPClient ftp = null;
InputStream fis = null;
try {
//1.建立连接
ftp = new FTPClient();
ftp.connect(host, port);
//2.验证连接地址
int reply = 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 = new FileInputStream(file);
ftp.storeFile(fileName, fis);
} catch (SocketException e) {
e.printStackTrace();
} catch (IOException e) {
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 (IOException e) {
e.printStackTrace();
}
}
}
}

8、JAVA怎么实现删除远程FTP服务器上的某一文件

一个JAVA 实现FTP功能的代码,包括了服务器的设置模块,并包括有上传文件至FTP的通用方法、下载文件的通用方法以及删除文件、在ftp服务器上传文件夹、检测文件夹是否存在等,里面的有些代码对编写JAVA文件上传或许有参考价值,

(1):Java FTP主文件代码:

package ftpDemo;

import java.io.DataOutputStream;

import java.io.InputStream;

import java.io.OutputStream;

import sun.net.TelnetInputStream;

import sun.net.TelnetOutputStream;

import sun.net.ftp.FtpClient;

public class ftpUtil {

// 上传文件至FTP通用方法

public static void upLoadFileFtp(KmConfig kmConfig,InputStream is, String fileName){

try {

String ftpHost = kmConfig.getFtpHost();

int port = kmConfig.getFtpPort();

String userName = kmConfig.getFtpUser();

String passWord = kmConfig.getFtpPassword();

String path = kmConfig.getFtpPath();

FtpClient ftpClient = new FtpClient(ftpHost, port);// ftpHost为FTP服务器的IP地址,port为FTP服务器的登陆端口,ftpHost为String型,port为int型。

ftpClient.login(userName, passWord);// userName、passWord分别为FTP服务器的登陆用户名和密码

ftpClient.binary();

ftpClient.cd(path);// path为FTP服务器上保存上传文件的路径。

TelnetOutputStream telnetOut = ftpClient.put(fileName);// fileName为上传的文件名

DataOutputStream dataOut = new DataOutputStream(telnetOut);

byte buffer[] = new byte[ * ];

int count = ;

while ((count = is.read(buffer)) != -) {

dataOut.write(buffer, , count);

}

telnetOut.close();

dataOut.close();

ftpClient.closeServer();

} catch (Exception e) {

System.out.println("上传文件失败!请检查系统FTP设置,并确认FTP服务启动");

}

}

// 删除文件至FTP通用方法

public static void deleteFileFtp(KmConfig kmConfig,String fileName){

try {

String ftpHost = kmConfig.getFtpHost();

int port = kmConfig.getFtpPort();

String userName = kmConfig.getFtpUser();

String passWord = kmConfig.getFtpPassword();

String path = kmConfig.getFtpPath();

FtpClient ftpClient = new FtpClient(ftpHost, port);// ftpHost为FTP服务器的IP地址,port为FTP服务器的登陆端口,ftpHost为String型,port为int型。

ftpClient.login(userName, passWord);// userName、passWord分别为FTP服务器的登陆用户名和密码

ftpClient.binary();

ftpClient.cd(path);// path为FTP服务器上保存上传文件的路径。

try {

ftpClient.sendServer("dele " + fileName + " ");

} catch (Exception e) {

System.out.println("删除文件失败!请检查系统FTP设置,并确认FTP服务启动");

}

ftpClient.closeServer();

} catch (Exception e) {

System.out.println("删除文件失败!");

}

}

// 下载ftp文件

public static void downloadFileFtp(KmConfig kmConfig,String fileName, String clientFileName, OutputStream outputStream){

try {

String ftpHost = kmConfig.getFtpHost();

int port = kmConfig.getFtpPort();

String userName = kmConfig.getFtpUser();

String passWord = kmConfig.getFtpPassword();

String path = kmConfig.getFtpPath();

FtpClient ftpClient = new FtpClient(ftpHost, port);// ftpHost为FTP服务器的IP地址,port为FTP服务器的登陆端口,ftpHost为String型,port为int型。

ftpClient.login(userName, passWord);// userName、passWord分别为FTP服务器的登陆用户名和密码

ftpClient.binary();

ftpClient.cd(path);// path为FTP服务器上保存上传文件的路径。

try {

TelnetInputStream in = ftpClient.get(fileName);

byte[] bytes = new byte[];

int cnt=;

while ((cnt=in.read(bytes,,bytes.length)) != -) {

outputStream.write(bytes, , cnt);

}

outputStream.close();

in.close();

} catch (Exception e) {

ftpClient.closeServer();

e.printStackTrace();

}

ftpClient.closeServer();

} catch (Exception e) {

System.out.println("下载文件失败!请检查系统FTP设置,并确认FTP服务启动");

}

}

//在ftp服务器上传件文件夹

public boolean createDir(String path,FtpClient ftpClient) throws Exception{

//进入到home文件夹下

ftpClient.cd("/home");

//创建远程文件夹

//远程命令包括

//USER  PORT  RETR  ALLO  DELE  SITE  XMKD  CDUP  FEAT<br>

// PASS  PASV  STOR  REST  CWD STAT  RMD XCUP  OPTS<br>

// ACCT  TYPE  APPE  RNFR  XCWD  HELP  XRMD  STOU  AUTH<br>

// REIN  STRU  SMNT  RNTO  LIST  NOOP  PWD SIZE  PBSZ<br>

// QUIT  MODE  SYST  ABOR  NLST  MKD XPWD  MDTM  PROT<br>

//  在服务器上执行命令,如果用sendServer来执行远程命令(不能执行本地FTP命令)的话,所有FTP命令都要加上/r/n<br>

//  ftpclient.sendServer("XMKD /test/bb/r/n"); //执行服务器上的FTP命令<br>

//  ftpclient.readServerResponse一定要在sendServer后调用<br>

//  nameList("/test")获取指目录下的文件列表<br>

//  XMKD建立目录,当目录存在的情况下再次创建目录时报错<br>

//  XRMD删除目录<br>

//  DELE删除文件<br>

//通过远程命令 穿件一个files文件夹

ftpClient.sendServer("MKD "+ path + " ");

//这个方法必须在 这两个方法中间调用 否则 命令不管用

ftpClient.binary();

ftpClient.readServerResponse();

return false;

}

/**

* 检查文件夹是否存在

* @param dir

* @param ftpClient

* @return

*/

public boolean isDirExist(String dir, FtpClient ftpClient) {

try {

ftpClient.cd(dir);

} catch (Exception e) {

return false;

}

return true;

}

}

(2):KmConfig.java代码如下:定义FTP服务器参数,包括登录的用户名密码之类的。

package ftpDemo;

public class KmConfig {

//主机ip

private String FtpHost = "";

//端口号

private int FtpPort;

//ftp用户名

private String FtpUser = "";

//ftp密码

private String FtpPassword = "";

//ftp中的目录

private String FtpPath = "";

public String getFtpHost() {

return FtpHost;

}

public void setFtpHost(String ftpHost) {

FtpHost = ftpHost;

}

public int getFtpPort() {

return FtpPort;

}

public void setFtpPort(int ftpPort) {

FtpPort = ftpPort;

}

public String getFtpUser() {

return FtpUser;

}

public void setFtpUser(String ftpUser) {

FtpUser = ftpUser;

}

public String getFtpPassword() {

return FtpPassword;

}

public void setFtpPassword(String ftpPassword) {

FtpPassword = ftpPassword;

}

public String getFtpPath() {

return FtpPath;

}

public void setFtpPath(String ftpPath) {

FtpPath = ftpPath;

}

}

(3):下面是测试代码:

9、如何用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){}
}

10、java如何实现将FTP文件转移到另一个FTP服务器上

你有FTPClient就比较好办,假如你的两台FTP服务器分别为fs1和fs2

在本地开发代码思路如下:

通过专FTPClient连接上fs1,然后下载属(可以循环批量下载)到本地服务器,保存到一个临时目录。

下载完成后,FTPClient断开与fs1的连接,记得必须logout。

本地服务器通过FileInputStream将刚下载到临时目录的文件读进来,得到一个List<File>集合。

通过FTPClient连接上fs2,循环List<File>集合,将文件上传至fs2的特定目录,然后清空临时目录,上传完毕后,断开fs2的连接,同样必须logout。

与ftp服务器java相关的知识