java使用Socket實現(xiàn)文件上傳功能
本文實例為大家分享了使用Socket實現(xiàn)文件上傳功能的具體代碼,供大家參考,具體內(nèi)容如下
文件上傳的步驟:
服務(wù)器端步驟:
1、創(chuàng)建ServerSocket
2、調(diào)用accept獲得客戶端Socket
3、定義字節(jié)數(shù)組
4、創(chuàng)建文件輸出流,獲得客戶端輸入流
5、循環(huán)讀取輸入流的字節(jié),寫入到文件輸出流
客戶端步驟:
1、創(chuàng)建Socket
2、獲得socket對象輸出流
3、創(chuàng)建文件輸入流
4、循環(huán)讀取文件輸入流字節(jié),寫入到輸出流
代碼實現(xiàn):
服務(wù)器端:
public class FileServer { ? ? public static final int PORT = 8888; ? ? public static final String PATH = "D:\\upload\\"; ? ? public void start(){ ? ? ? ? System.out.println("start..."); ? ? ? ? try ( ? //創(chuàng)建服務(wù)器端對象 ? ? ? ? ? ? ? ? ServerSocket server = new ServerSocket(PORT);){ ? ? ? ? ? ? while (true){ ? ? ? ? ? ? ? ? Socket socket = server.accept(); ? ? ? ? ? ? ? ? try ( ? //創(chuàng)建文件輸出流和網(wǎng)絡(luò)輸入流 ? ? ? ? ? ? ? ? ? ? ? ? DataInputStream in = new DataInputStream(socket.getInputStream()); ? ? ? ? ? ? ? ? ? ? ? ? //讀取哭護短發(fā)來的文件名,創(chuàng)建文件輸出流 ? ? ? ? ? ? ? ? ? ? ? ? FileOutputStream out = new FileOutputStream(PATH+in.readUTF())){ ? ? ? ? ? ? ? ? ? ? ? ? int len = 0; ? ? ? ? ? ? ? ? ? ? ? ? byte[] buffer = new byte[1024]; ? ? ? ? ? ? ? ? ? ? ? ? while ((len = in.read(buffer)) != -1){ ? ? ? ? ? ? ? ? ? ? ? ? ? ? out.write(buffer,0,len); ? ? ? ? ? ? ? ? ? ? ? ? } ? ? ? ? ? ? ? ? ? ? System.out.println("服務(wù)器保存完畢!"); ? ? ? ? ? ? ? ? } ? ? ? ? ? ? } ? ? ? ? } catch (IOException e) { ? ? ? ? ? ? e.printStackTrace(); ? ? ? ? } ? ? } ? ? public static void main(String[] args) { ? ? ? ? new FileServer().start(); ? ? } }
客戶端:
public class FileClient { ? ? /** ? ? ?* 發(fā)送文件 ? ? ?*/ ? ? public void sendFile(String ip,int port,String path){ ? ? ? ? File file = new File(path); ? ? ? ? try ( ? ? ? ? ? ? //創(chuàng)建連接,創(chuàng)建文件輸入流,網(wǎng)絡(luò)輸出流 ? ? ? ? ? ? Socket socket = new Socket(ip,port); ? ? ? ? ? ? InputStream in = new FileInputStream(path); ? ? ? ? ? ? DataOutputStream out = new DataOutputStream(socket.getOutputStream())){ ? ? ? ? ? ? //先發(fā)送文件給服務(wù)器 ? ? ? ? ? ? out.writeUTF(file.getName()); ? ? ? ? ? ? out.flush(); ? ? ? ? ? ? //讀取本地文件,寫入到網(wǎng)絡(luò)輸出流中 ? ? ? ? ? ? int len = 0; ? ? ? ? ? ? byte[] buffer = new byte[1024]; ? ? ? ? ? ? while ((len ?= in.read(buffer)) != -1){ ? ? ? ? ? ? ? ? out.write(buffer,0,len); ? ? ? ? ? ? } ? ? ? ? ? ? System.out.println("客戶端發(fā)送完畢!"); ? ? ? ? } catch (UnknownHostException e) { ? ? ? ? ? ? e.printStackTrace(); ? ? ? ? } catch (IOException e) { ? ? ? ? ? ? e.printStackTrace(); ? ? ? ? } ? ? } ? ? public static void main(String[] args) { ? ? ? ? new FileClient().sendFile("192.168.31.226",8888,"C:\\Users\\admin\\Desktop\\C.txt"); ? ? } }
實現(xiàn)效果:
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
IDEA啟動報錯Internal?error.?Please?refer?to?https://jb.gg/i
這篇文章主要介紹了IDEA啟動報錯Internal?error.?Please?refer?to?https://jb.gg/ide/critical-startup-errors解決辦法,本文給大家介紹的非常詳細,對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2023-04-04springboot application.yml使用@@pom文件配置問題
這篇文章主要介紹了springboot application.yml使用@@pom文件配置問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-07-07Java 實戰(zhàn)項目之精美物流管理系統(tǒng)的實現(xiàn)流程
讀萬卷書不如行萬里路,只學(xué)書上的理論是遠遠不夠的,只有在實戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用java+SpringBoot+Vue+maven+Mysql實現(xiàn)一個精美的物流管理系統(tǒng),大家可以在過程中查缺補漏,提升水平2021-11-11利用Spring Boot如何開發(fā)REST服務(wù)詳解
這篇文章主要給大家介紹了關(guān)于利用Spring Boot如何開發(fā)REST服務(wù)的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-12-12SpringBoot2 實現(xiàn)JPA分頁和排序分頁的案例
這篇文章主要介紹了SpringBoot2 實現(xiàn)JPA分頁和排序分頁的案例,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-01-01