2015-11-12 167 views
0

似乎無法弄清楚如何服務favicon.ico。 JPG,GIF,PNG,HTML,CSS工作沒有問題。製作favicon.ico與澤西島

這裏是我的資源:

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

和:

@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")); 
     } 
    } 
} 

服務器:

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

我已經試過Path'ing它們作爲圖像和HTML,但不能得到它服務於一個圖標。

如果我做/favicon.ico我只是在瀏覽器中得到一個字節轉儲。

+0

似乎與您的Web服務器設置有關的.ico文件擴展名有關。請檢查。 – vijayP

+0

我已經添加了我的服務器代碼。我只是想讓圖標顯示出來。無論是最簡單的方法。 –

+1

@JohnSmith嘗試對圖標文件使用'image/x-icon'內容類型。 –

回答

0

好的。我想到了這一點,我相信卡西歐的答案圖像/ x圖標也可以工作,但還沒有嘗試過。我從調試網絡服務器得到內容/未知,但它確實有效。

更新:我試過image/x-icon。這也行得通,可能是更好的答案。

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

    } 
}