标签:
package com.tz.util.screen;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.ServerSocket;
import java.net.Socket;
/**
* 图片上传
* 客服端
* 1.服务端点
* 2.读取客服端已有的图片数据
* 3.通过socket输出流将数据发给服务端
* 4.读取服务端反馈信息
* 5.关闭
* @author Administrator
*
*/
public class TcpPic {
public static void main(String[] args) throws Exception, Exception {
Socket s=new Socket("192.168.1.254",10007);
FileInputStream fis=new FileInputStream("1.bmp");
OutputStream out=s.getOutputStream();
byte[] buf=new byte[1024];
int len=0;
while ((len=fis.read(buf))!=-1) {
out.write(buf,0,len);
}
//告诉服务端数据写完
s.shutdownOutput();
InputStream in=s.getInputStream();
byte[] bufIn=new byte[1024];
int num=in.read(bufIn);
System.out.println(new String(bufIn,0,num));
fis.close();
s.close();
}
}
/**
* 服务端
*/
class PicServer{
public static void main(String[] args) throws Exception {
ServerSocket ss=new ServerSocket(10007);
Socket s=ss.accept();
InputStream in=s.getInputStream();
FileOutputStream fos=new FileOutputStream("server.bmp");
byte[] buf=new byte[1024];
int len=0;
while ((len=in.read(buf))!=-1) {
fos.write(buf,0,len);
}
OutputStream out=s.getOutputStream();
out.write("上传成功".getBytes());
fos.close();
s.close();
ss.close();
}
}
标签:
原文地址:http://my.oschina.net/u/2329247/blog/389347