一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務(wù)器之家:專(zhuān)注于服務(wù)器技術(shù)及軟件下載分享
分類(lèi)導(dǎo)航

PHP教程|ASP.NET教程|Java教程|ASP教程|編程技術(shù)|正則表達(dá)式|C/C++|IOS|C#|Swift|Android|VB|R語(yǔ)言|JavaScript|易語(yǔ)言|vb.net|

服務(wù)器之家 - 編程語(yǔ)言 - Java教程 - SocketIo+SpringMvc實(shí)現(xiàn)文件的上傳下載功能

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

2021-05-24 13:33charlyFeng Java教程

這篇文章主要介紹了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)文件的上傳下載,下面是代碼的效果演示:

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

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

部分代碼如下:

服務(wù)端的代碼:

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("我是服務(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:

?
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";
 }
}

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

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("客戶(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ò)程中傳輸速度如下:

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

總結(jié)

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

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

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 狠狠色婷婷日日综合五月 | 火影忍者羞羞 | 亚洲天堂2013 | 国产资源在线视频 | 国产一级在线观看 | 四虎影视免费 | 吻戏辣妞范1000免费体验 | 40岁女人三级全黄 | 交换年轻夫妇HD中文字幕 | 国产品精人成福利视频 | 欧美高清片 | 亚洲精品国产精品精 | 色中文字幕 | 美女被草漫画 | 欧美日韩国产精品自在自线 | 精品操| 性一交一无一伦一精一品 | 2022色婷婷综合久久久 | 男女福利视频 | 无限在线看免费视频大全 | 双性小说肉 | 国产日韩精品一区二区三区 | 久久综合久综合久久鬼色 | 日本漫画工囗全彩番在线 | 刺客女仆| 精品国产视频 | 啪啪无尽3d动漫漫画免费网站 | yellow视频在线观看 | oneday高清在线观看 | 欧美成人另类人妖 | 楚乔传第二部免费观看全集完整版 | 性xxx免费视频 | 免费观看二十女人一摸是水 | 91成人啪国产啪永久地址 | 国产成人精品免费大全 | 色戒真做gif动图 | 369手机看片 | 91久久偷偷做嫩草影院免费 | 免费观看视频在线 | 日本96在线精品视频免费观看 | 肉肉小说在线阅读 |