《黑马程序员》 TCP端图片复制

import java.io.*;
import java.net.*;

/*
  TCP端复制图片:
*/
class CFpicClient
{
	public static void main(String[] args) throws Exception{
		//使用字节输入流和字节输出流
	    Socket s=new Socket("172.11.0.34",10009);
		InputStream is=new FileInputStream(new File("c:\\bgpic.jpg"));
		//读取图片使用字节流
		byte[] buff=new byte[1024];
		OutputStream os=s.getOutputStream();
		int len=0;
		while((len=is.read(buff))!=-1){
			os.write(buff,0,len);
		}
		//插入结束标记
		s.shutdownOutput();
		BufferedReader bir=new BufferedReader(new InputStreamReader(s.getInputStream()));
		String line=bir.readLine();
		System.out.println("服务器返回信息:"+line);
		os.close();
		s.close();
	}
}
/**
    图片复制
*/class CFpicServer 
{
	public static void main(String[] args) throws Exception
	{
		ServerSocket ss=new ServerSocket(10009);
		Socket s=ss.accept();
		InputStream is=s.getInputStream();
		OutputStream os=new FileOutputStream(new File("e:\\123.jpg"));
		byte[] buff=new byte[1024];
		int len=0;
		while((len=is.read(buff))!=-1){
			os.write(buff,0,len);
		}
		//发送信息给客户端
		PrintWriter pw=new PrintWriter(s.getOutputStream(),true);
		pw.write("图片上传成功");
		pw.close();
		os.close();
		s.close();
	}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值