简体   繁体   English

用Jersey生产favicon.ico

[英]Producing favicon.ico with Jersey

Can't seem to figure out how to serve a favicon.ico . 似乎无法弄清楚如何为favicon.ico提供服务。 JPG, GIF, PNG, HTML, CSS work without problems. JPG,GIF,PNG,HTML,CSS可以正常工作。

Here's my resource: 这是我的资源:

@Path("/{fileName: .+(?:png|jpg|gif)}")
@Produces({"image/png, image/jpg, image/gif"})
public class MimeImages {
    @GET
    public Response getFullImage(@PathParam("fileName") String fileName) throws IOException {
        String sImgType = "jpg";
        if(fileName.toLowerCase().endsWith(".png")) {
            sImgType = "png";
        } else if(fileName.toLowerCase().endsWith(".gif")) {
            sImgType = "gif";
        }
        URL urlToResource = getClass().getResource("/com/test/web/" + fileName);
        BufferedImage image = ImageIO.read(urlToResource);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageIO.write(image, sImgType, baos);
        byte[] imageData = baos.toByteArray();
        return Response.ok(new ByteArrayInputStream(imageData)).build();
    }
}

And: 和:

@Path("/{fileName: .*(?!png|jpg|gif|mp3)}")
@Produces({"text/html, text/plain, text/css"})
public class MimeHtml {
    @GET
    public Response getPage(@PathParam("fileName") String fileName) throws IOException {
        fileName = fileName.equals("") ? "index.htm" : fileName;
        URL urlToResource = getClass().getResource("/com/test/web/" + fileName);
        return Response.ok(read(urlToResource.openConnection().getInputStream())).build();
    }

    private String read(InputStream stream) throws IOException {
        try (BufferedReader buffer = new BufferedReader(new InputStreamReader(
                stream, StandardCharsets.UTF_8))) {
            return buffer.lines().collect(Collectors.joining("\n"));
        }
    }
}

Server: 服务器:

public class WebServer {

    private HttpServer webServer;

    public void start() throws IOException {
        System.out.println("Starting WebServer\n");
        webServer = createHttpServer();
        webServer.start();
        System.out.println(String.format("\nWeb Server started:" + "%sapplication.wadl\n", getURI()));
    }

    public void stop() {
        webServer.stop(0);
    }

    public static HttpServer createHttpServer() throws IOException {
        ResourceConfig rc = new PackagesResourceConfig("com.test");
        return HttpServerFactory.create(getURI(), rc);
    }

    private static URI getURI() {
        return UriBuilder.fromUri("http://localhost/").port(4444).build();
    }
}

I've tried Path'ing them as image and HTML but can't get it to serve an icon. 我已经尝试过将它们作为图像和HTML进行路径处理,但无法将其用作图标。

If I do /favicon.ico I just get a byte dump in the browser. 如果我执行/favicon.ico我只会在浏览器中得到一个字节转储。

Ok. 好。 I figured this out and I believe Cassio's answer image/x-icon may work as well but haven't tried it. 我知道了这一点,并且我相信Cassio的答案图像/ x-icon可能也起作用,但是还没有尝试过。 I got content/unknown from debugging a webserver but it does work. 我从调试Web服务器中获得了内容/未知,但是它确实可以工作。

UPDATE: I tried image/x-icon. 更新:我尝试了图像/ X图标。 This also works and probably is the better answer. 这也可行,可能是更好的答案。

@Path("/{fileName: .*ico}")
@Produces({"image/x-icon"})
public class MimeFavIcon {
    @GET
    public Response getPage(@PathParam("fileName") String fileName) throws IOException {
        fileName = fileName.equals("") ? "index.htm" : fileName;
        URL urlToResource = getClass().getResource("/com/daford/web/" + fileName);
        URLConnection conn = urlToResource.openConnection();
        InputStream inConnectionReader = conn.getInputStream();
        int size = conn.getContentLength();
        byte[] imageData = new byte[size];
        //noinspection ResultOfMethodCallIgnored
        inConnectionReader.read(imageData, 0, size);
        return Response.ok(new ByteArrayInputStream(imageData)).build();

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM