我已经为课程制作了这段代码,但我只是不明白为什么当我单击链接时,它不会向我显示 .jpg 或 .mp4 文件。我一直在网上搜索,我尝试将图像从 CMYK 转换为 RGB,在文件末尾添加更多的 CRLF,我根本不明白为什么它会给我这个错误:
“图像无法显示,因为它包含错误。” 或“无法显示 mp4 文件,因为它已损坏”
这是我的“欢迎页面”
<html>
<head>
<title> Welcome to my server</title>
</head>
<body bgcolor=white>
<p> Click one of the following: </p>
<p> <a href="candyplease.jpg">Candy</a> </p>
<p> <a href="moto.mp4">Rossi vs Stoner</a> </p>
</body>
</html>
这是我的服务器。
import java.io.*;
import java.net.Socket;
import java.util.*;
import java.awt.*;
import javax.imageio.*;
public class HTTPRequest implements Runnable
{
public static String CRLF = "\r\n"; // returning carriage return (CR) and a line feed (LF)
Socket socket;
// constructor
public HTTPRequest(Socket socket) throws Exception
{
this.socket = socket;
}
// Implement the run() method of the Runnable interface.
// Within run(), we explicitly catch and handle exceptions with a try/catch statement.
public void run()
{
try
{
processRequest();
} catch (Exception e)
{
System.out.println(e);
}
}
private void processRequest() throws Exception
{
//create an input and an output stream
InputStream instream = socket.getInputStream();
DataOutputStream outStream = new DataOutputStream(socket.getOutputStream());
// create a buffer
BufferedReader buffRead = new BufferedReader(new InputStreamReader(instream));// reads the input data
// Get the request line of the HTTP request message.
String requestLine = buffRead.readLine();// get /path/file.html version of http
// Display the request line.
System.out.println();
System.out.println(requestLine);
// HERE WE NEED TO DEAL WITH THE REQUEST
// Extract the filename from the request line.
StringTokenizer tokens = new StringTokenizer(requestLine);
tokens.nextToken();
String fileName = tokens.nextToken();
//this is so that i don't have to write /front.html at the start
if(fileName.equals("/")){
fileName="/front.html";
}
// attach a "." so that file request is within the current directory.
fileName = "." + fileName;
// Open the requested file.
FileInputStream fis = null;
boolean fileExists = true;
try
{
fis = new FileInputStream(fileName);
} catch (FileNotFoundException e)
{
fileExists = false;
}
// Construct the response message.
String statusLine = null;
String contentTypeLine = null;
String entityBody = null;
if (fileExists)
{
statusLine = "HTTP/1.0 200 OK" + CRLF; // 200 success code
contentTypeLine = "Content-type: " + contentType(fileName) + CRLF;
}// content info
else
{
contentTypeLine = "Content-type: text/html" + CRLF;// content info
entityBody = "<HTML>" + "<HEAD><TITLE>Not Found</TITLE></HEAD>"
+ "<BODY>Not Found</BODY></HTML>";
statusLine = "HTTP/1.0 404 Not Found" + CRLF;// 404 not found...
}
// Send the status line.
outStream.writeBytes(statusLine);
// Send the content type line.
outStream.writeBytes(contentTypeLine);
// Send a blank line to indicate the end of the header lines.
outStream.writeBytes(CRLF);
// Send the entity body.
if (fileExists)
{
outStream.writeBytes(statusLine);// Send the status line.
outStream.writeBytes("\n"+contentTypeLine);// Send the content type line.
sendBytes(fis, outStream);
fis.close();
} else
{
outStream.writeBytes(statusLine);// Send the status line
outStream.writeBytes("\n"+contentTypeLine);// Send the content type line.
outStream.writeBytes(entityBody);// Send the an html error message info body.
}
System.out.println("*****");
System.out.println(fileName);// print out file request to console
System.out.println("*****");
// Get and display the header lines.
String headerLine = null;
while ((headerLine = buffRead.readLine()).length() != 0)
{
System.out.println(headerLine);
}
// Close streams and socket.
outStream.close();
buffRead.close();
socket.close();
}
// return the file types
private static String contentType(String fileName)
{
if (fileName.endsWith(".htm") || fileName.endsWith(".html"))
{
return "text/html";
}
if (fileName.endsWith(".jpg") || fileName.endsWith(".jpeg"))
{
return "image/jpeg";
}
if (fileName.endsWith(".gif"))
{
return "image/gif";
}
if(fileName.endsWith(".mp4"))
{
return "movie";
}
return "application/octet-stream";
}
// set up i/o streams
private static void sendBytes(FileInputStream fis, DataOutputStream outStream)
throws Exception
{
// Construct a 1K buffer to hold bytes on their way to the socket.
byte[] buffer = new byte[1024];
int bytes = 0;
// Copy requested file into the socket's output stream.
while ((bytes = fis.read(buffer)) != -1)// read() returns minus one, indicating that the end of the file
{
outStream.write(buffer, 0, bytes);
outStream.writeBytes("\r\n");
}
}
}
请帮我。谢谢你。