Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/main/java/ex03/pyrmont/connector/http/HttpResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,12 @@ public void sendStaticResource() throws IOException {
/* request.getUri has been replaced by request.getRequestURI */
File file = new File(Constants.WEB_ROOT, request.getRequestURI());
fis = new FileInputStream(file);
PrintWriter printWriter = getWriter();
// 补充http响应头
printWriter.println("HTTP/1.1 200 OK");
printWriter.println("Content-Type:text/html;charset=utf-8");
printWriter.println("Content-length: " + file.length());
printWriter.println();
/*
HTTP Response = Status-Line
*(( general-header | response-header | entity-header ) CRLF)
Expand All @@ -320,6 +326,7 @@ public void sendStaticResource() throws IOException {
output.write(bytes, 0, ch);
ch = fis.read(bytes, 0, BUFFER_SIZE);
}
printWriter.close();
} catch (FileNotFoundException e) {
String errorMessage = "HTTP/1.1 404 File Not Found\r\n" +
"Content-Type: text/html\r\n" +
Expand Down Expand Up @@ -468,7 +475,7 @@ public ServletOutputStream getOutputStream() throws IOException {

public PrintWriter getWriter() throws IOException {
ResponseStream newStream = new ResponseStream(this);
newStream.setCommit(false);
newStream.setCommit(true);
OutputStreamWriter osr =
new OutputStreamWriter(newStream, getCharacterEncoding());
writer = new ResponseWriter(osr);
Expand Down