Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/http/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Image 为什么我不能在服务器上显示图像?_Image_Http_Tcp_Webserver - Fatal编程技术网

Image 为什么我不能在服务器上显示图像?

Image 为什么我不能在服务器上显示图像?,image,http,tcp,webserver,Image,Http,Tcp,Webserver,我为这个类编写了这段代码,但我不明白为什么当我点击一个链接时,它不会显示.jpg或.mp4文件。我一直在浏览互联网,并尝试将图像从CMYK转换为RGB,在文件末尾添加更多CRLF,但我无法理解为什么会出现这种错误: “无法显示图像,因为它包含错误。” 或 “无法显示mp4文件,因为它已损坏” 这是我的“欢迎页” 欢迎来到我的服务器 单击以下选项之一: 这是我的服务器 import java.io.*; import java.net.Socket; import java.util.*;

我为这个类编写了这段代码,但我不明白为什么当我点击一个链接时,它不会显示.jpg或.mp4文件。我一直在浏览互联网,并尝试将图像从CMYK转换为RGB,在文件末尾添加更多CRLF,但我无法理解为什么会出现这种错误:

“无法显示图像,因为它包含错误。” 或 “无法显示mp4文件,因为它已损坏”

这是我的“欢迎页”


欢迎来到我的服务器
单击以下选项之一:

这是我的服务器

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");
    }
}
import java.io.*;
导入java.net.Socket;
导入java.util.*;
导入java.awt.*;
导入javax.imageio.*;
公共类HTTPRequest实现可运行
{
公共静态字符串CRLF=“\r\n”//返回回车符(CR)和换行符(LF)
插座;
//建造师
公共HTTPRequest(套接字)引发异常
{
this.socket=socket;
}
//实现Runnable接口的run()方法。
//在run()中,我们使用try/catch语句显式捕获和处理异常。
公开募捐
{
尝试
{
processRequest();
}捕获(例外e)
{
系统输出打印ln(e);
}
}
私有void processRequest()引发异常
{
//创建输入和输出流
InputStream instream=socket.getInputStream();
DataOutputStream outStream=新的DataOutputStream(socket.getOutputStream());
//创建缓冲区
BufferedReader buffRead=new BufferedReader(new InputStreamReader(instream));//读取输入数据
//获取HTTP请求消息的请求行。
String requestLine=buffRead.readLine();//http的get/path/file.html版本
//显示请求行。
System.out.println();
System.out.println(请求行);
//这里我们需要处理这个请求
//从请求行提取文件名。
StringTokenizer令牌=新的StringTokenizer(requestLine);
tokens.nextToken();
字符串文件名=tokens.nextToken();
//这样我就不必一开始就编写/front.html
if(fileName.equals(“/”){
fileName=“/front.html”;
}
//附加“.”以便文件请求位于当前目录中。
fileName=“.”+文件名;
//打开请求的文件。
FileInputStream fis=null;
布尔fileExists=true;
尝试
{
fis=新文件输入流(文件名);
}catch(filenotfounde异常)
{
fileExists=false;
}
//构造响应消息。
字符串statusLine=null;
字符串contentTypeLine=null;
字符串entityBody=null;
如果(文件存在)
{
statusLine=“HTTP/1.0 200 OK”+CRLF;//200成功代码
contentTypeLine=“内容类型:”+contentType(文件名)+CRLF;
}//内容信息
其他的
{
contentTypeLine=“Content-type:text/html”+CRLF;//内容信息
entityBody=“+”未找到”
+“未找到”;
statusLine=“HTTP/1.0 404未找到”+CRLF;//404未找到。。。
}
//发送状态行。
超流写入字节(状态行);
//发送内容类型行。
outStream.writeBytes(contentTypeLine);
//发送一个空行以指示标题行的结尾。
超流写入字节(CRLF);
//发送实体体。
如果(文件存在)
{
outStream.writeBytes(状态行);//发送状态行。
outStream.writeBytes(“\n”+contentTypeLine);//发送内容类型行。
发送字节(fis,外扩);
fis.close();
}否则
{
outStream.writeBytes(状态行);//发送状态行
outStream.writeBytes(“\n”+contentTypeLine);//发送内容类型行。
outStream.writeBytes(entityBody);//发送html错误消息信息正文。
}
System.out.println(“*******”);
System.out.println(文件名);//打印输出到控制台的文件请求
System.out.println(“*******”);
//获取并显示标题行。
字符串标题行=null;
while((headerLine=buffRead.readLine()).length()!=0)
{
系统输出打印LN(车头线);
}
//关闭流和套接字。
exptream.close();
buffRead.close();
socket.close();
}
//返回文件类型
私有静态字符串contentType(字符串文件名)
{
if(fileName.endsWith(“.htm”)| | fileName.endsWith(“.html”))
{
返回“text/html”;
}
if(fileName.endsWith(“.jpg”)| | fileName.endsWith(“.jpeg”))
{
返回“图像/jpeg”;
}
if(fileName.endsWith(“.gif”))
{
返回“image/gif”;
}
if(fileName.endsWith(“.mp4”))
{
返回“电影”;
}
返回“应用程序/八位字节流”;
}
//设置i/o流
私有静态void sendBytes(FileInputStream fis、DataOutputStream outStream)
抛出异常
{
//构造一个1K缓冲区,在字节到达套接字的过程中保存字节。
字节[]缓冲区=新字节[1024];
int字节=0;
//将请求的文件复制到套接字的输出流中。
而((bytes=fis.read(buffer))!=-1)//read()返回负一,表示文件结尾
{
输出流写入(缓冲区,0,字节);
outStream.writeBytes(“\r\n”);
}
}
}


请帮帮我。谢谢。

我听不懂这句话:

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

不管怎么说,你能把这篇文章扔了吗?这样我们就可以检查到底发生了什么事了?

我问每个人该怎么办,有人遇到了一个问题,他们在什么地方没有回程线,所以我大发雷霆,几乎到处都是。可能是我忘了带的东西。你能告诉我“转储帖子”是什么意思吗?我指的是你上一次写的字节,在你写二进制数据时,这对我来说没有什么意义。“post”转储是尝试向我/我们展示您的答案,至少是前10行。GET/candyplease.jpg
outStream.writeBytes("\r\n");