簡體   English   中英

Java-HttpServer不提供圖像

[英]Java - HttpServer not serving images

我有一個簡單的設置,包括一個帶有一些上下文的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標記“ background-image”包含的BUT圖像。為什么?

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();
            }
        }
    }
}

太好了

這有效:

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

但這不起作用:

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

有人可以解釋原因並提出解決方案嗎?

由於部分URL相對於樣式表的來源,因此您的路徑結構如下所示:

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

因此example.css圖片URL應該是絕對URL( /images/example.png ),或具有正確的相對路徑( ../images/example.png )。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM