码迷,mamicode.com
首页 > 其他好文 > 详细

Tcp文件传输---转载

时间:2015-02-06 14:40:22      阅读:102      评论:0      收藏:0      [点我收藏+]

标签:

/**
客户端
1、服务端点
2、读取客户端已有的文件数据
3、通过socket输出流发给服务端
4、读取服务端反馈信息
5、关闭
**/
import java.io.*;
import java.net.*;
class  UploadClient
{
public static void main(String[] args) throws Exception
{
Socket s = new Socket("127.0.0.1",4434);
FileInputStream fis = new FileInputStream("D://2.png");
OutputStream out = s.getOutputStream();
byte[] buf = new byte[1024];
int len = 0;
while((len = fis.read(buf))!=-1)
{
out.write(buf,0,len);
}
//数据已经完成的时候执行shutdownOutput
s.shutdownOutput();
InputStream is = s.getInputStream();
byte[] bufin = new byte[1024];
int lenin = is.read(bufin);
System.out.println(new String(bufin,0,lenin));
fis.close();
s.close();
}
}
/**
服务器端
*/
class  UploadServer
{
public static void main(String[] args)throws Exception
{
ServerSocket ss = new ServerSocket(4434);
Socket s = ss.accept();
InputStream is = s.getInputStream();
FileOutputStream fos = new FileOutputStream("C://test.jpg");
byte[] buf = new byte[1024];
int len = 0;
while((len = is.read(buf))!=-1)
{
fos.write(buf,0,buf.length);
}
OutputStream out = s.getOutputStream();
out.write("上传成功".getBytes());
fos.close();
s.close();
ss.close();
}
}

Tcp文件传输---转载

标签:

原文地址:http://www.cnblogs.com/kevinfuture/p/4276914.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!