溫馨提示×

溫馨提示×

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

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

SocketIo+SpringMvc實現文件的上傳下載功能

發布時間:2020-09-10 02:14:50 來源:腳本之家 閱讀:231 作者:charlyFeng 欄目:編程語言

socketIo不僅可以用來做聊天工具,也可以實現局域網(當然你如果有外網也可用外網)內實現文件的上傳和下載,下面是代碼的效果演示:

SocketIo+SpringMvc實現文件的上傳下載功能

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

部分代碼如下:

服務端的代碼:

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("我是服務端");
      String str = br.readLine();
      System.out.println("br.readLine():" + str);
      System.out.println("服務端來接收了!!");
      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);
   //讀取服務器返回的消息
   String str = br.readLine();
   System.out.println("服務器發來的消息為:" + str);
   pr.println(fileName);
   in(is, os);
   pr.close();
   br.close();
   System.out.println("客戶端已關閉");
  } 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();
 }
}

這里相應的訪問路徑為:

服務端: http://localhost:8080/

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

完整項目GIT地址:

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

傳輸過程中的我們用的是系統提供的BufferedInputStream和BufferedOutputStream緩沖流來傳輸文件,相對而言傳輸小文件比較合適,大文件比較慢,可以進一步優化,傳輸過程中傳輸速度如下:

SocketIo+SpringMvc實現文件的上傳下載功能

總結

以上所述是小編給大家介紹的SocketIo+SpringMvc實現文件的上傳下載功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對億速云網站的支持!

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

亚洲午夜精品一区二区_中文无码日韩欧免_久久香蕉精品视频_欧美主播一区二区三区美女