在好例子网,分享、交流、成长!
您当前所在位置:首页Java 开发实例常规Java编程 → 基于java socket的简单FTP功能实现

基于java socket的简单FTP功能实现

常规Java编程

下载此实例
  • 开发语言:Java
  • 实例大小:0.01M
  • 下载次数:13
  • 浏览次数:331
  • 发布时间:2019-04-19
  • 实例类别:常规Java编程
  • 发 布 人:crazycode
  • 文件格式:.rar
  • 所需积分:2
 相关标签: FTP Socket java

实例介绍

【实例简介】实现FTP的一些功能,如上传文件,下载文件,显示目录,改变目录,退出等功能
【实例截图】

【核心代码】

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.*;
import java.util.ArrayList;
import java.io.File; 

public class server {
	ServerSocket ss = null;
    String dir = "";//显示目录
	String cmd = "";
	DataOutputStream dos;
	String direcFile = "";
	File rootDirectory;
	String shareFile;
	DataInputStream dis ;
	//ArrayList <File> fileArrayList = new ArrayList <File>();
	ArrayList<File> fileArrayList = new ArrayList<File>();
	private String shareFiledirectory;
	public static void main(String[] args) {
		//System.out.println("等待连接");
		new server("D:\\Backup\\我的文档\\网络课程设计共享文件");
		

	}
	public server(String shareFiledirectory){
		this.shareFiledirectory = shareFiledirectory; 
		//建立套接字
		try {
			ss  = new ServerSocket(8888);//建立提供服务器的端口号为8888
		} catch (IOException e1) {
			e1.printStackTrace();
		}
		
		try{
		
		while(true){
			
			boolean flag = true;
			
			while(flag){
				Socket s = ss.accept();//阻塞式,等待客户端请求连接
				System.out.println("已有客户端连接");
			//接受对方的要求:
				 dis = new DataInputStream(new BufferedInputStream(s
						.getInputStream()));//BufferedInputStream 利用缓冲区来提高读效率,
		//             从此套接字读取字节的输入流
				 
				 //BufferedInputStream(InputStream in) 参数in指定需要被装饰的输入流
				   cmd= dis.readUTF();//从dis输入流读取若干字节,把它转换为采用UTF-8字符编码的字符串,并将其放在cmd String变量里
				   //UTF-8对ASCII字符采用一个字节形式的编码,对非ASCII字符则采用两个或两个以上字节形式的编码
				   dis.close();//关闭输入流
				   s.close();//关闭socket
				   
				   System.out.println("输出读入的字符串:" cmd);
				//   System.out.println(cmd=="get");
			
			//接受后放在cmd里用于判断:
			if(cmd.equals("get"))
				get();
			else if(cmd.equals("put"))
				put();
			else if(cmd.equals("cd"))
				cd ();
			else if(cmd.equals("dir"))
				dir();
			else if(cmd == "quit")
				flag = false;
			
			
			}
			
			
	}
		}catch (IOException e){
			
		}
	}
		
