Java编程利用socket多线程访问服务器文件代码示例
作者:ysk_xh_521 时间:2023-10-11 15:36:37
这篇文章将向大家展示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
标签:java,socket,多线程
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
一起来学习C#的观察者模式
2022-04-02 13:43:15
![](https://img.aspxhome.com/file/2023/5/78115_0s.png)
Android控件之Spinner用法实例分析
2022-08-06 08:36:33
![](https://img.aspxhome.com/file/2023/0/126690_0s.png)
Android异步消息机制详解
2023-08-07 09:42:52
![](https://img.aspxhome.com/file/2023/0/85960_0s.png)
简单仿写Android控件SlidingMenu的实例代码
2022-01-23 05:11:29
![](https://img.aspxhome.com/file/2023/3/139073_0s.gif)
C#运算符之与,或,异或及移位运算小结
2023-07-19 10:35:50
一起聊聊Java中13种锁的实现方式
2022-11-23 15:26:00
![](https://img.aspxhome.com/file/2023/1/103981_0s.jpg)
详解java关于对象的比较
2022-12-11 01:28:55
![](https://img.aspxhome.com/file/2023/3/85833_0s.png)
Java Lambda表达式入门示例
2022-03-19 00:15:47
关于@ApiImplicitParams、ApiImplicitParam的使用说明
2023-11-09 10:49:34
![](https://img.aspxhome.com/file/2023/0/59220_0s.jpg)
c#项目将dll打包到exe中的步骤
2021-06-21 14:44:25
Android Rxjava3 使用场景详解
2023-08-06 08:58:50
![](https://img.aspxhome.com/file/2023/3/84723_0s.jpg)
monkeyrunner之电脑安装驱动(5)
2022-02-28 19:17:00
![](https://img.aspxhome.com/file/2023/4/87334_0s.png)
Android中Retrofit 2.0直接使用JSON进行数据交互
2022-01-08 07:22:46
![](https://img.aspxhome.com/file/2023/4/138934_0s.png)
java程序员必须要学会的linux命令总结(推荐)
2021-12-11 00:47:47
idea的使用之关于tomcat热部署的教程
2022-12-02 20:16:46
![](https://img.aspxhome.com/file/2023/3/129953_0s.png)
C# 使用Fluent API 创建自己的DSL(推荐)
2022-03-30 23:36:48
springboot全局异常处理代码实例
2023-02-05 20:41:36
C++ 超详细讲解stack与queue的使用
2021-08-28 02:23:32
![](https://img.aspxhome.com/file/2023/5/122585_0s.png)
SpringBoot JS-SDK自定义微信分享的实现
2023-11-10 05:08:28
![](https://img.aspxhome.com/file/2023/6/59256_0s.jpg)
C#把UNICODE编码转换为GB编码的实例
2023-03-11 09:28:25