java基于socket傳輸zip文件功能示例
本文實(shí)例講述了java基于socket傳輸zip文件的方法。分享給大家供大家參考,具體如下:
服務(wù)器端程序:
import java.io.*; import java.net.*; import java.io.BufferedInputStream; public class SocketServer { ServerSocket ss=null; Socket s=null; DataInputStream inStream=null; DataOutputStream outStream=null; FileInputStream fin = null; public SocketServer() { try{ ss=new ServerSocket(765); s.setSoTimeout(3000); }catch(Exception e){ System.out.println(e.toString()); } } void waitForClient(){ try{ while(true){ s=ss.accept(); ThreadServer thread = new ThreadServer(s); thread.start(); } }catch(Exception e){ System.out.println(e.toString()); } } public static void main(String[] args) { SocketServer socketServer1 = new SocketServer(); socketServer1.waitForClient(); } }
線程類:
import java.io.*; import java.net.*; class ThreadServer extends Thread{ private Socket socket; private DataInputStream inStream=null; private DataOutputStream outStream=null; private FileInputStream fin = null; public ThreadServer(Socket sock){ this.socket = sock; } public void run(){ boolean bool = false; //while(!bool){ try{ inStream=new DataInputStream(socket.getInputStream()); outStream=new DataOutputStream(socket.getOutputStream()); fin = new FileInputStream("C:/temp/socket/200212060001_ds.zip"); //socket.setSoTimeout(3000); byte[] b = new byte[200]; int i; while((i=fin.read(b))!=-1){ outStream.write(b); } fin.close(); socket.close(); //bool = true; }catch(IOException ex){ System.out.println(ex); } //} } }
客戶端:
import java.net.*; import java.io.*; public class SocketClient{ Socket s=null; DataInputStream inStream=null; DataOutputStream outStream=null; FileOutputStream fout = null; public SocketClient() { try{ s=new Socket("192.9.207.52",765); //把這里的IP改成你運(yùn)行SocketServer.class的IP inStream=new DataInputStream(s.getInputStream()); outStream=new DataOutputStream(s.getOutputStream()); fout = new FileOutputStream("C:/temp/socket/test11.zip"); s.setSoTimeout(3000); waitData(); } catch(Exception e){ System.out.println(e.toString()); } } void init() throws Exception{ } void waitData(){ try{ byte[] b = new byte[200]; int i; while((i=inStream.read(b))!=-1){ fout.write(b); } fout.flush(); fout.close(); s.close(); }catch(Exception e){ System.out.println(e.toString()); } } public static void main(String[] args) { SocketClient socketClient1 = new SocketClient(); } }
更多關(guān)于java相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Java Socket編程技巧總結(jié)》、《Java文件與目錄操作技巧匯總》、《Java數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Java操作DOM節(jié)點(diǎn)技巧總結(jié)》和《Java緩存操作技巧匯總》
希望本文所述對(duì)大家java程序設(shè)計(jì)有所幫助。
- Java編程Socket實(shí)現(xiàn)多個(gè)客戶端連接同一個(gè)服務(wù)端代碼
- Java編程利用socket多線程訪問服務(wù)器文件代碼示例
- Java多線程編程實(shí)現(xiàn)socket通信示例代碼
- Java Socket通信介紹及可能遇到的問題解決
- Java基于Socket實(shí)現(xiàn)簡(jiǎn)單的多線程回顯服務(wù)器功能示例
- JAVA中實(shí)現(xiàn)原生的 socket 通信機(jī)制原理
- java使用websocket,并且獲取HttpSession 源碼分析(推薦)
- Java Socket編程服務(wù)器響應(yīng)客戶端實(shí)例代碼
相關(guān)文章
spring?boot集成jasypt?并實(shí)現(xiàn)自定義加解密的詳細(xì)步驟
由于項(xiàng)目中的配置文件?配置的地方過多,現(xiàn)將配置文件統(tǒng)一放到nacos上集中管理?且密碼使用加密的方式放在配置文件中,配置文件的加密使用加密庫jasypt,本文給大家介紹spring boot集成jasypt并實(shí)現(xiàn)自定義加解密,感興趣的朋友一起看看吧2023-08-08Java語言實(shí)現(xiàn)簡(jiǎn)單的酒店前臺(tái)管理小功能(實(shí)例代碼)
這篇文章主要介紹了Java語言實(shí)現(xiàn)簡(jiǎn)單的酒店前臺(tái)管理小功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-03-03前端與RabbitMQ實(shí)時(shí)消息推送未讀消息小紅點(diǎn)實(shí)現(xiàn)示例
這篇文章主要為大家介紹了前端與RabbitMQ實(shí)時(shí)消息推送未讀消息小紅點(diǎn)實(shí)現(xiàn)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07