java实现服务器和客户端之间的文件传输

实现思路

一、客户端发文件:首先建立和服务器的连接,然后我们通过IO流来实现数据的传输,步骤:
1、通过服务器的IP地址和端口号实现和服务器的连接(这里不要忘记先开服务器哦)
2、获取本地的文件的地址,建立java和文件的连接。
3、获取文件输入流和数据输出流(注意:这里的输入和输出都是指对于java,文件输入流的对象名可以为fis,数据输出流的对象名可以为dos;反之,文件输出流fos,数据输入流dis,即和前者相反的过程)
4、写文件。
二、服务器收文件:
1、接收客户端建立连接的申请,生成一个socket对象。
2、赋予一个服务器端的文件地址(之后在写数据的时候如果文件不存在,则自动创建文件并写数据,若存在文件,则会覆盖原文件,如果要不覆盖可以这样改:fos = new FileOutputStream(file,true);)
3、获取数据输入流和文件输出流。
4、读文件。

代码实现

我在代码中实现了先发一段字符串再发一个完整的文件,文件前的字符串作为文件附带信息,作为文件的信息补充。在聊天室的编写中,可以用来补充文件的发送者和接受者信息。
1、客户端:

public class Client {    
	 public static void main(String[] args){
	int length =0;
	FileInputStream fis = null;
        DataOutputStream dos = null;
        Socket socket = null; 
        OutputStream out =null;
        PrintWriter pw = null;
        byte[] sendByte = null;
		 try {
			socket = new Socket("localhost",7777);
			out = socket.getOutputStream();
			pw = new PrintWriter(out);
			System.out.println("连接到服务器成功");
			File file = new File("E:\\workspace\\mayifan\\src\\com\\myf\\clientTest1207\\tree.zip");
			fis = new FileInputStream(file);
			dos = new DataOutputStream(socket.getOutputStream());
			sendByte = new byte[1024];			
			pw.write("123"+"\r\n");
			pw.flush();
			pw.write("456"+"\r\n");
			pw.flush();			
			System.out.println("准备发送");
			while((length=fis.read(sendByte))>0){				
				dos.write(sendByte, 0 , length);
				dos.flush();
			}
			System.out.println("文件发送完毕");						
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			try {
				fis.close();
				dos.close();
			        socket.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}			
		}		 		 
	 }		
}

2、服务器:

public class Server {
	 public static void main(String[] args){
		 DataInputStream dis=null;
		 Socket socket =null;
		 FileOutputStream fos =null;
		 InputStreamReader ir = null;
		 BufferedReader br = null;
		 try{
		    int length=0;
		    byte[] getByte = new byte[1024];		
		    ServerSocket ss = new ServerSocket(7777);
		    System.out.println("服务器创建完毕");
		    socket = ss.accept();
		    ir=new InputStreamReader(socket.getInputStream());
		    br=new BufferedReader(ir);			
		    System.out.println("连接到客户端");
		    dis = new DataInputStream(socket.getInputStream());
		    File file = new File("E:\\workspace\\mayifan\\src\\com\\myf\\serverTest1207\\tree1.zip");
		    fos = new FileOutputStream(file);		
	            String first = br.readLine();
	            String second = br.readLine();        
	            System.out.println(first);
	            System.out.println(second);						
                    System.out.println("准备接收文件");
			while((length=dis.read(getByte))>0){				
				fos.write(getByte, 0, length);
				fos.flush();
			}
	            System.out.println("文件接收完毕");	       	        	        
		 }catch(IOException e){
			 e.getStackTrace();
		 }finally{
			 try {
				dis.close();
				fos.close();
			        socket.close();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}			
		 }		 
	 }	 	 
}

大家在使用时,注意修改文件的源地址和目标地址。

猜你喜欢

转载自blog.csdn.net/mayifan_blog/article/details/84896877