package cn.itcast_09; |
import java.io.BufferedInputStream; |
import java.io.BufferedOutputStream; |
import java.io.FileInputStream; |
import java.io.IOException; |
import java.io.InputStream; |
import java.io.InputStreamReader; |
import java.net.Socket; |
import java.net.UnknownHostException; |
/* |
* TCP上传图片的客户端: |
* |
*/ |
public class UploadImageDemo { |
public static void main(String[] args) throws IOException { |
//创建套接字 |
Socket s = new Socket( "192.168.1.103" , 4444 ); |
//封装图片文件对象和通道流对象 |
BufferedInputStream is = new BufferedInputStream( new FileInputStream( "C:\\a.png" )); |
BufferedOutputStream os = new BufferedOutputStream(s.getOutputStream()); |
|
//读写数据 |
byte [] bys = new byte [ 1024 ]; |
int len; |
while ((len=is.read(bys))!=- 1 ){ |
os.write(bys, 0 , len); |
os.flush(); |
} |
|
s.shutdownOutput(); |
|
//读取反馈 |
InputStream bos = s.getInputStream(); |
byte [] by = new byte [ 1024 ]; |
int lenth = 0 ; |
lenth=bos.read(by); |
String ss = new String(by, 0 ,lenth); |
System.out.println(ss); |
|
//释放资源 |
is.close(); |
s.close(); |
|
|
|
|
|
|
|
} |
} |
by: 发表于:2017-05-25 13:48:44 顶(0) | 踩(0) 回复
??
回复评论