使用TCP从客户端上传图片到服务器端
2015-10-27 19:40
525 查看
客户端:
package upload.tcp;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
/*
* 从客户端上传图片到服务器端
*/
public class TCPClient {
/**
* @param args
* @throws IOException
* @throws UnknownHostException
*/
public static void main(String[] args) throws UnknownHostException, IOException {
// TODO Auto-generated method stub
//1.创建客户端Socket对象
Socket s=new Socket("192.168.166.193",10008);
//2.读取要上传的图片。源:硬盘。
InputStream is=new FileInputStream("d://dog.jpg");
//3.将图片发送到服务器端。目的:Socket流
OutputStream out=s.getOutputStream();
byte [] bt=new byte[1024];
int len=0;
while((len=is.read(bt))!=-1)
{
out.write(bt,0,len);
}
s.shutdownOutput();
//4.接收服务器端返回的数据。源:Socket流
InputStream isIn=s.getInputStream();
byte [] btIn=new byte[1024];
int lenIn=isIn.read(btIn);
String text=new String(btIn,0,lenIn);
System.out.println(text);
//告诉服务器端,上传结束
//关闭资源
is.close();
s.close();
}
}
服务器端:
package upload.tcp;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class TCPServer {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
//1.创建服务器端的ServerSocket对象
ServerSocket ss=new ServerSocket(10008);
//2.获取Socket对象
Socket s=ss.accept();
String ip=s.getInetAddress().getHostAddress();
System.out.println(ip+"connected...");
//3.接收客户端传来的图片。源:Socket流
InputStream in=s.getInputStream();
//4.存放到服务器端的硬盘。目的:硬盘
File dir=new File("d:\\pic");
if(!dir.exists()) dir.mkdirs();
File file=new File(dir,ip+".jpg");
FileOutputStream os=new FileOutputStream(file);
byte [] bt=new byte[1024];
int len;
while((len=in.read(bt))!=-1)
{
os.write(bt, 0, len);
}
//5.告诉客户端上传成功。目的:Socket流
OutputStream out=s.getOutputStream();
out.write("上传成功".getBytes());
//关闭资源
in.close();
s.close();
ss.close();
}
}
package upload.tcp;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;
/*
* 从客户端上传图片到服务器端
*/
public class TCPClient {
/**
* @param args
* @throws IOException
* @throws UnknownHostException
*/
public static void main(String[] args) throws UnknownHostException, IOException {
// TODO Auto-generated method stub
//1.创建客户端Socket对象
Socket s=new Socket("192.168.166.193",10008);
//2.读取要上传的图片。源:硬盘。
InputStream is=new FileInputStream("d://dog.jpg");
//3.将图片发送到服务器端。目的:Socket流
OutputStream out=s.getOutputStream();
byte [] bt=new byte[1024];
int len=0;
while((len=is.read(bt))!=-1)
{
out.write(bt,0,len);
}
s.shutdownOutput();
//4.接收服务器端返回的数据。源:Socket流
InputStream isIn=s.getInputStream();
byte [] btIn=new byte[1024];
int lenIn=isIn.read(btIn);
String text=new String(btIn,0,lenIn);
System.out.println(text);
//告诉服务器端,上传结束
//关闭资源
is.close();
s.close();
}
}
服务器端:
package upload.tcp;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
public class TCPServer {
/**
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
// TODO Auto-generated method stub
//1.创建服务器端的ServerSocket对象
ServerSocket ss=new ServerSocket(10008);
//2.获取Socket对象
Socket s=ss.accept();
String ip=s.getInetAddress().getHostAddress();
System.out.println(ip+"connected...");
//3.接收客户端传来的图片。源:Socket流
InputStream in=s.getInputStream();
//4.存放到服务器端的硬盘。目的:硬盘
File dir=new File("d:\\pic");
if(!dir.exists()) dir.mkdirs();
File file=new File(dir,ip+".jpg");
FileOutputStream os=new FileOutputStream(file);
byte [] bt=new byte[1024];
int len;
while((len=in.read(bt))!=-1)
{
os.write(bt, 0, len);
}
//5.告诉客户端上传成功。目的:Socket流
OutputStream out=s.getOutputStream();
out.write("上传成功".getBytes());
//关闭资源
in.close();
s.close();
ss.close();
}
}
相关文章推荐
- netstat查看网络信息
- httpclient4.3设置代理请求
- IOS网络笔记--地图内容2(正反向编码)
- 【工具类】安卓开发 HttpPost和HttpGet请求
- HttpClient使用详解
- 通过URLHttpConnection方式来取得图片,并且显示在ImageView上
- Java IO: 网络
- 同行不同命:中美两国的“网络水军”有哪些差异
- openfire因为网络不稳定而造成消息丢失的解决方案
- TCP/IP
- IdHttpServer实现webservice
- HTTPS 101
- HTTP相关概念
- IOS网络笔记--地图内容1
- Java之网络爬虫WebCollector+selenium+phantomjs(三)
- http post/get 2种使用方式
- http post/get 2种使用方式
- 网络营销广告的类型(CPC、CPM、CPA、CPS, CTR)
- 卷积神经网络中卷积概念的理解
- iOS网络请求---1