溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶服務(wù)條款》

Java如何實(shí)現(xiàn)文件上傳服務(wù)器和客戶端

發(fā)布時(shí)間:2021-04-17 14:23:39 來(lái)源:億速云 閱讀:201 作者:小新 欄目:編程語(yǔ)言

這篇文章主要介紹Java如何實(shí)現(xiàn)文件上傳服務(wù)器和客戶端,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

具體內(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如何實(shí)現(xiàn)文件上傳服務(wù)器和客戶端”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問(wèn)一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI