java使用Socket實(shí)現(xiàn)文件上傳功能
本文實(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é),寫(xiě)入到文件輸出流
客戶端步驟:
1、創(chuàng)建Socket
2、獲得socket對(duì)象輸出流
3、創(chuàng)建文件輸入流
4、循環(huán)讀取文件輸入流字節(jié),寫(xiě)入到輸出流
代碼實(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ā)來(lái)的文件名,創(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();
? ? ? ? ? ? //讀取本地文件,寫(xiě)入到網(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/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文件配置問(wèn)題
這篇文章主要介紹了springboot application.yml使用@@pom文件配置問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-07-07
Java 實(shí)戰(zhàn)項(xiàng)目之精美物流管理系統(tǒng)的實(shí)現(xiàn)流程
讀萬(wàn)卷書(shū)不如行萬(wàn)里路,只學(xué)書(shū)上的理論是遠(yuǎn)遠(yuǎn)不夠的,只有在實(shí)戰(zhàn)中才能獲得能力的提升,本篇文章手把手帶你用java+SpringBoot+Vue+maven+Mysql實(shí)現(xiàn)一個(gè)精美的物流管理系統(tǒng),大家可以在過(guò)程中查缺補(bǔ)漏,提升水平2021-11-11
利用Spring Boot如何開(kāi)發(fā)REST服務(wù)詳解
這篇文章主要給大家介紹了關(guān)于利用Spring Boot如何開(kāi)發(fā)REST服務(wù)的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-12-12
java實(shí)現(xiàn)馬踏棋盤(pán)的完整版
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)馬踏棋盤(pán)的完整版,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-02-02
如何實(shí)現(xiàn)Spring?Event(異步事件)
這篇文章主要介紹了如何實(shí)現(xiàn)Spring?Event(異步事件)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-02-02
在SpringBoot項(xiàng)目中的使用Swagger的方法示例
這篇文章主要介紹了在SpringBoot項(xiàng)目中的使用Swagger的方法示例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-05-05
SpringBoot2 實(shí)現(xiàn)JPA分頁(yè)和排序分頁(yè)的案例
這篇文章主要介紹了SpringBoot2 實(shí)現(xiàn)JPA分頁(yè)和排序分頁(yè)的案例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-01-01

