TCP之上传图片并给出反馈

SeverDome

package cn.itcast_05;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;

/*
 * 上传图片并给出反馈
 */
public class SeverDome {

	public static void main(String[] args) throws IOException {
		
		// 创建服务器对象
		ServerSocket ss = new ServerSocket(12345);
		
		//监听客户端连接
		Socket s = ss.accept();
		
		//封装通道数据
		BufferedInputStream bis = new BufferedInputStream(s.getInputStream());
		
		
		//封装图片文件
		BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("a.jpg"));
			
		byte[] bys = new byte[1024];
		int len = 0;
		while((len = bis.read(bys)) != -1) {
			bos.write(bys,0, len);
			bos.flush();
		}
		//给一个反馈
		OutputStream os = s.getOutputStream();
		os.write("图片上传成功".getBytes());
		
		//释放资源
		bos.close();
		s.close();
	}
}

ClientDome

package cn.itcast_05;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.Socket;

public class ClientDome {

	public static void main(String[] args) throws IOException {
		
		// 创建客户端对象
		Socket s = new Socket("10.212.31.21",12345);
		
		//封装图片
		BufferedInputStream bis = new BufferedInputStream(new FileInputStream("ling.jpg"));
		
		//封装发送到通道流
		BufferedOutputStream bos = new BufferedOutputStream(s.getOutputStream());
		
		byte[] bys = new byte[1024];
		int len = 0;
		while((len = bis.read(bys)) != -1) {
			bos.write(bys,0,len);
			bos.flush();
		}
		
		s.shutdownOutput();
		
		//接受反馈
		InputStream is = s.getInputStream();
		byte[] bys2 = new byte[1024];
		int len2= is.read(bys2);
		String Cilent = new String (bys2, 0, len2);
		System.out.println(Cilent);
		
		//释放资源
		bis.close();
		s.close();
	}
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Unknown To Known

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值