package experiment1;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.Socket;
import java.util.Scanner;
public class FTPClientTest {
public static void main(String[] args) {
System.out.println("========客户端========");
System.out.println("FTP Client启动");
FTPClient fc = new FTPClient();
fc.tidyCommand();
}
}
class FTPClient{
private String DownlDir;
private String PutDir;
private Socket socket;
public FTPClient() {
}
//主函数,用于调用连接方法、接受控制台输入命令并选择接下来要执 行的方法
public void tidyCommand(){
connect();
String command = null;
DataOutputStream dos = null;
DataInputStream dis = null;
BufferedReader br = null;
try {
br = new BufferedReader(
new InputStreamReader(System.in));
dos = new DataOutputStream(
new BufferedOutputStream(this.socket.getOutputStream()));
dis = new DataInputStream(
new BufferedInputStream(this.socket.getInputStream()));
while(true){
System.out.print("ftp:\\>");
command = br.readLine();
if(command.startsWith("put ")){
sendfiles(command.substring(4),dos);
}else if(command.startsWith("get ")){
receivefiles(command.substring(4),dis,dos);
}else if(command.equals("exit")){
disconnect(dos, dis);
break;
}else{
System.out.println("无效指令,请重试!!!");
}
}
dis.close();
dos.close();
} catch (IOException e) {
e.printStackTrace();
}
}
public void connect(){
System.out.println("FTP Client连接中...");
try {
this.socket = new Socket("localhost",8888);
} catch (IOException e) {
System.out.println("FTP Client连接失败!!!");
e.printStackTrace();
}
System.out.println("FTP Client连接成功!!!");
setPutDir();
setDownlDir();
}
public void disconnect(DataOutputStream dos,DataInputStream dis){
if(this.socket != null){
try {
dos.writeUTF("disconnect");
dos.flush();
this.socket.close();
} catch (IOException e) {
System.out.println("网络连接异常,程序退出!!!");
e.printStackTrace();
}
}
System.out.println("程序退出!!!");
}
//请设置上传路径
public void setPutDir(){
System.out.println("请设置上传路径!!!(例如:D:\\lll)");
@SuppressWarnings("resource")
Scanner sc = new Scanner(System.in);
String PutDir = sc.next();
File file = new File(PutDir);
if(file.isDirectory()){
System.out.println("设置成功!!!");
}else {
System.out.println("设置失败!!!请重新输入");
setPutDir();
}
this.PutDir = PutDir;
}
//请设置下载路径
@SuppressWarnings("resource")
public void setDownlDir(){
System.out.println("请设置下载路径!!!(例如:D:\\lll)");
Scanner scanner = new Scanner(System.in);
String DownlDir = scanner.next();
File file = new File(DownlDir);
if(file.isDirectory()){
System.out.println("设置成功!!!");
}else {
System.out.println("设置失败!!!请重新输入");
setDownlDir();
}
this.DownlDir = DownlDir;
}
//接收文件方法,用于接收服务器端发来的文件
public void receivefiles(String filename,DataInputStream dis,DataOutputStream dos){
BufferedOutputStream bout = null;
File file = null;
try {
file = new File(this.DownlDir,filename);
dos.writeUTF("get " + filename);
dos.flush();
boolean exist = dis.readBoolean();
if(exist){
System.out.println("开始下载"+file.getName()+"文件....");
long size = dis.readLong();
bout = new BufferedOutputStream(new FileOutputStream(file));
for(int i=0;i<size;i++) {
bout.write(dis.read());
bout.flush();
}
bout.close();
}else {
System.out.println("文件不存在!!!下载文件失败");
return;
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("文件"+file.getName()+"下载成功!!!");
}
//发送文件方法,用于上传文件给服务器端
public void sendfiles(String filename,DataOutputStream dos){
File file = null;
BufferedInputStream bis = null;
try {
file = new File(this.PutDir,filename);
if(file.exists()){
System.out.println("开始上传"+file.getName()+"文件....");
long size = file.length();
dos.writeUTF("put " + file.getName());
dos.flush();
dos.writeLong(size);
dos.flush();
bis = new BufferedInputStream(new FileInputStream(file));
for(int i=0;i<size;i++) {
dos.write(bis.read());
dos.flush();
}
bis.close();
}else{
System.out.println("上传文件失败!!!文件不存在");
return;
}
} catch (IOException e) {
System.out.println("文件"+file.getName()+"上传失败!!!");
e.printStackTrace();
return;
}
System.out.println("文件"+file.getName()+"上传成功!!!");
}
}
程序员Ale-阿乐
- 粉丝: 1w+
- 资源: 99
最新资源
- 水果系统.rar
- 企业级新闻系统(SSH+MYSQL)130221.rar
- 图片浏览小程序(java+applet)130227.rar
- 通用的在线考试系统(jsp+struts+hibernate+oracle)130220.rar
- 微信文章爬虫项目全套技术资料100%好用.zip
- 单机无穷大系统发生各类(三相短路,单相接地,两相接地,两相相间短路)等短路故障,各类(单相断线,两相断线,三相断线)等断线故障,暂态稳定仿真分析
- 图书管理系统(struts+hibernate+spring).rar
- 图书管理系统(struts+hibernate+spring)130225.rar
- 图书管理系统(struts+hibernate+spring+ext).rar
- 网上订餐系统(struts+spring+hibernate).rar
- 网上订餐系统(struts+spring+hibernate)130221.rar
- 图书管理系统(struts+hibernate+spring+ext)130221.rar
- 网上商城系统(JSP+MYSQL)130220.rar
- 网上商城系统(JSP+MYSQL).rar
- 网上购物系统(JavaBean+Servlet+jsp)130223.rar
- 网上书城系统(Struts+Hibernate+Mysql)130222.rar
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