1、用java做出一个FTP 服务器(!客户端)软件,具体都要干什么;
SOCKET网络编程,这个是基本的。
还有FTP协议要看,文件的读取与下载。
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服务器
1.使用的FileZillaServer开源免费软件,安装过后建立的本地FTP服务器。2.使用的apache上下载专FTP工具包,引用到工程目录属中。3.IDE,Eclipse,JDK6上传和下载目录的实现原理:对每一个层级的目录进行判断,是为目录类型、还是文件类型。如果为目录类型,采用递归调用方法,检查到最底层的目录为止结束。如果为文件类型,则调用上传或者下载方法对文件进行上传或者下载操作。贴出代码:(其中有些没有代码,可以看看,还是很有用处的)!
5、求用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);
}
}}
6、java中怎么实现ftp服务器
我知道apache有个commons net包,其中的copyFTPClient类可以实现客户端和服务之间的文件传输,但是我如果使用这种方式的话,就得将一台服务器上的文件传到我本地,再将这个文件传到另一台服务器上,感觉这中间多了一步操作;
7、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());
}
}
}
8、java做ftp服务器需要什么jar包
apache的包:commons-net.jar
主要使用到的类:
org.apache.commons.net.ftp.FTPClient;
org.apache.commons.net.ftp.FTPFile;
org.apache.commons.net.ftp.FTPReply;
JDK中也有自带的ftp包:
sun.net.ftp.下有FTP操作类
但sun已经不建议使版用了, 建议楼主使用apache的包权
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用户名和密码,其实就在ftp服务器的用户文件里面添加条记录。
方法有两种,我说专下思路。属
一、你可以用java程序找到相应的配置文件,打开、把用户名密码写入进去。ok了。
二、你用用java程序调用创建ftp用户的命令,来创建ftp用户。