		public void get(){
			System.out.println("get" 1111);
			
			Socket s = null;//连接为空
			try{
				s = ss.accept();
				//接受对方的要求的文件名:
			dis = new DataInputStream(new BufferedInputStream(s
					.getInputStream()));//
			   String filePath = dis.readUTF();//
			   System.out.println(filePath);
			   //传输文件:
			   
			   dos = new DataOutputStream(new BufferedOutputStream(s.getOutputStream()));
	    		
	    		File file = new File(filePath);
	    		dos.writeUTF(file.getName());
	    		dos.flush();
	    		dos.writeLong(file.length());
	    		dos.flush();
	    		
	    		dis = new DataInputStream(new BufferedInputStream(new FileInputStream(filePath)));
	    		
	    		int BUFSIZE = 8192;
	    		byte [] buf = new byte[BUFSIZE];
	    		
	    		while(true){
	    			int read = 0;
	    			if(dis != null){
	    				read = dis.read(buf);
	    			}else{
	    				System.out.println("no file founded!");
	    				break;
	    			}
	    			if (read == -1){
	    				break;
	    			}
	    			dos.write(buf, 0, read);
	    		}
	    		dos.flush();
			   
			   
			   
			}catch(IOException e){
				System.out.println("asdfsssssssssss");
			}finally{
				try{
					dos.close();
				dis.close();
				s.close();
				}catch(IOException e){
					
				}
			}
			
		}
		public void put(){
			System.out.println("put");
			Socket s = null;
			try{
				s = ss.accept();
				//下载文件
		        dis = new DataInputStream(new BufferedInputStream(s.getInputStream()));//从客户端接收存放上传文件路径的输出流
		    
		    	int bufferSize = 8192;   
		        // 缓冲区   
		        byte[] buf = new byte[bufferSize];   
		        int passedlen = 0;   
		        
		        long len = 0;   
		        String savePath = "E:/上传";
		        // 获取文件名称             保存上传 文件的路径名
		        savePath =  savePath File.separator dis.readUTF();   
		        //在本地路径建一个数据流
		        DataOutputStream fileOut = new DataOutputStream(   
		                new BufferedOutputStream( new FileOutputStream(savePath)));   
		        // 获取文件长度   
		        len = dis.readLong(); //从输入流  中读取8个字节

		        System.out.println("文件的长度为:"   len   "  KB");   
		        System.out.println("开始接收文件!");   

		        // 获取文件   
		        while (true) {   
		            int read = 0;   
		            if (dis != null) {   
		                read = dis.read(buf); //从输入流将数据读到缓冲区中,并将返回结果赋给read  
		            }   
		            passedlen  = read;   
		            if (read == -1) {   
		                break;   
		            }   
		            System.out.println("文件接收了"   (passedlen * 100 / len)   "%");   
		            fileOut.write(buf, 0, read);   
		        }   
		        System.out.println("接收完成,文件存为"   savePath);   
		        fileOut.close();   
				dis.close();
				s.close();
			}catch(IOException e ){
				
			}
			
		}
		public void cd(){
			System.out.println("cd");
			Socket s = null;
			try{
				s = ss.accept();
				//读取要到达的改变的目录:
			dis = new DataInputStream(new BufferedInputStream(s
					.getInputStream()));
			   shareFile = dis.readUTF();
			   System.out.println(shareFiledirectory);
			   dir();
			}catch(IOException e){
				
			}finally{
				
			}
			
		}
		public void dir ( ) throws IOException{
			rootDirectory = new File(shareFiledirectory);//shareFiledirectory表示共享文件的路径
			fileArrayList.clear();
			//String[] myList;//定义一个字符串数组
			//myList = rootDirectory.listFiles();
			//for(int i = 0;i<myList.length;i  ){
			//	System.out.println(myList[i]);
			//}
			//catch(Exception e){
			//	e.printStackTrace();
			//}
			//}
			 /* File f=new File(shareFiledirectory);
			File[] list=f.listFiles();
			for(int i=0;i<list.length;i  )
			System.out.println(list[i].getAbsolutePath());//打印全路径*/
			
			initFileArrayList();
			for(int i =0;i<fileArrayList.size();i  ){
			System.out.println(fileArrayList.get(i).getAbsolutePath());
			direcFile = direcFile fileArrayList.get(i).getAbsolutePath() '\n';
			}
			try{
				Socket s = ss.accept();
			dos = new DataOutputStream(
					new BufferedOutputStream(s.getOutputStream()));
			byte []buf = direcFile.getBytes();
			dos.write(buf);
			dos.flush();
			dos.close();
			s.close();
			}catch(IOException e){
				
			}
		}
		
		public void initFileArrayList() { // 将目录下所有文件放在一个数组列表里面fileArrayList
			if (rootDirectory.isDirectory()) {
				// 遍历目录下面的文件和子目录
				File[] fileList = rootDirectory.listFiles();
				System.out.println(fileList.length);
				for (int i = 0; i < fileList.length; i  ) {
					// 如果是文件,添加到文件列表中
					if (fileList[i].isFile()) {
						
						
						fileArrayList.add(new File(fileList[i].getAbsolutePath()));
						System.out.println(fileList[i].getAbsolutePath());
						System.out.println("添加了"   fileList[i].getName());
					}
					// 否则递归遍历子目录
					else if (fileList[i].isDirectory()) {
						System.out.println("文件");
						fileList[i].mkdir();//
						rootDirectory = fileList[i];
						initFileArrayList();
					}

				}
			}

		}
}

标签: FTP Socket java

实例下载地址

基于java socket的简单FTP功能实现

不能下载?内容有错? 点击这里报错 + 投诉 + 提问

好例子网口号:伸出你的我的手 — 分享

网友评论

发表评论

(您的评论需要经过审核才能显示)

查看所有0条评论>>

小贴士

感谢您为本站写下的评论,您的评论对其它用户来说具有重要的参考价值,所以请认真填写。

  • 类似“顶”、“沙发”之类没有营养的文字,对勤劳贡献的楼主来说是令人沮丧的反馈信息。
  • 相信您也不想看到一排文字/表情墙,所以请不要反馈意义不大的重复字符,也请尽量不要纯表情的回复。
  • 提问之前请再仔细看一遍楼主的说明,或许是您遗漏了。
  • 请勿到处挖坑绊人、招贴广告。既占空间让人厌烦,又没人会搭理,于人于己都无利。

关于好例子网

本站旨在为广大IT学习爱好者提供一个非营利性互相学习交流分享平台。本站所有资源都可以被免费获取学习研究。本站资源来自网友分享,对搜索内容的合法性不具有预见性、识别性、控制性,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,平台无法对用户传输的作品、信息、内容的权属或合法性、安全性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论平台是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二与二十三条之规定,若资源存在侵权或相关问题请联系本站客服人员,点此联系我们。关于更多版权及免责申明参见 版权及免责申明

;
报警