廢話(huà)不多說(shuō)了,直接給大家貼代碼了,具體代碼如下所示:
公司專(zhuān)注于為企業(yè)提供網(wǎng)站建設(shè)、做網(wǎng)站、微信公眾號(hào)開(kāi)發(fā)、商城系統(tǒng)網(wǎng)站開(kāi)發(fā),成都小程序開(kāi)發(fā),軟件按需策劃設(shè)計(jì)等一站式互聯(lián)網(wǎng)企業(yè)服務(wù)。憑借多年豐富的經(jīng)驗(yàn),我們會(huì)仔細(xì)了解各客戶(hù)的需求而做出多方面的分析、設(shè)計(jì)、整合,為客戶(hù)設(shè)計(jì)出具風(fēng)格及創(chuàng)意性的商業(yè)解決方案,創(chuàng)新互聯(lián)建站更提供一系列網(wǎng)站制作和網(wǎng)站推廣的服務(wù)。
package com.lanqiao.demo2; import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.IOException; import java.io.OutputStream; import java.net.ServerSocket; import java.net.Socket; /** * @author * @version 創(chuàng)建時(shí)間:2017年6月12日 上午8:47:37 * 類(lèi)說(shuō)明:服務(wù)端上傳一個(gè)txt類(lèi)型的文件 * 注意:服務(wù)端是一直不關(guān)閉的 */ public class TestServer { // 這個(gè)路徑是我電腦上的一個(gè)記事本文件 private static final String PATH = "D:\\JavaFile_Test\\test\\網(wǎng)上JDK環(huán)境變量配置.txt"; public static void main(String[] args) { int count = 0; OutputStream os = null; ServerSocket severscoket = null; Socket s1 = null; BufferedInputStream bis =null; int len=0; try { // 創(chuàng)建 Socket 服務(wù) severscoket = new ServerSocket(8888); while (true) { // 阻塞 s1 = severscoket.accept(); //服務(wù)端被連接的次數(shù) count++; System.out.println("---服務(wù)端開(kāi)啟 " + count + " 次---"); // 服務(wù)端寫(xiě)入文件 os = s1.getOutputStream(); //創(chuàng)建一個(gè)BufferedInputStream對(duì)象讀取我電腦上的文件 bis = new BufferedInputStream(new FileInputStream(PATH)); //每次寫(xiě)入512個(gè)字節(jié) byte[] b = new byte[512]; while ((len = bis.read(b)) != -1) { os.write(b, 0, len); } s1.shutdownOutput(); os.flush(); } } catch (IOException e) { // TODO 自動(dòng)生成的 catch 塊 e.printStackTrace(); } finally { try { if (os != null) os.close(); } catch (IOException e) { // TODO 自動(dòng)生成的 catch 塊 e.printStackTrace(); } } } } package com.lanqiao.demo2; import java.io.BufferedInputStream; import java.io.IOException; import java.net.Socket; /** * @author * @version 創(chuàng)建時(shí)間:2017年6月12日 上午9:04:36 * 類(lèi)說(shuō)明:客戶(hù)端打印出從服務(wù)端下載的txt內(nèi)容 */ public class TestClient { public static void main(String[] args) { BufferedInputStream bis = null; Socket socket = null; int len=0; try { // 通過(guò)IP地址和端口號(hào)創(chuàng)建一個(gè)Socket對(duì)象 socket = new Socket("127.0.0.1", 8888); // 客戶(hù)端讀取文件 bis = new BufferedInputStream(socket.getInputStream()); // 每次讀512個(gè)字節(jié) byte[] b = new byte[512]; //當(dāng)讀取的字節(jié)不為空 循環(huán)打印下載的內(nèi)容 while ((len = bis.read(b)) != -1) { System.out.println(new String(b, 0, len)); } } catch (IOException e) { // TODO 自動(dòng)生成的 catch 塊 e.printStackTrace(); } finally { //關(guān)閉客戶(hù)端的輸入流對(duì)象 和 Socket對(duì)象 try { if(bis!=null) bis.close(); if(socket!=null) socket.close(); } catch (IOException e) { // TODO 自動(dòng)生成的 catch 塊 e.printStackTrace(); } } } }
以上所述是小編給大家介紹的Java中Socket下載一個(gè)文本文件,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)創(chuàng)新互聯(lián)網(wǎng)站的支持!
網(wǎng)站題目:Java中Socket下載一個(gè)文本文件
鏈接URL:http://www.chinadenli.net/article4/jsgdie.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站維護(hù)、網(wǎng)頁(yè)設(shè)計(jì)公司、動(dòng)態(tài)網(wǎng)站、企業(yè)建站、云服務(wù)器、搜索引擎優(yōu)化
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶(hù)投稿、用戶(hù)轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話(huà):028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)