最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

SocketIo+SpringMvc實(shí)現(xiàn)文件的上傳下載功能

 更新時(shí)間:2018年08月08日 09:15:45   作者:charlyFeng  
這篇文章主要介紹了SocketIo+SpringMvc實(shí)現(xiàn)文件的上傳下載功能,socketIo不僅可以用來(lái)做聊天工具,也可以實(shí)現(xiàn)局域網(wǎng)。文中給出了實(shí)現(xiàn)代碼,需要的朋友可以參考下

socketIo不僅可以用來(lái)做聊天工具,也可以實(shí)現(xiàn)局域網(wǎng)(當(dāng)然你如果有外網(wǎng)也可用外網(wǎng))內(nèi)實(shí)現(xiàn)文件的上傳和下載,下面是代碼的效果演示:

GIT地址: https://github.com/fengcharly/sockeio-springMvcUpload.git

部分代碼如下:

服務(wù)端的代碼:

ChuanServer:

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.nio.channels.FileChannel;
public class ChuanServer {
 public static void protServer(String po) throws IOException {
    int port = Integer.parseInt(po);
  ServerSocket serverSocket = new ServerSocket(port);
  while (true) {
   final Socket clientSocket = serverSocket.accept();
   new Thread() {
    @Override
    public void run() {
     try {
      BufferedReader br = new BufferedReader(
        new InputStreamReader(clientSocket.getInputStream(), "GBK")
      );
      InputStream is = clientSocket.getInputStream();
      PrintStream pr = new PrintStream(
        clientSocket.getOutputStream()
      );
      pr.println("我是服務(wù)端");
      String str = br.readLine();
      System.out.println("br.readLine():" + str);
      System.out.println("服務(wù)端來(lái)接收了!!");
      out(is, str);
     } catch (Exception e) {
      e.printStackTrace();
     }
    }
   }.start();
  }
 }
 public static void out(InputStream is, String str) throws IOException {
  FileOutputStream fo = new FileOutputStream("C:\\Users\\Administrator\\Desktop\\upload\\" + str);
  BufferedInputStream bi = new BufferedInputStream(is);
  BufferedOutputStream bo = new BufferedOutputStream(fo);
  int len = 0;
  while ((len=bi.read())!=-1){
   bo.write(len);
  }
  bi.close();
  bo.close();
 }
}

這里我固定了上傳后保存的路徑為:"C:\Users\Administrator\Desktop\upload\"

PortController:

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import socket.ChuanServer;
import java.io.IOException;
@Controller
public class PortController {
 @RequestMapping("/port")
 public String port(String port,Model model){
  model.addAttribute("port",port);
  try {
   ChuanServer.protServer(port);
  } catch (IOException e) {
   e.printStackTrace();
  }
  return "success";
 }
}

再來(lái)看下上傳的客戶(hù)端的代碼:

UpLoadController:

@Controller
@RequestMapping("/")
public class UpLoadController {
 @Autowired
 private UpService upService;
 private String zhuan="";
 @RequestMapping("/upload")
 public String upload(@RequestParam(value = "file", required = false) MultipartFile file,
       HttpServletRequest request, @RequestParam("iphost") String iphost,@RequestParam("port") String port,Model model) throws IOException {
  String fileName = file.getOriginalFilename();
  InputStream is = file.getInputStream();
  upService.upload(fileName,is,iphost,port);
  return "success";
 }
}

UpServiceImpl:

@Service
public class UpServiceImpl implements UpService {
 @Override
 public void upload(String fileName, InputStream is, String iphost, String port) {
  getClientSocket(is, fileName, iphost, port);
 }
//建立socket通信
 public void getClientSocket(InputStream is, String fileName, String iphost, String port) {
  int po = Integer.parseInt(port);
  try {
   Socket socket = new Socket(iphost, po);
   BufferedReader br = new BufferedReader(
     new InputStreamReader(socket.getInputStream(), "UTF-8")
   );
   PrintStream pr = new PrintStream(
     socket.getOutputStream()
   );
   OutputStream os = socket.getOutputStream();
   System.out.println("客戶(hù)端給你傳文件了!");
   System.out.println("文件名為:" + fileName);
   //讀取服務(wù)器返回的消息
   String str = br.readLine();
   System.out.println("服務(wù)器發(fā)來(lái)的消息為:" + str);
   pr.println(fileName);
   in(is, os);
   pr.close();
   br.close();
   System.out.println("客戶(hù)端已關(guān)閉");
  } catch (Exception e) {
   e.printStackTrace();
  }
 }
 //上傳文本
 public static void in(InputStream is, OutputStream os) throws IOException {
  //BIO
  BufferedInputStream bi = new BufferedInputStream(is);
  BufferedOutputStream bo = new BufferedOutputStream(os);
  int len = 0;
  while ((len=bi.read())!=-1){
   bo.write(len);
   System.out.println(len);
  }
  bi.close();
  bo.close();
 }
}

這里相應(yīng)的訪問(wèn)路徑為:

服務(wù)端: http://localhost:8080/

客戶(hù)端: http://localhost:8082/upload

完整項(xiàng)目GIT地址:

