国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 開發(fā) > Java > 正文

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

2024-07-13 10:16:35
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

本文實(shí)例為大家分享了Java實(shí)現(xiàn)文件上傳服務(wù)器和客戶端的具體代碼,供大家參考,具體內(nèi)容如下

文件上傳服務(wù)器端:

/**  * 使用TCP協(xié)議實(shí)現(xiàn)上傳功能的服務(wù)器端  * 思路:  * 新建ServerSocket  * 等待客戶端連接  * 連接上后開啟子線程,把連接獲取的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);  }    /**   * 功能:接受連接,開啟子線程,循環(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ù)寫入到本地   * 思路:   * 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)容寫入到文件    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)容寫入到輸出流   * 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");  }  /**   * 作用:傳入文件名和輸出流,將文件寫入到輸出流   * @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.寫入文件名    fileName=path.substring(path.lastIndexOf('//')+1);    System.out.println("您要上傳的文件名為:"+fileName);    out.write(fileName.getBytes());    out.write('/n');    //2.寫入文件內(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();   }     }   }

 以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持VeVb武林網(wǎng)。


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到JAVA教程頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 丹阳市| 泽普县| 霍山县| 高州市| 兰坪| 西林县| 温宿县| 永福县| 黄石市| 方山县| 保山市| 郎溪县| 滦平县| 顺昌县| 辽阳市| 类乌齐县| 兖州市| 建平县| 东明县| 康马县| 石柱| 新绛县| 绍兴县| 绵阳市| 上高县| 五河县| 阳曲县| 达尔| 建平县| 麻栗坡县| 资兴市| 汶上县| 临洮县| 十堰市| 嘉义县| 泗水县| 乌拉特后旗| 乌鲁木齐市| 锦屏县| 南京市| 林甸县|