我正在使用套接字在Java中开发一个简单的HTTP Web服务器.我目前正在开发一项功能,该功能允许我自动呈现静态文件.一切都很好,我可以渲染CSS和JavaScript文件,但由于某种原因,我无法渲染任何图像文件.
打印响应头确实会返回图像字节.当我在调试时查看网络选项卡时,它显示映像路由返回了大约381kB的数据,而映像本身是151kB,所以我假设有什么东西正在返回.
当我转到图像路径时,它显示"图像(目录)无法显示,因为它包含错误."
MochaResponse.java个
package com.gabrielgavrilov.mocha;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
public class MochaResponse
{
public StringBuilder header = new StringBuilder();
MochaResponse(String status, String contentType)
{
this.header.append("HTTP/1.0 " + status + "\r\n");
this.header.append("Content-Type: " + contentType + "\r\n");
appendEmpty();
}
public void addHeader(String header, String value)
{
this.header.append(header + ": " + value + "\r\n");
}
public void setCookie(String key, String value)
{
addHeader("Set-Cookie", key+"="+value);
}
public void send(String data)
{
this.header.append(data);
appendEmpty();
}
public void render(String fileName)
{
render(fileName, Mocha.VIEWS_DIRECTORY);
}
public void render(String fileName, String directory)
{
try
{
String fileContent = Files.readString(Paths.get(directory + fileName));
send(fileContent);
}
catch (IOException e)
{
throw new RuntimeException(e);
}
}
protected void renderImage(String file)
{
try
{
FileInputStream content = new FileInputStream(Mocha.STATIC_DIRECTORY + file);
int i = 0;
while((i = content.read()) != -1)
{
this.header.append(i);
}
System.out.println(this.header);
appendEmpty();
content.close();
}
catch(IOException e)
{
e.printStackTrace();
}
}
private void appendEmpty()
{
this.header.append("\r\n");
}
}
MochaClient.java个
package com.gabrielgavrilov.mocha;
import java.io.*;
import java.nio.Buffer;
import java.util.HashMap;
import java.util.Map;
import java.util.function.BiConsumer;
public class MochaClient {
MochaClient(InputStream clientInput, OutputStream clientOutput)
{
try
{
InputStreamReader sr = new InputStreamReader(clientInput);
BufferedReader br = new BufferedReader(sr);
StringBuilder clientHeader = new StringBuilder();
String line;
while((line = br.readLine()).length() != 0)
{
clientHeader.append(line + "\r\n");
}
String route = getRequestedRoute(clientHeader.toString());
String method = getRequestedMethod(clientHeader.toString());
handleRequest(clientHeader.toString(), route, method, clientOutput, br);
}
catch (IOException e)
{
throw new RuntimeException(e);
}
}
private void handleRequest(String header, String route, String method, OutputStream clientOutput, BufferedReader br) throws IOException
{
String type = checkForStaticRoute(route);
if(type != null)
{
handleStaticRoute(route, type, clientOutput);
return;
}
switch(method)
{
case "GET":
handleGetRequest(header, route, clientOutput);
break;
case "POST":
handlePostRequest(header, route, clientOutput, br);
break;
}
}
private String checkForStaticRoute(String route) throws IOException
{
if(route.contains("."))
{
String[] routeSplit = route.split("\\.");
return routeSplit[routeSplit.length-1];
}
return null;
}
private void handleStaticRoute(String route, String type, OutputStream clientOutput) throws IOException
{
switch(type)
{
case "css":
renderStaticFile("text/css", route, clientOutput);
break;
case "js":
renderStaticFile("text/javascript", route, clientOutput);
break;
case "png":
renderStaticImage("image/png", route, clientOutput);
break;
case "jpeg":
renderStaticImage("image/jpeg", route, clientOutput);
break;
}
}
private void renderStaticFile(String contentType, String route, OutputStream clientOutput) throws IOException
{
String file = route.substring(1);
MochaResponse response = new MochaResponse("200 OK", contentType);
response.render(file, Mocha.STATIC_DIRECTORY);
clientOutput.write(response.header.toString().getBytes());
clientOutput.flush();
}
private void renderStaticImage(String contentType, String route, OutputStream clientOutput) throws IOException
{
String file = route.substring(1);
MochaResponse response = new MochaResponse("200 OK", contentType);
response.renderImage(file);
clientOutput.write(response.header.toString().getBytes());
clientOutput.flush();
}
...
如果有人能帮我指引正确的方向,我将不胜感激. 谢谢!