SocketIo+SpringMvc实现文件的上传下载功能
程序员文章站
2024-02-23 21:56:52
socketio不仅可以用来做聊天工具,也可以实现局域网(当然你如果有外网也可用外网)内实现文件的上传和下载,下面是代码的效果演示:
git地址: https://g...
socketio不仅可以用来做聊天工具,也可以实现局域网(当然你如果有外网也可用外网)内实现文件的上传和下载,下面是代码的效果演示:
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(); } }
这里相应的访问路径为:
服务端:
客户端:
完整项目git地址:
注意: https://github.com/fengcharly/sockeio-springmvcupload.git
传输过程中的我们用的是系统提供的bufferedinputstream和bufferedoutputstream缓冲流来传输文件,相对而言传输小文件比较合适,大文件比较慢,可以进一步优化,传输过程中传输速度如下:
总结
以上所述是小编给大家介绍的socketio+springmvc实现文件的上传下载功能,希望对大家有所帮助