Java編程利用socket多線程訪問服務器文件代碼示例
這篇文章將向大家展示Java編程利用socket多線程訪問服務器文件代碼示例,如果您想先了解Java多線程socket編程的基礎知識,可以看下這篇文章:Java多線程編程實現(xiàn)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相應規(guī)則返回數(shù)據(jù)給瀏覽器的,
// http響應規(guī)則中可能既要寫字符又要寫字節(jié) 所以使用這個流
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協(xié)議封裝好的請求(字符串)
// 關心請求行
// 按照空格拆分字符串,拆出來的第一部分是請求方式
// 拆出來的第二部分是資源路徑
// 4 處理響應
// 如果 請求方式 是GET即代表沒有請求體
// 從請求行中尋找到要訪問的文件
// 從本地目錄下查找(不是遍歷整個文件系統(tǒng)
// 代表著我們要定義一個目錄位置,此位置為數(shù)據(jù)倉庫,
// 專門來存放客戶端可能會訪問的數(shù)據(jù)
// 咱們暫定這個目錄為“項目/files”)
// 看看是否有此文件,對于/ 資源特殊處理,代表
// 如果有文件,利用輸出流,把數(shù)據(jù)拼成http響應格式的數(shù)據(jù),
// 返回給客戶端(數(shù)據(jù)找到了,響應碼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) {
// 找到文件,以字節(jié)方式去讀
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("該目錄下對象個數(shù):" + 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多線程訪問服務器文件代碼示例的全部內(nèi)容,希望對大家有所幫助。感興趣的朋友可以繼續(xù)參閱本站:Java網(wǎng)絡編程基礎篇之單向通信、Java多線程編程安全退出線程方法介紹等,有什么問題,歡迎大家留言交流討論,感謝朋友們對腳本之家網(wǎng)站的支持!
相關文章
Spring?Boot配置文件的語法規(guī)則詳解(properties和yml)
這篇文章主要介紹了Spring?Boot配置文件的語法規(guī)則,主要介紹兩種配置文件的語法和格式,properties和yml,對于配置文件也有獨立的文件夾存放,主要用來存放一些需要經(jīng)過變動的數(shù)據(jù)(變量值),感興趣的朋友跟隨小編一起看看吧2024-07-07
SpringBoot使用Scheduling實現(xiàn)定時任務的示例代碼
Spring Boot提供了一種方便的方式來實現(xiàn)定時任務,即使用Spring的@Scheduled注解,通過在方法上添加@Scheduled注解,我們可以指定方法在何時執(zhí)行,本文我們就給大家介紹一下SpringBoot如何使用Scheduling實現(xiàn)定時任務,需要的朋友可以參考下2023-08-08
Java使用JavaMail API發(fā)送和接收郵件的代碼示例
JavaMail是Oracle甲骨文開發(fā)的Java郵件類API,支持多種郵件協(xié)議,這里我們就來看一下Java使用JavaMail API發(fā)送和接收郵件的代碼示例2016-06-06
Java編程通過匹配合并數(shù)據(jù)實例解析(數(shù)據(jù)預處理)
這篇文章主要介紹了Java編程通過匹配合并數(shù)據(jù)實例解析(數(shù)據(jù)預處理),分享了相關代碼示例,小編覺得還是挺不錯的,具有一定借鑒價值,需要的朋友可以參考下2018-01-01
MyBatis傳入List集合查詢數(shù)據(jù)問題
這篇文章主要介紹了MyBatis傳入List集合查詢數(shù)據(jù)問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-02-02

