socketio不僅可以用來做聊天工具,也可以實現局域網(當然你如果有外網也可用外網)內實現文件的上傳和下載,下面是代碼的效果演示:
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實現文件的上傳下載功能,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對服務器之家網站的支持!
原文鏈接:https://www.cnblogs.com/charlypage/p/9440226.html