Java - HttpServer不提供图像

时间:2016-12-18 23:03:56

标签: java css http httpserver

我有一个简单的设置,包括一个带有一些上下文的HttpServer来加载图像和CSS文件:

public class Server {
    private HttpServer httpServer;

    public Server(int port, String path, HttpHandler handler) {
        try {
            httpServer = HttpServer.create(new InetSocketAddress(port), 0);

            httpServer.createContext(path, handler);

            // load css files
            List<String> cssFiles = new ArrayList<String>();
            Files.walk(Paths.get("../css")).forEach(filePath -> {
                if (Files.isRegularFile(filePath)) {
                    cssFiles.add(filePath.getFileName().toString());
                }
            });
            for (String cssFile : cssFiles) {
                httpServer.createContext("/css/" + cssFile, new CssHandler(cssFile));
            }

            // load image files
            List<String> imgFiles = new ArrayList<String>();
            Files.walk(Paths.get("../images")).forEach(filePath -> {
                if (Files.isRegularFile(filePath)) {
                    imgFiles.add(filePath.getFileName().toString());
                }
            });
            for (String imgFile : imgFiles) {
                httpServer.createContext("/images/" + imgFile, new ImageHandler(imgFile));
            }

            httpServer.setExecutor(null);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    public void start() {
        this.httpServer.start();
    }
}

除此之外,还有一个完美正常的css处理程序和一个图像处理程序类,它为html标签中定义的图像提供服务,但是通过css标签包含的图像&#34; background-image&#34;无法加载..为什么?

ImageHandler:

class ImageHandler implements HttpHandler {
    private String img;

    public ImageHandler(String img) {
        this.img = img;
    }

    @Override
    public void handle(HttpExchange http) throws IOException {
        if (http.getRequestMethod().equals("GET")) {
            System.out.println("img transfered..." + img);
            try {
                StringBuilder contentBuilder = new StringBuilder();
                try {
                    BufferedReader in = new BufferedReader(new FileReader("../images/" + img));
                    String str;
                    while ((str = in.readLine()) != null) {
                        contentBuilder.append(str);
                    }
                    in.close();
                } catch (IOException e) {
                }
                String response = contentBuilder.toString();
                http.sendResponseHeaders(200, response.length());
                OutputStream os = http.getResponseBody();
                os.write(response.getBytes());
                os.flush();
                os.close();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        }
    }
}

的sooo,

这有效:

<img src="images/example.png"/>

但是这个不起作用:

background-image: url("images/example.png");

有人可以解释原因并建议如何解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

由于部分URL相对于样式表的来源,因此您的路径结构如下所示:

/images/example.png
/css/example.css

因此,example.css中的图片网址应该是绝对的(/images/example.png),或者具有正确的相对路径(../images/example.png)。