请问用socket编程怎么socket传输图片片

博客访问: 2479910
博文数量: 419
博客积分: 9916
博客等级: 中将
技术积分: 7074
注册时间:
IT168企业级官微
微信号:IT168qiye
系统架构师大会
微信号:SACC2013
分类: Java
ClientTcpSend.java:import java.io.DataOutputStream;import java.io.File;import java.io.FileInputStream;import java.net.InetSocketAddress;import java.net.Socket;public class ClientTcpSend {&&&&public static void main(String[] args) {&&&&&&&&int length = 0;&&&&&&&&byte[] sendBytes = null;&&&&&&&&Socket socket = null;&&&&&&&&DataOutputStream dos = null;&&&&&&&&FileInputStream fis = null;&&&&&&&&try {&&&&&&&&&&&&try {&&&&&&&&&&&&&&&&socket = new Socket();&&&&&&&&&&&&&&&&socket.connect(new InetSocketAddress("127.0.0.1", 33456),&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&10 * 1000);&&&&&&&&&&&&&&&&dos = new DataOutputStream(socket.getOutputStream());&&&&&&&&&&&&&&&&File file = new File("/root/_c3a9c8b64c_b.jpg");&&&&&&&&&&&&&&&&fis = new FileInputStream(file);&&&&&&&&&&&&&&&&sendBytes = new byte[1024];&&&&&&&&&&&&&&&&while ((length = fis.read(sendBytes, 0, sendBytes.length)) > 0) {&&&&&&&&&&&&&&&&&&&&dos.write(sendBytes, 0, length);&&&&&&&&&&&&&&&&&&&&dos.flush();&&&&&&&&&&&&&&&&}&&&&&&&&&&&&} finally {&&&&&&&&&&&&&&&&if (dos != null)&&&&&&&&&&&&&&&&&&&&dos.close();&&&&&&&&&&&&&&&&if (fis != null)&&&&&&&&&&&&&&&&&&&&fis.close();&&&&&&&&&&&&&&&&if (socket != null)&&&&&&&&&&&&&&&&&&&&socket.close();&&&&&&&&&&&&}&&&&&&&&} catch (Exception e) {&&&&&&&&&&&&e.printStackTrace();&&&&&&&&}&&&&}}ServerTcpListener.java:import java.net.*;import java.io.*;public class ServerTcpListener implements Runnable {&&&&public static void main(String[] args) {&&&&&&&&try {&&&&&&&&&&&&final ServerSocket server = new ServerSocket(33456);&&&&&&&&&&&&Thread th = new Thread(new Runnable() {&&&&&&&&&&&&&&&&public void run() {&&&&&&&&&&&&&&&&&&&&while (true) {&&&&&&&&&&&&&&&&&&&&&&&&try {&&&&&&&&&&&&&&&&&&&&&&&&&&&&System.out.println("开始监听...");&&&&&&&&&&&&&&&&&&&&&&&&&&&&Socket socket = server.accept();&&&&&&&&&&&&&&&&&&&&&&&&&&&&System.out.println("有链接");&&&&&&&&&&&&&&&&&&&&&&&&&&&&receiveFile(socket);&&&&&&&&&&&&&&&&&&&&&&&&} catch (Exception e) {&&&&&&&&&&&&&&&&&&&&&&&&}&&&&&&&&&&&&&&&&&&&&}&&&&&&&&&&&&&&&&}&&&&&&&&&&&&});&&&&&&&&&&&&th.run(); //启动线程运行&&&&&&&&} catch (Exception e) {&&&&&&&&&&&&e.printStackTrace();&&&&&&&&}&&&&}&&&&public void run() {&&&&}&&&&public static void receiveFile(Socket socket) {&&&&&&&&byte[] inputByte = null;&&&&&&&&int length = 0;&&&&&&&&DataInputStream dis = null;&&&&&&&&FileOutputStream fos = null;&&&&&&&&try {&&&&&&&&&&&&try {&&&&&&&&&&&&&&&&dis = new DataInputStream(socket.getInputStream());&&&&&&&&&&&&&&&&fos = new FileOutputStream(new File("./cc.jpg"));&&&&&&&&&&&&&&&&inputByte = new byte[1024];&&&&&&&&&&&&&&&&System.out.println("开始接收数据...");&&&&&&&&&&&&&&&&while ((length = dis.read(inputByte, 0, inputByte.length)) > 0) {&&&&&&&&&&&&&&&&&&&&System.out.println(length);&&&&&&&&&&&&&&&&&&&&fos.write(inputByte, 0, length);&&&&&&&&&&&&&&&&&&&&fos.flush();&&&&&&&&&&&&&&&&}&&&&&&&&&&&&&&&&System.out.println("完成接收");&&&&&&&&&&&&} finally {&&&&&&&&&&&&&&&&if (fos != null)&&&&&&&&&&&&&&&&&&&&fos.close();&&&&&&&&&&&&&&&&if (dis != null)&&&&&&&&&&&&&&&&&&&&dis.close();&&&&&&&&&&&&&&&&if (socket != null)&&&&&&&&&&&&&&&&&&&&socket.close();&&&&&&&&&&&&}&&&&&&&&} catch (Exception e) {&&&&&&&&}&&&&}}
阅读(22624) | 评论(0) | 转发(2) |
相关热门文章
给主人留下些什么吧!~~
请登录后评论。5522人阅读
网络编程(1)
实现思路:
在客户端获取到文件流,将文件流写入到通过socket指定到某服务器的输出流中,在服务器中通过socket获取到输入流,将数据写入到指定的文件夹内,为了提供多用户同时上传,这里需要将在服务器上传客户端的文件操作放在另开启一个线程去运行。
import java.net.*;
import java.io.*;
服务端将获取到的客户端封装到单独的线程中。
JpgClient2
public static void main(String[] args) throws Exception
//检验文件
if(args.length==0)
System.out.println(&指定一个jpg文件先!&);
File file = new File(args[0]);
if(!(file.exists() && file.isFile() && file.getName().endsWith(&.jpg&)))
System.out.println(&选择文件错误,请重新选择一个正确的文件。&);
//读取文件并写入到服务器中
Socket s = new Socket(&192.168.137.199&,9006);
FileInputStream fis = new FileInputStream(file);
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);
String str = new String(bufIn,0,num);
System.out.println(str);
fis.close();
s.close();
class JpgThread implements Runnable
JpgThread(Socket s)
public void run()
int count = 1;
String ip = s.getInetAddress().getHostAddress();
//获取客户端数据
InputStream in = s.getInputStream();
//指定文件存放路径将读取到客户提交的数据写入文件中
File dir = new File(&c:\\pic&);
File file = new File(dir,ip+&(&+count+&).jpg&);
while(file.exists())
file = new File(dir,ip+&(&+(count++)+&).jpg&);
FileOutputStream fos = new FileOutputStream(file);
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();
catch (Exception e)
System.out.println(e.toString());
JpgServer2
public static void main(String[] args)throws Exception
ServerSocket ss = new ServerSocket(9006);
//开启线程并发访问
while(true)
Socket s = ss.accept();
String ip = s.getInetAddress().getHostAddress();
System.out.println(ip+&....connected&);
new Thread(new JpgThread(s)).start();
参考知识库
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
访问:611197次
积分:6148
积分:6148
排名:第2954名
原创:89篇
评论:293条
(1)(1)(2)(5)(4)(19)(21)(45)android客户端和java服务端之间用socket来传输图片
为研究android客户端和java服务端之间用socket来传输图片的问题,困扰我很久,网上的参考内容不是完整,就是不详细,所以鉴于自己的痛苦,测试成功后,我决定把代码共享,希望遇到这个问题的同胞,不再痛苦。
一、从服务端向客户端发送图片:
服务端的代码:
import&java.io.DataOutputS&&&&
import&java.io.FileInputS&&&&
import&java.io.IOE&&&&&&
import&java.net.ServerS&&&&
import&java.net.S&&&&
public&class&Server01&{&&&&
&&&&public&static&void&main(String[]&args)&{&&&&
&&&&&&&&try&{&&&&
&&&&&&&&&&&&ServerSocket&server&=&new&ServerSocket(30000);&&&&
&&&&&&&&&&&&Socket&socket&=&server.accept();&&&&
&&&&&&&&&&&&DataOutputStream&dos&=&new&DataOutputStream(socket.getOutputStream());&&&&
&&&&&&&&&&&&FileInputStream&fis&=&new&FileInputStream("C:/sunnyTest/picture/cat01.jpg");&&&&
&&&&&&&&&&&&int&size&=&fis.available();&&
&&&&&&&&&&&&&&
&&&&&&&&&&&&System.out.println("size&=&"+size);&&
&&&&&&&&&&&&byte[]&data&=&new&byte[size];&&&&
&&&&&&&&&&&&fis.read(data);&&&&
&&&&&&&&&&&&dos.writeInt(size);&&&&
&&&&&&&&&&&&dos.write(data);&&&&
&&&&&&&&&&&&&&
&&&&&&&&&&&&dos.flush();&&&&
&&&&&&&&&&&&dos.close();&&&&
&&&&&&&&&&&&fis.close();&&&&
&&&&&&&&&&&&socket.close();&&&&
&&&&&&&&&&&&server.close();&&&&
&&&&&&&&}&catch&(IOException&e)&{&&&&
&&&&&&&&&&&&e.printStackTrace();&&&&
&&&&&&&&}&&&&
客户端的代码:
import&com.login.R;&&
import&android.app.A&&&&
import&android.content.I&&
import&android.graphics.B&&&&
import&android.pressF&&&&
import&android.graphics.BitmapF&&&&
import&android.os.B&&&&
import&android.os.H&&&&
import&android.view.V&&&&
import&android.view.View.OnClickL&&&&
import&android.widget.B&&&&
import&android.widget.ImageV&&&
public&class&TestActivity&extends&Activity&{&&&&
&&&&private&ImageView&imageView&=&null;&&&&
&&&&private&Bitmap&bmp&=&null;&&
&&&&private&ImageView&imageView02;&&
&&&&private&Bitmap&bmp02;&&
&&&&private&Button&button02;&&
&&&&private&String&uploadFile="/mnt/sdcard/qt.png";&&&&
&&&&@Override&&&&
&&&&public&void&onCreate(Bundle&savedInstanceState)&{&&&&
&&&&&&&&super.onCreate(savedInstanceState);&&&&
&&&&&&&&setContentView(R.layout.image);&&&&
&&&&&&&&imageView&=&(ImageView)&findViewById(R.id.image01);&&&&
&&&&&&&&Button&btn&=&(Button)&findViewById(R.id.Button01);&&&&
&&&&&&&&btn.setOnClickListener(new&OnClickListener()&{&&&&
&&&&&&&&&&&&public&void&onClick(View&v)&{&&&&
&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&Socket&socket&=&null;&&&&
&&&&&&&&&&&&&&&&try&{&&&&
&&&&&&&&&&&&&&&&&&&&socket&=&new&Socket("192.168.1.203",&30000);&&&&
&&&&&&&&&&&&&&&&&&&&DataInputStream&dataInput&=&new&DataInputStream(socket.getInputStream());&&&&
&&&&&&&&&&&&&&&&&&&&int&size&=&dataInput.readInt();&&&&
&&&&&&&&&&&&&&&&&&&&byte[]&data&=&new&byte[size];&&&&
&&&&&&&&&&&&&&&&&&&&int&len&=&0;&&&&
&&&&&&&&&&&&&&&&&&&&while&(len&&&size)&{&&&&
&&&&&&&&&&&&&&&&&&&&&&&&len&+=&dataInput.read(data,&len,&size&-&len);&&&&
&&&&&&&&&&&&&&&&&&&&}&&&&
&&&&&&&&&&&&&&&&&&&&ByteArrayOutputStream&outPut&=&new&ByteArrayOutputStream();&&&&
&&&&&&&&&&&&&&&&&&&&bmp&=&BitmapFactory.decodeByteArray(data,&0,&data.length);&&&&
&&&&&&&&&&&&&&&&&&&&<press(CompressFormat.PNG,&100,&outPut);&&&&
&&&&&&&&&&&&&&&&&&&&imageView.setImageBitmap(bmp);&&&&
&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&&&&&//Bitmap&bitmap&=&BitmapFactory.decodeStream(dataInput);&&
&&&&&&&&&&&&&&&&&&&&//myHandler.obtainMessage().sendToTarget();&&&&
&&&&&&&&&&&&&&&&}&catch&(IOException&e)&{&&&&
&&&&&&&&&&&&&&&&&&&&e.printStackTrace();&&&&
&&&&&&&&&&&&&&&&}&&&
&&&&&&&&&&&&&&&&finally&{&&&&
&&&&&&&&&&&&&&&&&&&&try&{&&&&
&&&&&&&&&&&&&&&&&&&&&&&&socket.close();&&&&
&&&&&&&&&&&&&&&&&&&&}&catch&(IOException&e)&{&&&&
&&&&&&&&&&&&&&&&&&&&&&&&e.printStackTrace();&&&&
&&&&&&&&&&&&&&&&&&&&}&&&&
&&&&&&&&&&&&&&&&}&&&&
&&&&&&&&&&&&&&&&&&&&}&&&&
&&&&&&&&});&&
二、客户端向服务端发送图片的代码:
import&java.io.DataInputS&&
import&java.io.DataOutputS&&&&
import&java.io.FileInputS&&&&
import&java.io.IOE&&&&
import&java.net.InetSocketA&&&&
import&java.net.ServerS&&&&
import&java.net.S&&&&
public&class&Server02&{&&&&
&&&&public&static&void&main(String[]&args)&{&&&&
&&&&&&&&try&{&&&&
&&&&&&&&&&&&ServerSocket&server&=&new&ServerSocket(40000);&&&&
&&&&&&&&&&&&Socket&socket&=&server.accept();&&&&
&&&&&&&&&&&&DataInputStream&dos&=&new&DataInputStream(socket.getInputStream());&&&&
&&&&&&&&&&&&int&len&=&dos.available();&&&
&&&&&&&&&&&&System.out.println("len&=&"+len);&&
&&&&&&&&&&&&byte[]&data&=&new&byte[len];&&&&
&&&&&&&&&&&&dos.read(data);&&
&&&&&&&&&&&&&&
&&&&&&&&&&&&System.out.println("data&=&"+data);&&
&&&&&&&&&&&&dos.close();&&&&
&&&&&&&&&&&&socket.close();&&&&
&&&&&&&&&&&&server.close();&&&&
&&&&&&&&}&catch&(IOException&e)&{&&&&
&&&&&&&&&&&&e.printStackTrace();&&&&
&&&&&&&&}&&&&
imageView02&=&(ImageView)findViewById(R.id.image02);&&
&&&&&&&button02&=&(Button)findViewById(R.id.Button02);&&
&&&&&&&button02.setOnClickListener(new&OnClickListener(){&&
&&&&&&&&public&void&onClick(View&arg0)&{&&
&&&&&&&&&&&&Socket&&&
&&&&&&&&&&&&try&{&&
&&&&&&&&&&&&&&&&socket&=&new&Socket("192.168.1.203",40000);&&
&&&&&&&&&&&&&&&&DataOutputStream&out&=&new&DataOutputStream(socket.getOutputStream());&&&
&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&Bitmap&bitmap&=&BitmapFactory.decodeResource(getResources(),&R.drawable.qt);&&
&&&&&&&&&&&&&&&&imageView02.setImageBitmap(bitmap);&&
&&&&&&&&&&&&&&&&ByteArrayOutputStream&baos&=&new&ByteArrayOutputStream();&&
&&&&&&&&&&&&&&&&//读取图片到ByteArrayOutputStream&&&&&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&<pressFormat.PNG,&100,&baos);&&
&&&&&&&&&&&&&&&&byte[]&bytes&=&baos.toByteArray();&&
&&&&&&&&&&&&&&&&out.write(bytes);&&
&&&&&&&&&&&&&&&&&&
&&&&&&&&&&&&&&&&System.out.println("bytes---&"+bytes);&&
&&&&&&&&&&&&&&&&out.close();&&
&&&&&&&&&&&&&&&&socket.close();&&
&&&&&&&&&&&&}&catch&(UnknownHostException&e)&{&&
&&&&&&&&&&&&&&&&e.printStackTrace();&&
&&&&&&&&&&&&}&catch&(IOException&e)&{&&
&&&&&&&&&&&&&&&&e.printStackTrace();&&
&&&&&&&&&&&&}&&
&&&&&&&&}&&&&&&&&&&&&&
已投稿到:
以上网友发言只代表其个人观点,不代表新浪网的观点或立场。Socket编程------TCP文件传输(文档、声音、图片、视频和压缩包等)
Socket编程------TCP文件传输(文档、声音、图片、视频和压缩包等)
Java学习笔记
本程序是基于TCP稳定传输的文件传输,可以兼容任何类型任何&#26684;式的文件传输。
☆基本思路(客户端)
客户端需要明确服务器的ip地址以及端口,这样才可以去试着建立连接,如果连接失败,会出现异常。
连接成功,说明客户端与服务端建立了通道,那么通过IO流就可以进行数据的传输,而Socket对象已经提供了输入流和输出流对象,通过getInputStream(), getOutputStream()获取即可。
与服务端通讯结束后,关闭Socket。
☆基本思路(服务器端)
服务端需要明确它要处理的数据是从哪个端口进入的。
当有客户端访问时,要明确是哪个客户端,可通过accept()获取已连接的客户端对象,并通过该对象与客户端通过IO流进行数据传输。
当该客户端访问结束,关闭该客户端。
为了兼容所有类型的文件,客户端在发送的时候要求先将文件的后缀名发送过去,一边在服务器端建立正确类型的文件,这就要求客户端发送时要分两次发送,第一次发送后缀名,第二次发送文件正文。在第一次发送完成后要再次一个换行符作为标识符。
而对于服务器方,首先需要开多线程进行接收数据,因为用户是不止一个的,且线程类应实现Runnable接口。在接收后缀名时接收到换行符之后即建立新文件,方便后面输出到该文件中。
package cn.
import java.io.BufferedInputS
import java.io.F
import java.io.FileInputS
import java.io.IOE
import java.io.OutputS
import java.net.S
import javax.swing.JFileC
public class TcpUploadClient {
public static void main(String[] args) {
JFileChooser jfc=new JFileChooser();
File file=
int result=jfc.showOpenDialog(null);
if (result==JFileChooser.APPROVE_OPTION){
file=jfc.getSelectedFile();
//get the suffix
int index=file.getName().indexOf(&.&);
String suffix=file.getName().substring(index);
byte suffixBuf[]=suffix.getBytes();
Socket socket=
socket=new Socket(&192.168.1.106&, 8080);
//send the suffix first
OutputStream out=socket.getOutputStream();
out.write(suffixBuf, 0, suffixBuf.length);
out.write(&#39;\n&#39;);
//send the file
BufferedInputStream bis=new BufferedInputStream(new FileInputStream(file));
byte buf[]=new byte[1024];
int len=0;
while ((len=bis.read(buf))!=-1){
out.write(buf, 0, len);
socket.shutdownOutput();
out.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
服务器端:
package cn.
import java.io.F
import java.io.FileOutputS
import java.io.IOE
import java.io.InputS
import java.net.ServerS
import java.net.S
public class TcpUploadServer {
public static void main(String[] args) {
ServerSocket server=new ServerSocket(8080);
while (true){
Socket socket=server.accept();
new Thread(new SocketThread(socket)).start();
} catch (IOException e) {
e.printStackTrace();
class SocketThread implements Runnable{
public SocketThread(Socket socket) {
this.socket =
public void run() {
File dir=new File(&myFiles&);
if (!dir.exists()){
dir.mkdirs();
InputStream in=socket.getInputStream();
//this variable &count& is used creating many files with different subscripts
int count=1;
File file=
byte buf[]=new byte[1024];
while ((buf[i++]=(byte)in.read())!=&#39;\n&#39;){
suffix=new String(buf,0,i-1);
file=new File(dir, &file&+suffix);
while (file.exists()){
file=new File(dir, &file(&+(count++)+&)&+suffix);
int len=0;
FileOutputStream fos=new FileOutputStream(file);
while ((len=in.read(buf))!=-1){
fos.write(buf, 0, len);
in.close();
fos.close();
socket.close();
} catch (IOException e) {
e.printStackTrace();
我的热门文章
即使是一小步也想与你分享}

我要回帖

更多关于 socket视频传输编程 的文章

更多推荐

版权声明:文章内容来源于网络,版权归原作者所有,如有侵权请点击这里与我们联系,我们将及时删除。

点击添加站长微信