网络编程实现服务端和客户端之间传输文件

客户端(Client):
/**  
 * @Title: Client.java
 * @Description: 用来发送文件
 * @author LYL
 * @date 2021-03-02 16:25:37
 */  

package homework;

import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.InetAddress;
import java.net.Socket;

public class Client{	
	public static void main(String[] args) {
		Socket socket = null;
		OutputStream os = null;
		FileInputStream fis = null;
		ByteArrayOutputStream baos = null;
		try {
			//拿到Socket对象
			socket = new Socket(InetAddress.getByName("127.0.0.1"),9090);
			//获取输出流
			os = socket.getOutputStream();
			//拿到文件输入流,加载本地文件
			fis = new FileInputStream(new File("girl.png"));
			//创建一个字节数组
			byte[] buffer = new byte[1024];
			int len;
			//循环将文件数据写出
			while((len = fis.read(buffer)) != -1){
				os.write(buffer,0,len);
			}
			//文件输出完以后将输出流关闭
			socket.shutdownOutput();
			
			//拿到输入流用来接收服务器接收完成以后返回的数据
			InputStream is = socket.getInputStream();
			//生产一个字节输出流
			baos = new ByteArrayOutputStream();
			byte[] bufferr = new byte[20];
			int len1;
			//将接收到的数据循环写入到baos中
			while((len1 = is.read(buffer)) != -1){
				baos.write(buffer,0,len1);
			}
			//输出服务器返回的数据
			System.out.println(baos.toString());
		} catch (IOException e) {
			e.printStackTrace();
		}finally {		
			if(fis != null) {				
				try {
					fis.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(os != null) {				
				try {
					os.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(socket!=null) {				
				try {
					socket.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(baos != null) {				
				try {
					baos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		
	}
}


服务端(Server):
/**  
 * @Title: Server.java
 * @Description: 
 * @author LYL
 * @date 2021-03-02 16:25:58
 */  

package homework;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

public class Server {
	public static void main(String[] args) {		
		ServerSocket ss = null;
		Socket socket = null;
		InputStream is = null;
		FileOutputStream fos = null;
		OutputStream os = null;
		try {
			//拿到该端口的ServerSocket对象
			ss = new ServerSocket(9090);
			//拿到socket对象
			socket = ss.accept();
			//拿到用来接收文件的输入流
			is = socket.getInputStream();
			//拿到将服务器接收到的文件写出的流
			fos = new FileOutputStream(new File("girl2.png"));
			byte[] buffer = new byte[1024];
			int len;
			//将接收到的文件循环读入
			while((len = is.read(buffer)) != -1){
				//将文件循环写出
				fos.write(buffer,0,len);
			}
			
			System.out.println("图片传输完成");
			
			//拿到输出流
			os = socket.getOutputStream();
			os.write("你好,照片我已收到,非常漂亮!".getBytes());
		}catch (Exception e) {
			e.printStackTrace();
		}finally {
			if(fos!=null) {
				try {
					fos.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(is!=null) {				
				try {
					is.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(socket!=null) {				
				try {
					socket.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(ss!=null) {				
				try {
					ss.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
			if(os!=null) {				
				try {
					os.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		
	}
}

posted @ 2021-03-02 16:37  阿伦啊  阅读(290)  评论(0编辑  收藏  举报