SocketIo+SpringMvc实现文件的上传下载
SocketIo+SpringMvc实现文件的上传下载
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(); } }
这里相应的访问路径为:
客户端:http://localhost:8082/upload
完整项目GIT地址:
注意: https://github.com/fengcharly/sockeio-springMvcUpload.git
传输过程中的我们用的是系统提供的BufferedInputStream和BufferedOutputStream缓冲流来传输文件,相对而言传输小文件比较合适,大文件比较慢,可以进一步优化,传输过程中传输速度如下:

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
TypeScript基础入门 - 类 - 简介
转载地址 TypeScript基础入门 - 类 - 简介 项目实践仓库 https://github.com/durban89/typescript_demo.git tag: 1.0.14 为了保证后面的学习演示需要安装下ts-node,这样后面的每个操作都能直接运行看到输出的结果。 npm install -D ts-node 后面自己在练习的时候可以这样使用 npx ts-node 脚本路径 类 介绍 传统的JavaScript程序使用函数和基于原型的继承来创建可重用的组件,但对于熟悉使用面向对象方式的程序员来讲就有些棘手,因为他们用的是基于类的继承并且对象是由类构建出来的。 从ECMAScript 2015,也就是ECMAScript 6开始,JavaScript程序员将能够使用基于类的面向对象的方式。 使用TypeScript,我们允许开发者现在就使用这些特性,并且编译后的JavaScript可以在所有主流浏览器和平台上运行,而不需要等到下个JavaScript版本。 类的使用 下面看一个使用类的例子: class Greeter { greeting: string; ...
- 下一篇
WPF中的文字修饰——上划线,中划线,基线与下划线
原文:WPF中的文字修饰——上划线,中划线,基线与下划线 我们知道,文字的修饰包括:空心字、立体字、划线字、阴影字、加粗、倾斜等。这里只说划线字的修饰方式,按划线的位置,我们可将之分为:上划线、中划线、基线与下划线。如图: 从上至下,分别为上划线(Overline),中划线(StrikeThrough),基线(Baseline)和下划线(Underline)。 如何实现? (1)XAML代码:<Page xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" ><TextBlock TextDecorations="Strikethrough" FontSize="72" FontFamily="Arial">A</TextBlock></Page>这里TextDecorations属性可以设置为:OverLine, Strikethrough, Bas...
相关文章
文章评论
共有0条评论来说两句吧...