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