欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

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

socketIo不僅可以用來做聊天工具,也可以實(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ù)端來接收了!!");
      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";
 }
}

再來看下上傳的客戶端的代碼:

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("客戶端給你傳文件了!");
   System.out.println("文件名為:" + fileName);
   //讀取服務(wù)器返回的消息
   String str = br.readLine();
   System.out.println("服務(wù)器發(fā)來的消息為:" + str);
   pr.println(fileName);
   in(is, os);
   pr.close();
   br.close();
   System.out.println("客戶端已關(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ù)端: http://localhost:8080/

客戶端: http://localhost:8082/upload

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

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

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

總結(jié)

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

相關(guān)文章

  • Kotlin Coroutines執(zhí)行異步加載示例詳解

    Kotlin Coroutines執(zhí)行異步加載示例詳解

    這篇文章主要給大家介紹了關(guān)于Kotlin Coroutines執(zhí)行異步加載的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2018-01-01
  • Java過濾器如何解決存儲(chǔ)型xss攻擊問題

    Java過濾器如何解決存儲(chǔ)型xss攻擊問題

    這篇文章主要介紹了Java過濾器如何解決存儲(chǔ)型xss攻擊問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-05-05
  • Spring Cloud重試機(jī)制與各組件的重試總結(jié)

    Spring Cloud重試機(jī)制與各組件的重試總結(jié)

    這篇文章主要給大家介紹了關(guān)于Spring Cloud中重試機(jī)制與各組件的重試的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。
    2017-11-11
  • Java this super代碼實(shí)例及使用方法總結(jié)

    Java this super代碼實(shí)例及使用方法總結(jié)

    這篇文章主要介紹了Java this super代碼實(shí)例及使用方法總結(jié),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-03-03
  • java 與web服務(wù)器鏈接的實(shí)例

    java 與web服務(wù)器鏈接的實(shí)例

    這篇文章主要介紹了java 與web服務(wù)器鏈接的實(shí)例的相關(guān)資料,使用net.Socket類sock.getInetAddress()方法獲得與Web服務(wù)器連接,需要的朋友可以參考下
    2017-07-07
  • XML解析四種方式代碼示例詳解

    XML解析四種方式代碼示例詳解

    這篇文章主要介紹了XML解析四種方式代碼示例詳解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-12-12
  • SpringBoot項(xiàng)目的配置文件中設(shè)置server.port不生效問題

    SpringBoot項(xiàng)目的配置文件中設(shè)置server.port不生效問題

    這篇文章主要介紹了SpringBoot項(xiàng)目的配置文件中設(shè)置server.port不生效問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2022-11-11
  • SpringBoot整合mybatis-plus快速入門超詳細(xì)教程

    SpringBoot整合mybatis-plus快速入門超詳細(xì)教程

    mybatis-plus 是一個(gè) Mybatis 的增強(qiáng)工具,在 Mybatis 的基礎(chǔ)上只做增強(qiáng)不做改變,為簡化開發(fā)、提高效率而生,本文給大家分享SpringBoot整合mybatis-plus快速入門超詳細(xì)教程,一起看看吧
    2021-09-09
  • JAVA 繼承基本類、抽象類、接口介紹

    JAVA 繼承基本類、抽象類、接口介紹

    Java是一個(gè)面向?qū)ο蟮恼Z言,java面向?qū)ο笠话阌腥筇卣鳎悍庋b、繼承、多態(tài)
    2013-01-01
  • Java對(duì)象轉(zhuǎn)JSON三種常用的方法

    Java對(duì)象轉(zhuǎn)JSON三種常用的方法

    在Java中可以使用多種方式將對(duì)象轉(zhuǎn)換為JSON字符串,下面這篇文章主要給大家介紹了關(guān)于Java對(duì)象轉(zhuǎn)JSON三種常用的方法,文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2024-04-04

最新評(píng)論