【问题标题】:Why can I not show images on my server?为什么我不能在我的服务器上显示图像?
【发布时间】:2013-09-20 18:04:19
【问题描述】:

我已经为课程编写了这段代码,但我无法弄清楚为什么当我单击链接时,它不会显示 .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");
    }
}

}

请帮助我。谢谢。

【问题讨论】:

    标签: image http tcp webserver


    【解决方案1】:

    我不明白这一行:

    outStream.writeBytes("\r\n");
    

    不管怎样,你能把帖子转出让我们检查发生了什么吗?

    【讨论】:

    • 我问每个人该怎么做,有人遇到问题,他们在某个地方没有回车换行,所以我去弹道,几乎把它放在任何地方。这可能是我忘记取出的东西。你能告诉我“转储”是什么意思吗?
    • 我指出你最后的 writeBytes 对我来说没有意义,因为你正在编写二进制数据。 “发布”转储是试图向我/我们展示你的答案是什么样的,至少前 10 行。
    • GET /candyplease.jpg HTTP/1.1 *****./candyplease.jpg ***** 主机:192.168.1.23:9999 用户代理:Mozilla/5.0 (Windows NT 6.2; WOW64; rv:23.0) Gecko/20100101 Firefox/23.0 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8 Accept-Language: en- US,en;q=0.5 Accept-Encoding: gzip, deflate Referer: 192.168.1.23:9999 Connection: keep-alive
    • 这是来自浏览器的查询,你能显示你的服务器答案的前 10 行吗?非常感谢:)
    • 这些是控制台中的返回行。正如我之前所说,在浏览器上,首页工作正常。但是,单击“candyplease”后,我收到“图像无法显示,因为它包含错误”错误。
    猜你喜欢
    • 2012-01-12
    • 2015-05-04
    • 1970-01-01
    • 1970-01-01
    • 2018-10-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多