Java Socket图片文件传输方法学习

java socket通信-传输文件图片

ClientTcpSend.java 客户端发送类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
package com.yjf.test;
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 String clientip = "127.0.0.1" ;
     public static int port = 33456 ;
 
     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(clientip, port), 30 * 1000 );
                 dos = new DataOutputStream(socket.getOutputStream());
                 File file = new File( "F:\\aa.xml" );
                 fis = new FileInputStream(file);
                 sendBytes = new byte [ 1024 * 4 ];
                 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 服务器监听类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package com.yjf.test;
import <span id= "0_nwp" style= "width: auto; height: auto; float: none;" ><a id= "0_nwl" href= "http://cpro.baidu.com/cpro/ui/uijs.php?c=news&cf=1001&ch=0&di=128&fv=11&jk=7421ce4a3a6c2ba4&k=java&k0=java&kdi0=0&luki=5&n=10&p=baidu&q=06011078_cpr&rb=0&rs=1&seller_id=1&sid=a42b6c3a4ace2174&ssp2=1&stid=0&t=tpclicked3_hc&tu=u1922429&u=http%3A%2F%2Fwww%2Eadmin10000%2Ecom%2Fdocument%2F576%2Ehtml&urlid=0" target= "_blank" mpid= "0" style= "text-decoration: none;" ><span style= "color:#0000ff;font-size:14px;width:auto;height:auto;float:none;" >java</span></a></span>.net.*;
import java.io.*;
 
public class ServerTcpListener implements Runnable {
 
