简体   繁体   English

通过HTTP服务器发送HTML图像?

[英]Send Html images over http server?

I have a java server socket (or http server, everyone calls it something different), and I am trying to display an image. 我有一个Java服务器套接字(或http服务器,每个人都称其不同),并且我试图显示图像。 Right now, I am just using an image hosting website, then linking it from there, but I would like to know, if I could send images through my connection? 现在,我只是使用图像托管网站,然后从那里链接它,但是我想知道是否可以通过连接发送图像? Here is my code, it is only one class, 这是我的代码,只有一个类,

import java.io.File;
import java.io.IOEXception;
import java.new.ServerSocket;
import java.net.Socket;
import java.util.Scanner
public class main {
public static void main(String argumentsCanBeNamedAnything[]) throws 
IOException {
ServerSocket server = new ServerSocket(80);
System.out.println("Listening");
while(true) {
try(Socket socket = server.accept()) {
String daCode = "";
File file = new File("index.html");
Scanner sc = new Scanner(file);
while(sc.hasNextLine()) {
String i = sc.nextLine();
daCode+=i;
}
String httpResponse = "HTTP/1.1 200 OK\r\n\r\n" + daCode;
socket.getOutputStream().write(httpResponse.getBytes("UTF-8"));
}
}
}
}

I would like to be able to have an image in the same directory as index.html, and then be able to send that image so I don't have to use an image hosting website. 我希望能够在与index.html相同的目录中拥有一个图像,然后能够发送该图像,因此我不必使用图像托管网站。 Any idea is a good idea :) 任何想法都是一个好主意:)

You can handle all files with the same piece of code. 您可以使用同一段代码处理所有文件。 Once you have an input stream that reads from the file, you should copy all bytes to the socket output stream. 一旦具有从文件读取的输入流,则应将所有字节复制到套接字输出流。 So first you should get an input stream like this: 因此,首先您应该获得如下输入流:

String filename = ...
File file = new File(filename);
InputStream input = new FileInputStream(file);

Then you should get an output stream connected to the socket: 然后,您应该获得连接到套接字的输出流:

OutputStream output = socket.getOutputStream();

Now you can write the HTTP response like this: 现在,您可以这样编写HTTP响应:

write(output, "HTTP/1.1 200 OK");
write(output, "\n\n");
copyAllBytes(input, output);

The write method would look like this: write方法如下所示:

void write(OutputStream output, String s) throws IOException {
    output.write(s.getBytes());
}

And the copyAllBytes would look like this: 而copyAllBytes看起来像这样:

void copyAllBytes(InputStream input, OutputStream output) throws IOException {
    byte[] buf = new byte[8192];
    int n;
    while ((n = input.read(buf)) != -1) {
        output.write(buf, 0, n);
    }
}

Ideally you should also send a "Content-Type" header that indicates the nature and format of the document. 理想情况下,您还应该发送“ Content-Type”标头,以指示文档的性质和格式。 Headers should also be written to the socket output, here is a simple example: 标头也应写入套接字输出,这是一个简单的示例:

write(output, "HTTP/1.1 200 OK");
write(output, "\n");
write(output, "Content-Type: text/html; charset=utf-8");
write(output, "\n\n");
copyAllBytes(input, output);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM