亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

java使用Socket實(shí)現(xiàn)文件上傳功能

 更新時(shí)間:2022年02月24日 11:47:07   作者:??悲宸???  
這篇文章主要為大家詳細(xì)介紹了java使用Socket實(shí)現(xiàn)文件上傳功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了使用Socket實(shí)現(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對(duì)象輸出流

3、創(chuàng)建文件輸入流

4、循環(huán)讀取文件輸入流字節(jié),寫入到輸出流

代碼實(shí)現(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ù)器端對(duì)象
? ? ? ? ? ? ? ? ServerSocket server = new ServerSocket(PORT);){
? ? ? ? ? ? while (true){
? ? ? ? ? ? ? ? Socket socket = server.accept();
? ? ? ? ? ? ? ? try ( ? //創(chuàng)建文件輸出流和網(wǎng)絡(luò)輸入流
? ? ? ? ? ? ? ? ? ? ? ? DataInputStream in = new DataInputStream(socket.getInputStream());
? ? ? ? ? ? ? ? ? ? ? ? //讀取哭護(hù)短發(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");
? ? }
}

實(shí)現(xiàn)效果:

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • IDEA啟動(dòng)報(bào)錯(cuò)Internal?error.?Please?refer?to?https://jb.gg/ide/critical-startup-errors解決辦法

    IDEA啟動(dòng)報(bào)錯(cuò)Internal?error.?Please?refer?to?https://jb.gg/i

    這篇文章主要介紹了IDEA啟動(dòng)報(bào)錯(cuò)Internal?error.?Please?refer?to?https://jb.gg/ide/critical-startup-errors解決辦法,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-04-04
  • springboot application.yml使用@@pom文件配置問題

    springboot application.yml使用@@pom文件配置問題

    這篇文章主要介紹了springboot application.yml使用@@pom文件配置問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-07-07
  • Java 實(shí)戰(zhàn)項(xiàng)目之精美物流管理系統(tǒng)的實(shí)現(xiàn)流程

    Java 實(shí)戰(zhàn)項(xiàng)目之精美物流管理系統(tǒng)的實(shí)現(xiàn)流程

    讀萬卷書不如行萬里路,只學(xué)書上的理論是遠(yuǎn)遠(yuǎn)不夠的,只有在實(shí)戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用java+SpringBoot+Vue+maven+Mysql實(shí)現(xiàn)一個(gè)精美的物流管理系統(tǒng),大家可以在過程中查缺補(bǔ)漏,提升水平
    2021-11-11
  • 利用Spring Boot如何開發(fā)REST服務(wù)詳解

    利用Spring Boot如何開發(fā)REST服務(wù)詳解

    這篇文章主要給大家介紹了關(guān)于利用Spring Boot如何開發(fā)REST服務(wù)的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-12-12
  • java實(shí)現(xiàn)馬踏棋盤的完整版

    java實(shí)現(xiàn)馬踏棋盤的完整版

    這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)馬踏棋盤的完整版,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-02-02
  • Netty中的心跳檢測(cè)機(jī)制詳解

    Netty中的心跳檢測(cè)機(jī)制詳解

    這篇文章主要介紹了Netty中的心跳檢測(cè)機(jī)制詳解,Netty 是 基于 TCP 協(xié)議開發(fā)的,在四層協(xié)議 TCP 協(xié)議的實(shí)現(xiàn)中也提供了 keepalive 報(bào)文用來探測(cè)對(duì)端是否可用,TCP 層將在定時(shí)時(shí)間到后發(fā)送相應(yīng)的 KeepAlive 探針以確定連接可用性,需要的朋友可以參考下
    2023-12-12
  • 如何實(shí)現(xiàn)Spring?Event(異步事件)

    如何實(shí)現(xiàn)Spring?Event(異步事件)

    這篇文章主要介紹了如何實(shí)現(xiàn)Spring?Event(異步事件)問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-02-02
  • 在SpringBoot項(xiàng)目中的使用Swagger的方法示例

    在SpringBoot項(xiàng)目中的使用Swagger的方法示例

    這篇文章主要介紹了在SpringBoot項(xiàng)目中的使用Swagger的方法示例,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2021-05-05
  • JAVA 枚舉相關(guān)知識(shí)匯總

    JAVA 枚舉相關(guān)知識(shí)匯總

    這篇文章主要介紹了JAVA 枚舉相關(guān)知識(shí),文中講解的非常詳細(xì),代碼幫助大家更好的參考和學(xué)習(xí),感興趣的朋友可以了解下
    2020-06-06
  • SpringBoot2 實(shí)現(xiàn)JPA分頁和排序分頁的案例

    SpringBoot2 實(shí)現(xiàn)JPA分頁和排序分頁的案例

    這篇文章主要介紹了SpringBoot2 實(shí)現(xiàn)JPA分頁和排序分頁的案例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2021-01-01

最新評(píng)論