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();
 }
}

当前题目:SocketIo+SpringMvc实现文件的上传下载功能-创新互联
URL网址:http://azwzsj.com/article/dphesj.html