通过http/1.0加载图像?

gorkyyrv  于 2021-07-07  发布在  Java
关注(0)|答案(0)|浏览(218)

我正在尝试实现http/1.0,但在加载图像时遇到问题。我可以加载文本文件,但我不能加载任何图像。当我将filereader内容添加到字符串并打印到控制台时,它不是空的,而是乱七八糟的,如下面的请求所示。当使用google chromes构建的网络包分析工具时,回应称“加载ressource失败”。我没有得到任何错误,但它只是显示一个空的黑色页面(谷歌浏览器)时,试图加载一个单一的图像也。
以下是一个请求示例:

HTTP/1.0 200 OK
Content-type: image/jpeg
Content-length: 58309
Date: Sat, 28 Nov 2020 16:02:45 GMT

��sf|���V��yn�����]3��B��� .......

这是我读取返回给客户端的文件/图像的方法:

public String readUri(String reqUri) { // image.png fx.

    // check if file exist
    reqFile = new File(reqUri);
    if (reqFile.exists() && reqFile.isFile()) {

        try {
            reader = new BufferedReader(new FileReader(reqUri,StandardCharsets.UTF_8));

            while ((fileLineReader = reader.readLine()) != null) {
                fileContent.append(fileLineReader);
            }
            reader.close();

        } catch (IOException e) {
            System.out.println("error");
        }
        returnFile = fileContent.toString();

    }
    return returnFile;
}

java文档说我应该使用inputstreamreader来读取原始字节,但这两种方法都不起作用。https://docs.oracle.com/javase/7/docs/api/java/io/filereader.html 然后将上述内容发送回套接字以答复浏览器请求

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题