【问题标题】:Serve font/images with Nanohttpd from Android使用来自 Android 的 Nanohttpd 提供字体/图像
【发布时间】:2018-08-12 04:24:24
【问题描述】:

我正在尝试使用 nanohttpd 托管 Angular 应用程序,因此我将文件放入了 android 应用程序的 assets 文件夹内的 dist/ 文件夹中。现在我想提供 angular 文件,但在控制台中不断出现这种错误(仅在尝试请求字体和图像时出现):

GET http://hostname/font.woff2 200 (OK)

这是我用来提供文件的代码:

public Response serve(IHTTPSession session) {
    String filepath = getFilepath(session.getUri()); // Get filepath depending on the requested url
    String mimeType = getMimeType(filepath); // Get mimetype depending on the extension of the filepath (font/woff, font/woff2, font/ttf, image/x-icon, text/html, application/javascript)
    String content;
    byte[] buffer;
    Response res;
    InputStream is;
    try {
        is = this.assetManager.open("dist/" + filepath);
        int size = is.available();

        buffer = new byte[size];
        is.read(buffer);
        is.close();

        content = new String(buffer);
        content = content.replace("old string", "new string");

        if (typeText(mimeType)) { // If mimeType is text/html or application/json
            res = newFixedLengthResponse(content);
        }else{ // This is when I try to serve fonts or images
            res = newFixedLengthResponse(Response.Status.OK, mimeType, is, size); // Not working
        }

    }catch(IOException e) {
        res = newFixedLengthResponse("Error!");
    }
    return res;
}

我认为可能是字体文件被压缩了,或者大小不是 InputStream 的实际大小。此外,在加载页面时,vendor.js 需要大量下载,然后停止下载其余文件。

我在 logcat 上也收到此错误:

Communication with the client broken, or an bug in the handler code

【问题讨论】:

    标签: android angular inputstream nanohttpd


    【解决方案1】:

    我是这样解决的:

    public Response serve(IHTTPSession session) {
        String filepath = getFilepath(session.getUri()); // Get filepath depending on the requested url
        String mimeType = getMimeType(filepath); // Get mimetype depending on the extension of the filepath (font/woff, font/woff2, font/ttf, image/x-icon, text/html, application/javascript)
        String content;
        byte[] buffer;
        Response res;
        InputStream is;
        try {
            is = this.assetManager.open("dist/" + filepath);
            if (!typeText(mimeType)) { // If mimeType is font/<something> or image/<something>
                return newFixedLengthResponse(Response.Status.OK, mimeType, is, -1);
            }
            int size = is.available();
            buffer = new byte[size];
            is.read(buffer);
            is.close();
            content = new String(buffer);
            content = content.replace("old string", "new string");
        }catch(IOException e) {
            content = "Error!";
        }
        return newFixedLengthResponse(content);
    }
    

    我真的不知道发生了什么,但这种方式效果很好。在我看来,is.available() 没有返回正确的文件大小。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-09-27
      • 1970-01-01
      • 2014-08-04
      • 2023-03-31
      相关资源
      最近更新 更多