将日志写入文件;升级依赖;将项目JDK设置为21;修改部分日志记录方式

This commit is contained in:
chuzhongzai 2024-06-08 03:11:11 +08:00
parent dd0b7e608c
commit 95329a5603
6 changed files with 50 additions and 40 deletions

View File

@ -7,13 +7,12 @@
<option value="$PROJECT_DIR$/pom.xml" />
</list>
</option>
<option name="workspaceImportForciblyTurnedOn" value="true" />
</component>
<component name="PWA">
<option name="enabled" value="true" />
<option name="wasEnabledAtLeastOnce" value="true" />
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_21" default="true" project-jdk-name="17" project-jdk-type="JavaSDK">
<component name="ProjectRootManager" version="2" languageLevel="JDK_21" default="true" project-jdk-name="21" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

View File

@ -50,13 +50,13 @@
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.8.20</version>
<version>5.8.26</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-compress</artifactId>
<version>1.21</version>
<version>1.25.0</version>
</dependency>
</dependencies>
@ -96,6 +96,8 @@
<arg>-H:+ReportExceptionStackTraces</arg>
<arg>--gc=G1</arg>
<arg>--enable-url-protocols=https</arg>
<arg>-H:IncludeResources="simplelogger.properties"</arg>
<arg>--initialize-at-build-time=org.slf4j.simple.SimpleLogger,org.slf4j.simple.SimpleLoggerFactory</arg>
<arg>-H:ReflectionConfigurationFiles=src/main/resources/reflect-config.json</arg>
</buildArgs>
<metadataRepository>

View File

@ -1,5 +1,7 @@
package lion;
import lombok.extern.slf4j.Slf4j;
import java.io.*;
import java.net.*;
import java.nio.charset.StandardCharsets;
@ -8,7 +10,7 @@ import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
@Slf4j
public class MultiThreadedHTTPServer {
private static final int PORT = 8888;
private static final int BUFFER_SIZE = 1024;
@ -22,26 +24,27 @@ public class MultiThreadedHTTPServer {
real_ip = "207.60.50.74";
}
try(ServerSocket serverSocket = new ServerSocket(PORT)) {
System.out.println("Server listening on port " + PORT);
log.info("Server listening on port {}", PORT);
while (true) {
Socket clientSocket = serverSocket.accept();
String ip = clientSocket.getInetAddress().getHostAddress();
if(ip.equals(real_ip)){
System.out.println("Client connected");
log.info("Client connected");
// 线程池处理下载请求
threadPool.submit(() -> handleClientRequest(clientSocket));
}else{
System.out.println("unknown ip: " + ip);
log.info("unknown ip: " + ip);
clientSocket.close();
}
}
} catch (IOException e) {
e.printStackTrace();
log.error("处理http请求时出错,IP:{},ERROR:{}", real_ip, e.getMessage());
}
}
private static void handleClientRequest(Socket clientSocket) {
String fileName = "";
try {
BufferedReader requestReader = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
String requestLine = requestReader.readLine();
@ -50,7 +53,7 @@ public class MultiThreadedHTTPServer {
String[] requestParts = requestLine.split(" ");
String method = requestParts[0];
Map<String, String> paramMap = parseRequestLine(requestParts[1]);//path
System.out.println(Arrays.toString(requestParts));
log.info(Arrays.toString(requestParts));
// Only handle GET requests
if (method.equals("GET")) {
@ -97,7 +100,8 @@ public class MultiThreadedHTTPServer {
sendErrorResponse(clientSocket, "403 Forbidden");
return;
}
System.out.println(file.getAbsolutePath());
fileName = file.getName();
log.info(file.getAbsolutePath());
// Check if the file exists and is readable
if (file.exists() && file.isFile() && file.canRead()) {
// Get the file length
@ -154,7 +158,7 @@ public class MultiThreadedHTTPServer {
requestReader.close();
clientSocket.close();
} catch (IOException e) {
e.printStackTrace();
log.error("处理文件下载时出错,IP:{}, 文件:{}, ERROR:{}", clientSocket.getInetAddress().getHostAddress(), fileName, e.getMessage());
}
}

View File

@ -116,22 +116,27 @@ public class DownloadCheckService {
ArrayList<GalleryTask> galleryTasks = new ArrayList<>(compress_queue);
compress_queue.clear();
reentrantLock.unlock();
for (GalleryTask galleryTask : galleryTasks) {
//创建文件夹
File file = new File(storagePath + galleryTask.getName());
if (file.isDirectory() || file.mkdirs()) {
log.info(galleryTask.getName() + "文件夹创建成功");
} else {
log.error(galleryTask.getName() + "文件夹创建失败");
continue;
for (GalleryTask galleryTask : galleryTasks) {
try {
log.info("开始压缩:{}", galleryTask.getName());
//创建文件夹
File file = new File(storagePath + galleryTask.getName());
if (file.isDirectory() || file.mkdirs()) {
log.info("{}文件夹创建成功", galleryTask.getName());
} else {
log.error("{}文件夹创建失败", galleryTask.getName());
continue;
}
//生成压缩包
ZipUtil.zip(galleryTask.getPath(), storagePath + galleryTask.getName() + "/" + galleryTask.getName() + ".zip");
log.info("{}压缩完成", galleryTask.getName());
FileUtil.del(galleryTask.getPath());
galleryTask.setStatus(GalleryTask.COMPRESS_COMPLETE);
} catch (Exception e){
log.error("{}压缩失败:{}", galleryTask, e.getMessage());
}
}
//生成压缩包
ZipUtil.zip(galleryTask.getPath(), storagePath + galleryTask.getName() + "/" + galleryTask.getName() + ".zip");
log.info(galleryTask.getName() + "压缩完成" );
FileUtil.del(galleryTask.getPath());
galleryTask.setStatus(GalleryTask.COMPRESS_COMPLETE);
}
}
}

View File

@ -15,8 +15,6 @@ import io.netty.handler.codec.LengthFieldBasedFrameDecoder;
import lombok.extern.slf4j.Slf4j;
import java.io.FileOutputStream;
import java.io.OutputStream;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.ArrayList;
@ -115,14 +113,7 @@ public class storageNode {
queue.removeIf(GalleryTask::is_compress_complete);
lock.unlock();
}catch (Exception e){
e.printStackTrace();
try (OutputStream outputStream = new FileOutputStream("/root/gallery/storageNode/err.txt")){
outputStream.write(e.getMessage().getBytes());
channelFuture.channel().close().sync();
System.exit(-1);
}catch (Exception ex){
ex.printStackTrace();
}
log.error("发送任务状态时发生异常:{}", e.getMessage());
}
}
@ -135,7 +126,7 @@ public class storageNode {
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
System.out.println(msg);
log.info(String.valueOf(msg));
AbstractMessage abstractMessage = (AbstractMessage) msg;
switch (abstractMessage.messageType){
@ -154,7 +145,7 @@ public class storageNode {
DownloadPostMessage dpm = (DownloadPostMessage) abstractMessage;
lock.lock();
queue.add(dpm.getGalleryTask());
System.out.println(queue);
log.info(String.valueOf(queue));
lock.unlock();
ctx.writeAndFlush(new ResponseMessage(dpm.messageId, (byte) 0));
}

View File

@ -0,0 +1,9 @@
org.slf4j.simpleLogger.defaultLogLevel=info
org.slf4j.simpleLogger.showDateTime=true
org.slf4j.simpleLogger.dateTimeFormat=yyyy-MM-dd HH:mm:ss
org.slf4j.simpleLogger.logFile=run.out