注意: https://github.com/fengcharly/sockeio-springMvcUpload.git

傳輸過(guò)程中的我們用的是系統(tǒng)提供的BufferedInputStream和BufferedOutputStream緩沖流來(lái)傳輸文件,相對(duì)而言傳輸小文件比較合適,大文件比較慢,可以進(jìn)一步優(yōu)化,傳輸過(guò)程中傳輸速度如下:

總結(jié)

以上所述是小編給大家介紹的SocketIo+SpringMvc實(shí)現(xiàn)文件的上傳下載功能,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

相關(guān)文章

  • Java命令行參數(shù)解析工具jcommander詳解

    Java命令行參數(shù)解析工具jcommander詳解

    這篇文章主要為大家介紹了Java命令行參數(shù)解析工具jcommander命令詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-09-09
  • java?面向?qū)ο蟠a塊及不同位置對(duì)屬性賦值的執(zhí)行順序

    java?面向?qū)ο蟠a塊及不同位置對(duì)屬性賦值的執(zhí)行順序

    這篇文章主要介紹了java面向?qū)ο蟠a塊及不同位置對(duì)屬性賦值的執(zhí)行順序,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下
    2022-09-09
  • SSH 框架簡(jiǎn)介

    SSH 框架簡(jiǎn)介

    SSH是 struts+spring+hibernate的一個(gè)集成框架,是目前較流行的一種web應(yīng)用程序開(kāi)源框架。本文給大家詳細(xì)看一下組成SSH的這三個(gè)框架
    2017-09-09
  • SpringBoot開(kāi)發(fā)案例之配置Druid數(shù)據(jù)庫(kù)連接池的示例

    SpringBoot開(kāi)發(fā)案例之配置Druid數(shù)據(jù)庫(kù)連接池的示例

    本篇文章主要介紹了SpringBoot開(kāi)發(fā)案例之配置Druid數(shù)據(jù)庫(kù)連接池的示例,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-03-03
  • intelliJ idea 2023 配置Tomcat 8圖文教程

    intelliJ idea 2023 配置Tomcat 8圖文教程

    這篇文章主要介紹了intelliJ idea 2023 配置Tomcat 8教程,本文通過(guò)圖文并茂的形式給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-06-06
  • Spring?Data?Jpa?復(fù)雜查詢(xún)方式總結(jié)(多表關(guān)聯(lián)及自定義分頁(yè))

    Spring?Data?Jpa?復(fù)雜查詢(xún)方式總結(jié)(多表關(guān)聯(lián)及自定義分頁(yè))

    這篇文章主要介紹了Spring?Data?Jpa?復(fù)雜查詢(xún)方式總結(jié)(多表關(guān)聯(lián)及自定義分頁(yè)),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-02-02
  • SpringBoot集成echarts實(shí)現(xiàn)k線圖功能

    SpringBoot集成echarts實(shí)現(xiàn)k線圖功能

    ECharts是一款基于JavaScript的數(shù)據(jù)可視化圖表庫(kù),提供直觀,生動(dòng),可交互,可個(gè)性化定制的數(shù)據(jù)可視化圖表,本文給大家介紹了SpringBoot集成echarts實(shí)現(xiàn)k線圖功能,文中有詳細(xì)的代碼示例供大家參考,需要的朋友可以參考下
    2024-07-07
  • 詳解Java?POI?excel自定義設(shè)置單元格格式

    詳解Java?POI?excel自定義設(shè)置單元格格式

    這篇文章主要介紹了Java?POI?excel設(shè)置單元格格式,自定義設(shè)置,設(shè)置單元格格式:來(lái)源_formats,更多數(shù)據(jù)類(lèi)型從formats里面發(fā)現(xiàn),需要的朋友可以參考下
    2024-01-01
  • 你知道Java中的注解可以繼承嗎?

    你知道Java中的注解可以繼承嗎?

    注解想必大家都用過(guò),也叫元數(shù)據(jù),是一種代碼級(jí)別的注釋?zhuān)梢詫?duì)類(lèi)或者方法等元素做標(biāo)記說(shuō)明。那么今天我想問(wèn)大家的是類(lèi)被繼承了,注解能否繼承呢?可能會(huì)和大家想的不一樣,感興趣的可以往下看
    2022-12-12
  • Spring Cloud Gateway調(diào)用Feign異步問(wèn)題記錄

    Spring Cloud Gateway調(diào)用Feign異步問(wèn)題記錄

    這篇文章主要介紹了Spring Cloud Gateway調(diào)用Feign異步問(wèn)題記錄,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2023-04-04

最新評(píng)論

平遥县| 叶城县| 孝义市| 桐城市| 镇平县| 文安县| 上栗县| 北海市| 台山市| 聂荣县| 乐亭县| 西青区| 景东| 政和县| 曲水县| 车险| 镇赉县| 东光县| 富川| 泽州县| 扎兰屯市| 诸城市| 盐源县| 光山县| 乌拉特后旗| 清水河县| 霍州市| 蒲江县| 双城市| 绥阳县| 江山市| 石河子市| 宜州市| 鲁山县| 馆陶县| 即墨市| 石嘴山市| 宜州市| 兴安盟| 沂南县| 安远县|