自己动手写简单的web应用服务器(4)—利用socket实现文件的下载

直接上源码:

服务器:

 1 package download;
 2 
 3 import java.io.BufferedInputStream;
 4 import java.io.BufferedOutputStream;
 5 import java.io.File;
 6 import java.io.FileInputStream;
 7 import java.io.IOException;
 8 import java.io.OutputStream;
 9 import java.net.ServerSocket;
10 import java.net.Socket;
11 
12 /**
13  * 目的:利用socket实现文件的下载。当有客户端连接到服务器时,自动从服务器上下载文件,客户端
14  *     按照系统毫秒数对下载的文件命名。
15  * 准备:服务器上准备要下载的文件d:/1.mp3。客户端接受文件后,将文件保存在d盘,文件采取系统毫秒数命名。    
16  */
17 public class Server {
18     
19     private ServerSocket ss;
20     private int port = 8080;//服务器开放的端口号
21     /**利用构造方法,初始化ServerSocket*/
22     public Server(){
23         try {
24             ss = new ServerSocket(port);
25         } catch (IOException e) {
26             e.printStackTrace();
27         }
28     }
29     /**服务器启动,等待客户端进入。当客户端进入,就产生socket对象,并将该对象交给线程去执行*/
30     public void start(){
31         while(true){
32             try {
33                 Socket s = ss.accept();//等待客户端进入
34                 Thread thread = new Thread(new Handler(s));//利用构造器传参数
35                 thread.run();
36             } catch (IOException e) {
37                 e.printStackTrace();
38             }
39         }
40     }
41     private class Handler implements Runnable {
42         private Socket s;
43         private byte[] buf = new byte[1024];//1K大小的缓冲区
44         public Handler(Socket s) {
45             this.s = s;
46         }
47 
48         public void run() {
49             try {
50                 File sourceFile = new File("d:/1.mp3");
51                 FileInputStream fis = new FileInputStream(sourceFile);
52                 BufferedInputStream bis = new BufferedInputStream(fis);
53                 OutputStream os = s.getOutputStream();
54                 BufferedOutputStream bos = new BufferedOutputStream(os);
55                 int size = -1;
56                 while((size=bis.read(buf))!=-1){
57                     bos.write(buf,0,size);
58                     bos.flush();
59                 }
60                 s.close();
61                 fis.close();
62             } catch (Exception e) {
63                 e.printStackTrace();
64             }
65         }
66 
67     }
68     public static void main(String[] args) {
69         Server s = new Server();
70         s.start();
71     }
72 }

客户端:

 1 package download;
 2 
 3 import java.io.BufferedInputStream;
 4 import java.io.BufferedOutputStream;
 5 import java.io.File;
 6 import java.io.FileOutputStream;
 7 import java.io.IOException;
 8 import java.io.InputStream;
 9 import java.net.Socket;
10 import java.net.UnknownHostException;
11 /**客户端程序,负责从客户端下载文件*/
12 public class Client {
13     private Socket s;
14     private byte[] buf = new byte[1024];//1K大小的缓冲区
15     
16     public void start(){
17         try {
18             s = new Socket("192.168.1.103", 8080);
19             String fileName = "d:/"+System.currentTimeMillis()+".mp3";
20             File targetFile = new File(fileName);
21             targetFile.createNewFile();
22             FileOutputStream fos = new FileOutputStream(targetFile);
23             BufferedOutputStream bos = new BufferedOutputStream(fos);
24             InputStream is = s.getInputStream();
25             BufferedInputStream bis = new BufferedInputStream(is);
26             int size = -1;
27             while((size=bis.read(buf))!=-1){
28                 bos.write(buf,0,size);
29                 bos.flush();
30             }
31             s.close();
32             fos.close();
33         } catch (UnknownHostException e) {
34             e.printStackTrace();
35         } catch (IOException e) {
36             e.printStackTrace();
37         }
38     }
39     public static void main(String[] args) {
40         Client c = new Client();
41         c.start();
42     }
43 }

 

posted @ 2014-10-09 09:32  迷音  阅读(948)  评论(0编辑  收藏  举报