0

クラス用にこのコードを作成しましたが、リンクをクリックしても.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");
    }
}

}

私を助けてください。ありがとうございました。

4

1 に答える 1