socketIo不仅可以用来做聊天工具,也可以实现局域网(当然你如果有外网也可用外网)内实现文件的上传和下载、下面是代码的效果演示:
GIT地址:https://github.com/fengcharly/sockeio-springMvcUpload.git
部分代码如下:
服务端的代码:
ChuanServer:
进口. io . *; 进口java.net.ServerSocket; 进口java.net.Socket; 进口java.nio.channels.FileChannel; 公开课ChuanServer { 公共静态孔隙protServer (String po)抛出IOException { int port=Integer.parseInt (po); ServerSocket ServerSocket=考察(港口); 而(真){ 最后一个套接字clientSocket=serverSocket.accept (); 新线程(){ @Override 公共空间run () { 尝试{ BufferedReader br=new BufferedReader ( 新InputStreamReader (clientSocket.getInputStream (),“GBK”) ); InputStream是=clientSocket.getInputStream (); PrintStream公关=new PrintStream ( clientSocket.getOutputStream () ); pr.println(“我是服务端”); 字符串str=br.readLine (); System.out.println (“br.readLine ():”+ str); System.out.println(“服务端来接收了! !”); (str); }捕捉(异常e) { e.printStackTrace (); } } } .start (); } } 公共静态孔隙(InputStream,字符串str)抛出IOException { FileOutputStream fo=new FileOutputStream (“C: \ \用户桌面\ \ \ \ \ \管理员上传\ \ " + str); BufferedInputStream bi=new BufferedInputStream(是); BufferedOutputStream bo=new BufferedOutputStream (fo); int len=0; 在((len=bi.read ()) !=1) { bo.write (len); } bi.close (); bo.close (); } }
这里我固定了上传后保存的路径为:“C: \ \用户管理员桌面\上传\ \ "
PortController:
进口org.springframework.stereotype.Controller; 进口org.springframework.ui.Model; 进口org.springframework.web.bind.annotation.RequestMapping; 进口socket.ChuanServer; 进口java.io.IOException; @ controller 公开课PortController { @RequestMapping(/端口) 港口公共字符串(字符串港口,模型模型){ model.addAttribute(“端口”,端口); 尝试{ ChuanServer.protServer(港口); }捕捉(IOException e) { e.printStackTrace (); } 返回“成功”; } }
再来看下上传的客户端的代码:
UpLoadController:
@ controller @RequestMapping (“/? 公开课UpLoadController { @ autowired 私人UpService UpService; 私人字符串转=" "; @RequestMapping("/上传”) 公共字符串上传(@RequestParam (value=" https://www.yisu.com/zixun/file ",要求=false) MultipartFile文件, HttpServletRequest请求,@RequestParam (“iphost”)字符串iphost @RequestParam(“港口”)字符串港口,模型模型)抛出IOException { 字符串文件名=file.getOriginalFilename (); InputStream是=file.getInputStream (); upService.upload(文件名,iphost、港口); 返回“成功”; } }
UpServiceImpl:
@ service 公共类UpServiceImpl实现UpService { @Override 公共空间上传(iphost字符串文件名、InputStream是字符串,字符串端口){ getClientSocket(文件名,iphost、港口); }//建立插座通信 公共空getClientSocket (iphost文件名InputStream,字符串,字符串,字符串端口){ int阿宝=Integer.parseInt(港口); 尝试{ 新的套接字(Socket套接字=iphost po); BufferedReader br=new BufferedReader ( 新InputStreamReader (socket.getInputStream (),“utf - 8”) ); PrintStream公关=new PrintStream ( socket.getOutputStream () ); OutputStream os=socket.getOutputStream (); System.out.println(“客户端给你传文件了!”); system . out。println(“文件名为:“+文件名);//读取服务器返回的消息 字符串str=br.readLine (); system . out。println(“服务器发来的消息为:" + str); pr.println(文件名); 在(操作系统); pr.close (); br.close (); System.out.println(“客户端已关闭”); }捕捉(异常e) { e.printStackTrace (); } }//上传文本 公共静态孔隙(InputStream是OutputStream os)抛出IOException {//生物 BufferedInputStream bi=new BufferedInputStream(是); BufferedOutputStream bo=new BufferedOutputStream (os); int len=0; 在((len=bi.read ()) !=1) { bo.write (len); System.out.println (len); } bi.close (); bo.close (); } }SocketIo + SpringMvc实现文件的上传下载功能