服务端:
<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->
1
package
com.socket;
2
3 import java.io. * ;
4 import java.net. * ;
5 public class Server
6 {
7 public Server ()
8 {
9 }
10 static ServerSocket ss;
11 static Socket client;
12 public static void main (String args[])
13 {
14 try
15 {
16 ss = new ServerSocket ( 8099 );
17 client = ss.accept ();
18 }
19 catch (Exception ex)
20 {
21 ex.printStackTrace ();
22 }
23 // 实现多线程
24 new ServerThread (ss,client).start ();
25 }
26 }
27 class ServerThread extends Thread
28 {
29 ServerSocket ss;
30 Socket client;
31
32 public ServerThread (ServerSocket bb,Socket aa)
33 {
34 ss = bb;
35 client = aa;
36 }
37 public void run ()
38 {
39 try
40 {
41 File file = new File ( " test2.jpg " );
42 // 创建网络服务器接受客户请求
43 /
44 // 得到客户输入的信息,这样可以处理不同请求
45 BufferedReader in = new BufferedReader ( new InputStreamReader (client.getInputStream ()));
46 if ((in.readLine ()).equals ( " 1 " ))
47 {
48 file = new File ( " test1.jpg " );
49 }
50 // /
51 FileInputStream fos = new FileInputStream (file);
52 // 创建网络输出流并提供数据包装器
53 OutputStream netOut = client.getOutputStream ();
54 OutputStream doc = new DataOutputStream ( new BufferedOutputStream (netOut));
55 // 创建文件读取缓冲区
56 byte [] buf = new byte [ 2048 ];
57 int num = fos.read (buf);
58 while (num != ( - 1 ))
59 {
60 doc.write (buf, 0 ,num); // 把文件数据写出网络缓冲区
61 doc.flush (); // 刷新缓冲区把数据写往客户端
62 num = fos.read (buf); // 继续从文件中读取数据
63 }
64 fos.close ();
65 doc.close ();
66 }
67 catch (FileNotFoundException ex)
68 {
69 ex.printStackTrace ();
70 }
71 catch (IOException ex)
72 {
73 ex.printStackTrace ();
74 }
75 }
76 }
77
客户端:
2
3 import java.io. * ;
4 import java.net. * ;
5 public class Server
6 {
7 public Server ()
8 {
9 }
10 static ServerSocket ss;
11 static Socket client;
12 public static void main (String args[])
13 {
14 try
15 {
16 ss = new ServerSocket ( 8099 );
17 client = ss.accept ();
18 }
19 catch (Exception ex)
20 {
21 ex.printStackTrace ();
22 }
23 // 实现多线程
24 new ServerThread (ss,client).start ();
25 }
26 }
27 class ServerThread extends Thread
28 {
29 ServerSocket ss;
30 Socket client;
31
32 public ServerThread (ServerSocket bb,Socket aa)
33 {
34 ss = bb;
35 client = aa;
36 }
37 public void run ()
38 {
39 try
40 {
41 File file = new File ( " test2.jpg " );
42 // 创建网络服务器接受客户请求
43 /
44 // 得到客户输入的信息,这样可以处理不同请求
45 BufferedReader in = new BufferedReader ( new InputStreamReader (client.getInputStream ()));
46 if ((in.readLine ()).equals ( " 1 " ))
47 {
48 file = new File ( " test1.jpg " );
49 }
50 // /
51 FileInputStream fos = new FileInputStream (file);
52 // 创建网络输出流并提供数据包装器
53 OutputStream netOut = client.getOutputStream ();
54 OutputStream doc = new DataOutputStream ( new BufferedOutputStream (netOut));
55 // 创建文件读取缓冲区
56 byte [] buf = new byte [ 2048 ];
57 int num = fos.read (buf);
58 while (num != ( - 1 ))
59 {
60 doc.write (buf, 0 ,num); // 把文件数据写出网络缓冲区
61 doc.flush (); // 刷新缓冲区把数据写往客户端
62 num = fos.read (buf); // 继续从文件中读取数据
63 }
64 fos.close ();
65 doc.close ();
66 }
67 catch (FileNotFoundException ex)
68 {
69 ex.printStackTrace ();
70 }
71 catch (IOException ex)
72 {
73 ex.printStackTrace ();
74 }
75 }
76 }
77
<!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>-->
1
package
com.socket;
2
3
4 import java.io. * ;
5 import java.net. * ;
6
7 public class Client
8 {
9
10 public Client ()
11 {
12
13 }
14 public static void main(String[] args) throws Exception
15 {
16 // 使用本地文件系统接受网络数据并存为新文件
17 File file = new File( " newFile.jpg " );
18 if (file.length() != 0 )
19 {
20 file.delete();
21 System.out.println(file.getPath());
22 System.out.println( " 执行删除! " );
23
24 }
25 file.createNewFile();
26 RandomAccessFile raf = new RandomAccessFile(file, " rw " );
27 // 通过Socket连接服务器
28 Socket server = new Socket( " 127.0.0.1 " , 8099 );
29
30 // /
31 // 创建向服务器传送流对象
32 OutputStream out = server.getOutputStream();
33 PrintWriter netout = new PrintWriter(out, true );
34 BufferedReader line = new BufferedReader( new InputStreamReader(System.in));
35 netout.println(line.readLine());
36 // /
37
38 // 创建网络接受流接受服务器文件数据
39 InputStream netIn = server.getInputStream();
40 InputStream in = new DataInputStream( new BufferedInputStream(netIn));
41 // 创建缓冲区缓冲网络数据
42 byte [] buf = new byte [ 2048 ];
43 int num = in.read(buf);
44 while (num != ( - 1 ))
45 { // 是否读完所有数据
46 raf.write(buf, 0 ,num); // 将数据写往文件
47 raf.skipBytes(num); // 顺序写文件字节
48 num = in.read(buf); // 继续从网络中读取文件
49 }
50 in.close();
51 raf.close();
52 }
53 }
54
2
3
4 import java.io. * ;
5 import java.net. * ;
6
7 public class Client
8 {
9
10 public Client ()
11 {
12
13 }
14 public static void main(String[] args) throws Exception
15 {
16 // 使用本地文件系统接受网络数据并存为新文件
17 File file = new File( " newFile.jpg " );
18 if (file.length() != 0 )
19 {
20 file.delete();
21 System.out.println(file.getPath());
22 System.out.println( " 执行删除! " );
23
24 }
25 file.createNewFile();
26 RandomAccessFile raf = new RandomAccessFile(file, " rw " );
27 // 通过Socket连接服务器
28 Socket server = new Socket( " 127.0.0.1 " , 8099 );
29
30 // /
31 // 创建向服务器传送流对象
32 OutputStream out = server.getOutputStream();
33 PrintWriter netout = new PrintWriter(out, true );
34 BufferedReader line = new BufferedReader( new InputStreamReader(System.in));
35 netout.println(line.readLine());
36 // /
37
38 // 创建网络接受流接受服务器文件数据
39 InputStream netIn = server.getInputStream();
40 InputStream in = new DataInputStream( new BufferedInputStream(netIn));
41 // 创建缓冲区缓冲网络数据
42 byte [] buf = new byte [ 2048 ];
43 int num = in.read(buf);
44 while (num != ( - 1 ))
45 { // 是否读完所有数据
46 raf.write(buf, 0 ,num); // 将数据写往文件
47 raf.skipBytes(num); // 顺序写文件字节
48 num = in.read(buf); // 继续从网络中读取文件
49 }
50 in.close();
51 raf.close();
52 }
53 }
54