当前位置: 移动技术网 > IT编程>开发语言>Java > Java编程利用socket多线程访问服务器文件代码示例

Java编程利用socket多线程访问服务器文件代码示例

2019年07月19日  | 移动技术网IT编程  | 我要评论

这篇文章将向大家展示java编程利用socket多线程访问服务器文件代码示例,如果您想先了解java多线程socket编程的基础知识,可以看下这篇文章:java多线程编程实现socket通信示例代码

接下来进入正文,我们看看利用socket多线程访问服务器代码:

servermain.java

package com.ysk.webserver;
import java.io.file;
import java.io.ioexception;
import java.net.serversocket;
import java.net.socket;
public class servermain {
  static boolean start = true;
  public static void main(string[] args) {
    // 1.声明所用的对象
    // serversocket
    // socket
    // bufferedreader
    // printstream 因为这个流是用来按照http相应规则返回数据给浏览器的,
    // http响应规则中可能既要写字符又要写字节 所以使用这个流
    try {
      // 2 赋值,为try catch语句块外面的变量赋值
      serversocket serversocket = new serversocket(7878);
      while (true) {
        while (start) {
          system.out.println("服务端已启动,等待客户端连接。。");
          socket socket = serversocket.accept();
          system.out.println("客户端已连接");
          thread thread = new serverthread(socket);
          thread.start();
        }
        // 3 处理请求,即从socket中拿出浏览器按照http协议封装好的请求(字符串)
        // 关心请求行
        // 按照空格拆分字符串,拆出来的第一部分是请求方式
        // 拆出来的第二部分是资源路径
        // 4 处理响应
        // 如果 请求方式 是get即代表没有请求体
        // 从请求行中寻找到要访问的文件
        // 从本地目录下查找(不是遍历整个文件系统
        // 代表着我们要定义一个目录位置,此位置为数据仓库,
        // 专门来存放客户端可能会访问的数据
        // 咱们暂定这个目录为“项目/files”)
        // 看看是否有此文件,对于/ 资源特殊处理,代表
        // 如果有文件,利用输出流,把数据拼成http响应格式的数据,
        // 返回给客户端(数据找到了,响应码200)
        // 如果没有文件,返还error.html文件(代表比较友好的提示方式),
        // 也得按照http响应格式返还error.html
      }
      // 如果是post方式,暂不处理
    } catch (exception e) {
    }
  }
  // 关闭资源
  // 什么时候关服务器,什么时候关客户端
}

serverthread.java

package com.ysk.webserver;
import java.io.bufferedreader;
import java.io.file;
import java.io.fileinputstream;
import java.io.ioexception;
import java.io.inputstreamreader;
import java.io.printstream;
import java.net.socket;
public class serverthread extends thread {
  // 和本线程相关的socket
  socket socket = null;
  bufferedreader in = null;
  bufferedreader inerror = null;
  printstream out = null;
  static boolean result = false;
  static string filepath = "";
  public serverthread(socket socket) {
    this.socket = socket;
  }
  @override
  public void run() {
    try {
      in = new bufferedreader(new inputstreamreader(socket.getinputstream()));
      string temp = in.readline();
      if (temp != null) {
        servermain.start = false;
        string method = temp.split(" ")[0];
        system.out.println(method);
        string filename = temp.split(" ")[1].replaceall("/", "");
        system.out.println(filename);
        string path = "files";
        findfile(path, filename);
        system.out.println("result:" + result);
        if (result) {
          // 找到文件,以字节方式去读
          string resource = filepath + filename;
          getresource(resource);
        } else {
          // 返回error.html
          string resource = "files\\error.html";
          getresource(resource);
        }
      }
    } catch (exception e) {
    } finally {
      try {
        if (out != null)
          out.close();
        if (inerror != null)
          inerror.close();
        if (in != null)
          in.close();
        if (socket != null)
          socket.close();
      } catch (ioexception e) {
        e.printstacktrace();
      }
    }
  }
//通过流去读文件
  private void getresource(string resource) {
    try {
      fileinputstream fileinputstream = new fileinputstream(resource);
      out = new printstream(socket.getoutputstream(), true);
      out.println("http/1.1 200 ok");
      out.println();
      int inttemp;
      while ((inttemp = fileinputstream.read()) != -1) {
        out.write(inttemp);
      }
      out.flush();
      fileinputstream.close();
      servermain.start = true;
      result = false;
    } catch (exception e) {
    }
  }
  // 查找文件
  private static void findfile(string path, string filename) throws ioexception {
    file file = new file(path);
    file[] templist = file.listfiles();
    system.out.println("该目录下对象个数:" + templist.length);
    for (int i = 0; i < templist.length; i++) {
      if (templist[i].isfile() && filename.equals(templist[i].getname())) {
        system.out.println("已找到该文件:" + filename);
        filepath = path + "\\";
        result = true;
      } else if (templist[i].isdirectory()) {
        // 读取某个文件夹下的所有文件夹
        system.out.println("读取某个文件夹下的所有文件夹");
        findfile(templist[i].getparent() + "\\" + templist[i].getname(), filename);
      }
    }
  }
}

总结

以上就是本文关于java编程利用socket多线程访问服务器文件代码示例的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站:java网络编程基础篇之单向通信java多线程编程安全退出线程方法介绍等,有什么问题,欢迎大家留言交流讨论,感谢朋友们对移动技术网网站的支持!

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网