【发布时间】:2009-07-14 07:17:43
【问题描述】:
我有 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”,但传输编码是 chunked ...我不知道这意味着什么!?有人知道吗?
编辑: 下面是文件读取代码:
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 ...
}
【问题讨论】:
-
@ck: 响应是作为字符串的 html 页面......它通过输出流发送回客户端 @Tim: 嗯?
-
你看到图片请求了吗?
-
@Jack:我确实明白了......设置 Content-type 不同,但我想我处理这些请求的方式错误:)
-
你能用 Firebug 检查你的网站吗?它应该为您提供详细的页面和网络信息 - 包括发送和接收的数据、标头等。
-
@kd304: 见上文...我检查了 charles 和 firebug ...两者都给了我“传输编码被分块”...