.net core 文件流保存图片_Java--利用TCP实现文件上传
生活随笔
收集整理的這篇文章主要介紹了
.net core 文件流保存图片_Java--利用TCP实现文件上传
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Java--利用TCP實現文件上傳
博客說明
文章所涉及的資料來自互聯網整理和個人總結,意在于個人學習和經驗匯總,如有什么地方侵權,請聯系本人刪除,謝謝!
圖解
步驟
代碼實現
服務器 import java.io.*; import java.net.ServerSocket; import java.net.Socket;/*** @author ServerTCP* @date 2020/4/25 10:51 上午*/ public class ServerTCP {public static void main(String[] args) throws IOException {System.out.println("服務啟動,等待連接中");//創建ServerSocket對象,綁定端口,開始等待連接ServerSocket ss = new ServerSocket(8888);//接受accept方法,返回socket對象Socket server = ss.accept();//獲取輸入對象,讀取文件BufferedInputStream bis = new BufferedInputStream(server.getInputStream());//保存到本地BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("copy.txt"));//創建字節數組byte[] b = new byte[1024 * 8];//讀取字符數組int len;while ((len = bis.read(b)) != -1) {bos.write(b, 0, len);}//關閉資源bos.close();bis.close();server.close();System.out.println("上傳成功");} } 客戶端 import java.io.*; import java.net.Socket;/*** @author ClientTCP* @date 2020/4/25 10:58 上午*/ public class ClientTCP {public static void main(String[] args) throws IOException {//創建輸入流BufferedInputStream bis = new BufferedInputStream(new FileInputStream("in.txt"));//創建SocketSocket client = new Socket("127.0.0.1", 8888);//輸出流BufferedOutputStream bos = new BufferedOutputStream(client.getOutputStream());//寫出數據byte[] b = new byte[1024 * 8];int len;while ((len = bis.read(b)) != -1) {bos.write(b, 0, len);bos.flush();}System.out.println("文件已上傳");//關閉資源bos.close();client.close();bis.close();System.out.println("文件上傳完成");} } 結果感謝
黑馬程序員
以及勤勞的自己
總結
以上是生活随笔為你收集整理的.net core 文件流保存图片_Java--利用TCP实现文件上传的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: js div加载html_说说 Vue.
- 下一篇: 中报表导出带表头_来看看Java是 如何