Java 通过http服务器发送Html图像?
我有一个java服务器套接字(或http服务器,每个人都称之为不同的东西),我试图显示一个图像。现在,我只是使用一个图像托管网站,然后从那里链接它,但我想知道,如果我可以通过我的连接发送图像?这是我的代码,它只是一个类Java 通过http服务器发送Html图像?,java,image,networking,server,Java,Image,Networking,Server,我有一个java服务器套接字(或http服务器,每个人都称之为不同的东西),我试图显示一个图像。现在,我只是使用一个图像托管网站,然后从那里链接它,但我想知道,如果我可以通过我的连接发送图像?这是我的代码,它只是一个类 import java.io.File; import java.io.IOEXception; import java.new.ServerSocket; import java.net.Socket; import java.util.Scanner public 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"));
}
}
}
}
我想能够有一个图像在同一个目录作为index.html,然后能够发送该图像,所以我不必使用图像托管网站。任何想法都是好主意:)您可以用同一段代码处理所有文件。一旦有了从文件读取的输入流,就应该将所有字节复制到套接字输出流。所以首先你 应获得如下所示的输入流:
String filename = ...
File file = new File(filename);
InputStream input = new FileInputStream(file);
write(output, "HTTP/1.1 200 OK");
write(output, "\n\n");
copyAllBytes(input, output);
void write(OutputStream output, String s) throws IOException {
output.write(s.getBytes());
}
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);
}
}
然后,您应该获得一个连接到套接字的输出流:
OutputStream output = socket.getOutputStream();
现在您可以这样编写HTTP响应:
String filename = ...
File file = new File(filename);
InputStream input = new FileInputStream(file);
write(output, "HTTP/1.1 200 OK");
write(output, "\n\n");
copyAllBytes(input, output);
void write(OutputStream output, String s) throws IOException {
output.write(s.getBytes());
}
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);
}
}
write方法如下所示:
String filename = ...
File file = new File(filename);
InputStream input = new FileInputStream(file);
write(output, "HTTP/1.1 200 OK");
write(output, "\n\n");
copyAllBytes(input, output);
void write(OutputStream output, String s) throws IOException {
output.write(s.getBytes());
}
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);
}
}
copyAllBytes将如下所示:
String filename = ...
File file = new File(filename);
InputStream input = new FileInputStream(file);
write(output, "HTTP/1.1 200 OK");
write(output, "\n\n");
copyAllBytes(input, output);
void write(OutputStream output, String s) throws IOException {
output.write(s.getBytes());
}
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);
}
}
理想情况下,您还应该发送一个“内容类型”标题,指示文档的性质和格式。头也应该写入套接字输出,下面是一个简单的示例:
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);
您必须在服务器上托管图像(与运行上述代码的服务器相同),然后发送一个带有正确图像标记的HTML文件来加载图像。您可能想查看O'Reily的Learning Java,它有一个完全用Java编写的小型web服务器,就像您所做的那样。它也不比你的例子大多少,基本的都不难。这是服务器,如果我在我的机器上有图像,然后发送它,它就不会显示了是的。我说“在你的服务器上托管映像”,所以它肯定会显示出来。你可以从在自己的机器上测试开始,这就是我所做的(是的,这适用于服务器,我已经写了一些)。@markspace我正在阅读这本书的在线pdf版本,我明白你的意思,抱歉说你错了,我错了。非常感谢您向我展示这一点:)