软件编程
位置:首页>> 软件编程>> java编程>> Java编程利用socket多线程访问服务器文件代码示例

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

作者:ysk_xh_521  发布时间:2023-10-11 15:36:37 

标签:java,socket,多线程

这篇文章将向大家展示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);
     }
   }
 }
}

来源:http://blog.csdn.net/ysk_xh_521/article/details/77460856

0
投稿

猜你喜欢

手机版 软件编程 asp之家 www.aspxhome.com