Java實(shí)現(xiàn)文件上傳服務(wù)器和客戶端
本文實(shí)例為大家分享了Java實(shí)現(xiàn)文件上傳服務(wù)器和客戶端的具體代碼,供大家參考,具體內(nèi)容如下
文件上傳服務(wù)器端:
/** * 使用TCP協(xié)議實(shí)現(xiàn)上傳功能的服務(wù)器端 * 思路: * 新建ServerSocket * 等待客戶端連接 * 連接上后開(kāi)啟子線程,把連接獲取的Socket傳給子線程 * 循環(huán)進(jìn)行 * @author yajun * */ public class UploadServer { public static void main(String[] args) { UploadServer server=new UploadServer(); UploadThread command=new UploadThread(); server.start(command); } /** * 功能:接受連接,開(kāi)啟子線程,循環(huán) * @param command 用于下載的子線程對(duì)象,該對(duì)象實(shí)現(xiàn)了Runnable接口 */ private void start(UploadThread command){ //局部變量 ServerSocket serverSocket = null; Socket transSocket; //業(yè)務(wù)邏輯 try { serverSocket=new ServerSocket(55555); while(true){ System.out.println("等待連接……"); transSocket=serverSocket.accept(); int i=0; i++; System.out.println("第"+i+"個(gè)連接"); //用不用在下載完后關(guān)閉線程呢??? command.setSocket(transSocket); Executors.newFixedThreadPool(5).execute(command); } //異常捕獲 } catch (IOException e) { e.printStackTrace(); //關(guān)閉資源 } finally{ try { serverSocket.close(); } catch (IOException e) { e.printStackTrace(); } }//End of try }//End of connect @Test public void testConnect() { //測(cè)試任務(wù):先運(yùn)行服務(wù)器端,然后多次運(yùn)行客戶端,服務(wù)器段可以不斷創(chuàng)建子線程,則測(cè)試成功 //測(cè)試準(zhǔn)備:構(gòu)造一個(gè)線程,用于模擬下載線程 UploadThread command=new UploadThread(); start(command); } @Test public void testDown() throws IOException { byte[] buf; ByteArrayInputStream bis; String str="canglaoshi.avi\ncontent,content,content"; buf=str.getBytes(); bis=new ByteArrayInputStream(buf); UploadThread ut=new UploadThread(); ut.down(bis); } } //完成各個(gè)傳輸任務(wù)的子線程 class UploadThread implements Runnable{ Socket socket; public UploadThread(){} public UploadThread(Socket socket){ this.socket=socket; } @Override public void run() { InputStream in; try { in = socket.getInputStream(); down(in); //異常處理 } catch (IOException e) { e.printStackTrace(); } finally{ try { socket.close(); } catch (IOException e) { e.printStackTrace(); } } //測(cè)試代碼 /*try { Thread.sleep(5000); System.out.println(Thread.currentThread().getName()+",復(fù)制完畢"); } catch (InterruptedException e) { e.printStackTrace(); }*/ }//End of run public void setSocket(Socket socket){ this.socket=socket; } //下載方法 /** * 目標(biāo):把InputStream中的數(shù)據(jù)寫(xiě)入到本地 * 思路: * 1.獲取輸入流,最好傳入輸入流,而不是直接從Socket獲取,傳入有利用單元測(cè)試 * 2.從輸入流中讀到文件名 * 3.新建文件和文件輸出流 * 4.從輸入流中讀到文件內(nèi)容到文件輸出流 * 5. * @throws IOException */ public void down(InputStream in) throws IOException{ //局部變量 char ch; char[] nameArr=new char[256]; byte[] buf=new byte[1024]; String name=""; OutputStream out = null; //業(yè)務(wù)邏輯 try { //第一步:獲取文件名,構(gòu)造文件輸出流 int i=0; while((ch=(char) in.read())!='\n'){ nameArr[i++]= ch; } //name=nameArr.toString();//這句話無(wú)法將字符數(shù)組轉(zhuǎn)換為字符串,需用下面的語(yǔ)句 name=new String(nameArr); System.out.println("要下載的文件為:"+name); out=new FileOutputStream("src\\down\\"+name); //第二步:將輸入流中的其他內(nèi)容寫(xiě)入到文件 int len; while((len=in.read(buf))!=-1){ out.write(buf,0,len); } out.flush(); //異常捕獲 } catch (IOException e) { e.printStackTrace(); //關(guān)閉資源 }finally{ //疑問(wèn):兩個(gè)捕獲可不可以放到一塊呢,怎樣處理關(guān)閉流時(shí)的異常最好呢? in.close(); out.close(); } //調(diào)試 System.out.println(name); } }//End of UploadThread
文件上傳客戶端:
/** * 使用TCP協(xié)議實(shí)現(xiàn)上傳功能的客戶端 * @author yajun */ public class UploadClient { public static void main(String[] args) { UploadClient client=new UploadClient(); client.upload("src\\thursday\\AsListTest.java"); } /** * 作用:上傳文件到服務(wù)器 * 1.建立到服務(wù)器的連接 * 2.獲取輸出流 * 3.將文件內(nèi)容寫(xiě)入到輸出流 * 4.獲取服務(wù)器的響應(yīng) */ private void upload(String name){ Socket socket=null; OutputStream out; try { socket=new Socket("127.0.0.1", 55555); out=socket.getOutputStream(); write2OutputStream(name, out); //異常捕獲 } catch (UnknownHostException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } @Test public void testUpload() { upload("src\\status.xml"); } /** * 作用:傳入文件名和輸出流,將文件寫(xiě)入到輸出流 * @param path * @throws IOException */ private void write2OutputStream(String path,OutputStream out) throws IOException{ FileInputStream fis = null; byte[] buf=new byte[1024]; String fileName=""; //業(yè)務(wù)邏輯 try { //1.寫(xiě)入文件名 fileName=path.substring(path.lastIndexOf('\\')+1); System.out.println("您要上傳的文件名為:"+fileName); out.write(fileName.getBytes()); out.write('\n'); //2.寫(xiě)入文件內(nèi)容 fis=new FileInputStream(path); int len; while((len=fis.read(buf))!=-1){ out.write(buf, 0, len); } out.flush(); //異常處理 } catch (IOException e) { e.printStackTrace(); //關(guān)閉資源 } finally{ fis.close(); out.close(); } }//End of upload @Test public void testWrite2OutputStream() throws IOException { ByteArrayOutputStream out = null; try { out=new ByteArrayOutputStream(); write2OutputStream("src\\status.xml", out); System.out.println(out.toString("utf-8")); } catch (IOException e) { e.printStackTrace(); } finally{ out.close(); } } }
本文已被整理到了《Java上傳操作技巧匯總》,歡迎大家學(xué)習(xí)閱讀。
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
通過(guò)Java來(lái)測(cè)試JSON和Protocol Buffer的傳輸文件大小
這篇文章主要介紹了通過(guò)Java來(lái)測(cè)試JSON和Protocol Buffer的傳輸文件大小,Protocol Buffer(文中簡(jiǎn)稱Protobuffer)是谷歌開(kāi)發(fā)的新的文件傳輸格式,需要的朋友可以參考下2015-12-12SpringCloud實(shí)現(xiàn)Redis在各個(gè)微服務(wù)的Session共享問(wèn)題
Redis是運(yùn)行在內(nèi)存中,查取速度很快。本文重點(diǎn)給大家介紹SpringCloud實(shí)現(xiàn)Redis在各個(gè)微服務(wù)的Session共享,感興趣的朋友一起看看吧2018-08-08java獲取指定開(kāi)始時(shí)間與結(jié)束時(shí)間之間的所有日期
這篇文章主要為大家詳細(xì)介紹了java獲取指定開(kāi)始時(shí)間與結(jié)束時(shí)間之間的所有日期,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-05-05JVM內(nèi)存區(qū)域劃分相關(guān)原理詳解
這篇文章主要介紹了JVM內(nèi)存區(qū)域劃分相關(guān)原理詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-10-10springboot+rabbitmq實(shí)現(xiàn)智能家居實(shí)例詳解
這篇文章主要為大家介紹了springboot+rabbitmq實(shí)現(xiàn)智能家居的示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-07-07HashMap方法之Map.getOrDefault()解讀及案例
這篇文章主要介紹了HashMap方法之Map.getOrDefault()解讀及案例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-03-03Java實(shí)現(xiàn)入?yún)?shù)據(jù)批量數(shù)據(jù)校驗(yàn)詳解
在業(yè)務(wù)處理中一般入?yún)⑹菃螚l數(shù)據(jù),這樣數(shù)據(jù)校驗(yàn)比較容易,但是這種方法對(duì)于集合數(shù)據(jù)的校驗(yàn)不適用,下面我們就來(lái)看看如何對(duì)入?yún)?shù)據(jù)進(jìn)行批量數(shù)據(jù)校驗(yàn)吧2024-02-02