我有Java网络服务器(没有标准软件......自己编写)。一切似乎都工作正常,但是当我尝试调用包含图片的页面时,这些图片不会显示。我是否必须将图像与输出流发送到客户端?我是否缺少额外的步骤?
由于代码太多,无法在此处发布,因此这里简要概述了发生或应该发生的情况:
1. client logs in
2. client gets a session id and so on
3. the client is connected with an output stream
4. we built the response with the HTML-Code for a certain 'GET'-request
5. look what the GET-request is all about
6. send html response || file || image (not working yet)
基本大纲就这么多了......
它发送 css 文件和其他内容,但我仍然有图像问题! 有人有想法吗?如何将图像从服务器发送到浏览器? 谢谢。
我使用 charles 检查来自客户端的请求和来自服务器的响应。它可以很好地发送文件(如 css 或 js),但不能发送图像:虽然状态是“200 OK”,但传输编码是分块 ...我不知道这意味着什么!?有人知道吗?
编辑: 这是文件读取代码:
try{
File requestedFile = new File( file );
PrintStream out = new PrintStream( this.getHttpExchange().getResponseBody() );
// File wird geschickt:
InputStream in = new FileInputStream( requestedFile );
byte content[] = new byte[(int)requestedFile.length()];
in.read( content );
try{
// some header stuff
out.write( content );
}
catch( Exception e ){
e.printStackTrace();
}
in.close();
if(out!=null){
out.close();
System.out.println( "FILE " + uri + " SEND!" );
}
}
catch ( /*all exceptions*/ ) {
// catch it ...
}
最佳答案
您的浏览器将单独发送GET image.png HTTP 1.1
向您的服务器发出请求时,您也应该处理这些文件获取。没有好的方法可以在 HTML 中嵌入图像并且独立于浏览器,只有 <img src="data:base64codedimage">
协议(protocol)处理程序在某些浏览器中可用。
当您创建 HTML 响应时,您可以直接在 <script></script>
之间包含外部 js/css 文件的内容。和<style></style>
标签。
编辑:我建议使用 Firebug以便进一步诊断。
关于java - 从自己编写的网络服务器调用网站时不显示图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1123921/