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();
}
}
《黑马程序员》 TCP端图片复制
最新推荐文章于 2024-06-19 11:00:59 发布