自己动手写简单的web应用服务器(4)—利用socket实现文件的下载
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                自己动手写简单的web应用服务器(4)—利用socket实现文件的下载
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.                        
                                直接上源碼:
服務(wù)器:
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實(shí)現(xiàn)文件的下載。當(dāng)有客戶端連接到服務(wù)器時(shí),自動(dòng)從服務(wù)器上下載文件,客戶端 14 * 按照系統(tǒng)毫秒數(shù)對(duì)下載的文件命名。 15 * 準(zhǔn)備:服務(wù)器上準(zhǔn)備要下載的文件d:/1.mp3。客戶端接受文件后,將文件保存在d盤,文件采取系統(tǒng)毫秒數(shù)命名。 16 */ 17 public class Server { 18 19 private ServerSocket ss; 20 private int port = 8080;//服務(wù)器開放的端口號(hào) 21 /**利用構(gòu)造方法,初始化ServerSocket*/ 22 public Server(){ 23 try { 24 ss = new ServerSocket(port); 25 } catch (IOException e) { 26 e.printStackTrace(); 27 } 28 } 29 /**服務(wù)器啟動(dòng),等待客戶端進(jìn)入。當(dāng)客戶端進(jìn)入,就產(chǎn)生socket對(duì)象,并將該對(duì)象交給線程去執(zhí)行*/ 30 public void start(){ 31 while(true){ 32 try { 33 Socket s = ss.accept();//等待客戶端進(jìn)入 34 Thread thread = new Thread(new Handler(s));//利用構(gòu)造器傳參數(shù) 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大小的緩沖區(qū) 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 /**客戶端程序,負(fù)責(zé)從客戶端下載文件*/ 12 public class Client { 13 private Socket s; 14 private byte[] buf = new byte[1024];//1K大小的緩沖區(qū) 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 }
?
轉(zhuǎn)載于:https://www.cnblogs.com/miyin/p/4012603.html
總結(jié)
以上是生活随笔為你收集整理的自己动手写简单的web应用服务器(4)—利用socket实现文件的下载的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
 
                            
                        - 上一篇: 求一个关于高跟鞋的个性签名
- 下一篇: 祥字开头成语有哪些?
