日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Tcp实现文件上传

發布時間:2024/9/27 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Tcp实现文件上传 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
package com.wuming.lesson02;import java.io.*; import java.net.ServerSocket; import java.net.Socket;public class TcpServerDemo02 {public static void main(String[] args) throws Exception {//1.創服務ServerSocket serverSocket = new ServerSocket(9000);//2.監聽客戶端連接Socket socket = serverSocket.accept();//阻塞式監聽,一直等待客戶端連接//3.獲取輸入流InputStream is = socket.getInputStream();//4.文件輸出FileOutputStream fos = new FileOutputStream(new File("receive2.jpg"));//自動生成圖片byte[] buffer = new byte[1024];int len;while((len=is.read(buffer))!=-1){fos.write(buffer,0,len);}//通知客戶端我接受完畢了OutputStream os = socket.getOutputStream();os.write("我接受完畢了,你可以斷開了".getBytes());//關閉資源fos.close();is.close();socket.close();serverSocket.close();} } =========================== package com.wuming.lesson02;import java.io.*; import java.net.InetAddress; import java.net.Socket;public class TcpClientDemo02 {public static void main(String[] args) throws Exception {//1.創Socket連接Socket socket = new Socket(InetAddress.getByName("127.0.0.1"),9000);//2.創輸出流OutputStream os = socket.getOutputStream();//3.讀取文件FileInputStream fis = new FileInputStream(new File("wg.jpg"));//圖片放入src同級路徑//4.寫文件byte[] buffer = new byte[1024];int len;while((len=fis.read(buffer))!=-1){os.write(buffer,0,len);}//通知服務器,我已經結束了socket.shutdownOutput();//我已經傳輸完了//確定服務器接受完畢,才斷開InputStream inputStream = socket.getInputStream();ByteArrayOutputStream baos = new ByteArrayOutputStream();byte[] buffer2 = new byte[1024];int len2;while((len2=inputStream.read(buffer2))!=-1){baos.write(buffer2,0,len2);}System.out.println(baos.toString());//5.關閉資源baos.close();inputStream.close();fis.close();os.close();socket.close();} }

=================

先啟動服務端,在啟動客戶端,控制臺輸出:

在src同級路徑生成一個圖片

?

?

總結

以上是生活随笔為你收集整理的Tcp实现文件上传的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。