     public static void main(String[] args) {
 
         try {
             final ServerSocket <span id= "1_nwp" style= "width: auto; height: auto; float: none;" ><a id= "1_nwl" href= "http://cpro.baidu.com/cpro/ui/uijs.php?c=news&cf=1001&ch=0&di=128&fv=11&jk=7421ce4a3a6c2ba4&k=server&k0=server&kdi0=0&luki=3&n=10&p=baidu&q=06011078_cpr&rb=0&rs=1&seller_id=1&sid=a42b6c3a4ace2174&ssp2=1&stid=0&t=tpclicked3_hc&tu=u1922429&u=http%3A%2F%2Fwww%2Eadmin10000%2Ecom%2Fdocument%2F576%2Ehtml&urlid=0" target= "_blank" mpid= "1" style= "text-decoration: none;" ><span style= "color:#0000ff;font-size:14px;width:auto;height:auto;float:none;" >server</span></a></span> = new ServerSocket(ClientTcpSend.port);
             Thread th = new Thread( new Runnable() {
                 public void run() {
                     while ( true ) {
                         try {
                             System.out.println( "开始监听..." );
                             Socket <span id= "2_nwp" style= "width: auto; height: auto; float: none;" ><a id= "2_nwl" href= "http://cpro.baidu.com/cpro/ui/uijs.php?c=news&cf=1001&ch=0&di=128&fv=11&jk=7421ce4a3a6c2ba4&k=socket&k0=socket&kdi0=0&luki=4&n=10&p=baidu&q=06011078_cpr&rb=0&rs=1&seller_id=1&sid=a42b6c3a4ace2174&ssp2=1&stid=0&t=tpclicked3_hc&tu=u1922429&u=http%3A%2F%2Fwww%2Eadmin10000%2Ecom%2Fdocument%2F576%2Ehtml&urlid=0" target= "_blank" mpid= "2" style= "text-decoration: none;" ><span style= "color:#0000ff;font-size:14px;width:auto;height:auto;float:none;" >socket</span></a></span> = 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 <span id= "3_nwp" style= "width: auto; height: auto; float: none;" ><a id= "3_nwl" href= "http://cpro.baidu.com/cpro/ui/uijs.php?c=news&cf=1001&ch=0&di=128&fv=11&jk=7421ce4a3a6c2ba4&k=socket&k0=socket&kdi0=0&luki=4&n=10&p=baidu&q=06011078_cpr&rb=0&rs=1&seller_id=1&sid=a42b6c3a4ace2174&ssp2=1&stid=0&t=tpclicked3_hc&tu=u1922429&u=http%3A%2F%2Fwww%2Eadmin10000%2Ecom%2Fdocument%2F576%2Ehtml&urlid=0" target= "_blank" mpid= "3" style= "text-decoration: none;" ><span style= "color:#0000ff;font-size:14px;width:auto;height:auto;float:none;" >socket</span></a></span>) {
 
         byte [] inputByte = null ;
         int length = 0 ;
         DataInputStream <span id= "4_nwp" style= "width: auto; height: auto; float: none;" ><a id= "4_nwl" href= "http://cpro.baidu.com/cpro/ui/uijs.php?c=news&cf=1001&ch=0&di=128&fv=11&jk=7421ce4a3a6c2ba4&k=dis&k0=dis&kdi0=0&luki=2&n=10&p=baidu&q=06011078_cpr&rb=0&rs=1&seller_id=1&sid=a42b6c3a4ace2174&ssp2=1&stid=0&t=tpclicked3_hc&tu=u1922429&u=http%3A%2F%2Fwww%2Eadmin10000%2Ecom%2Fdocument%2F576%2Ehtml&urlid=0" target= "_blank" mpid= "4" style= "text-decoration: none;" ><span style= "color:#0000ff;font-size:14px;width:auto;height:auto;float:none;" >dis</span></a></span> = null ;
         FileOutputStream fos = null ;
         try {
             try {
 
                 dis = new DataInputStream(socket.getInputStream());
                 fos = new FileOutputStream( new File( "E:\\aa.xml" ));
                 inputByte = new byte [ 1024 * 4 ];
                 System.out.println( "开始接收数据..." );
                 while ((length = dis.read(inputByte, 0 , inputByte.length)) > 0 ) {
                     fos.write(inputByte, 0 , length);
                     fos.flush();
                 }
                 System.out.println( "完成接收" );
             } finally {
                 if (fos != null )
                     fos.close();
                 if (<span id= "5_nwp" style= "width: auto; height: auto; float: none;" ><a id= "5_nwl" href= "http://cpro.baidu.com/cpro/ui/uijs.php?c=news&cf=1001&ch=0&di=128&fv=11&jk=7421ce4a3a6c2ba4&k=dis&k0=dis&kdi0=0&luki=2&n=10&p=baidu&q=06011078_cpr&rb=0&rs=1&seller_id=1&sid=a42b6c3a4ace2174&ssp2=1&stid=0&t=tpclicked3_hc&tu=u1922429&u=http%3A%2F%2Fwww%2Eadmin10000%2Ecom%2Fdocument%2F576%2Ehtml&urlid=0" target= "_blank" mpid= "5" style= "text-decoration: none;" ><span style= "color:#0000ff;font-size:14px;width:auto;height:auto;float:none;" >dis</span></a></span> != null )
                     dis.close();
                 if (<span id= "6_nwp" style= "width: auto; height: auto; float: none;" ><a id= "6_nwl" href= "http://cpro.baidu.com/cpro/ui/uijs.php?c=news&cf=1001&ch=0&di=128&fv=11&jk=7421ce4a3a6c2ba4&k=socket&k0=socket&kdi0=0&luki=4&n=10&p=baidu&q=06011078_cpr&rb=0&rs=1&seller_id=1&sid=a42b6c3a4ace2174&ssp2=1&stid=0&t=tpclicked3_hc&tu=u1922429&u=http%3A%2F%2Fwww%2Eadmin10000%2Ecom%2Fdocument%2F576%2Ehtml&urlid=0" target= "_blank" mpid= "6" style= "text-decoration: none;" ><span style= "color:#0000ff;font-size:14px;width:auto;height:auto;float:none;" >socket</span></a></span> != null )
                     socket.close();
             }
         } catch (Exception e) {
 
         }
     }
}

关注微信号:javalearns  随时随地学Java

或扫一扫Java Socket图片文件传输方法学习随时随地学Java

你可能感兴趣的:(java,socket)