SocketIo+SpringMvc实现文件的上传下载功能

时间:2022-09-14 23:17:56

socketio不仅可以用来做聊天工具,也可以实现局域网(当然你如果有外网也可用外网)内实现文件的上传下载,下面是代码的效果演示:

SocketIo+SpringMvc实现文件的上传下载功能

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

部分代码如下:

服务端的代码:

chuanserver:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
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:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
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:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@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:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
@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实现文件的上传下载功能,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对服务器之家网站的支持!

原文链接:https://www.cnblogs.com/charlypage/p/9440226.html

相关文章