加载一个托管在应用中与web服务器一起暴露的资产文件夹中的图像文件

loading an image file hosted in assets folder exposed with web server in the app

本文关键字:暴露 一起 服务器 文件 图像 文件夹 web 一个 加载 应用      更新时间:2023-09-26

在我的应用程序中,我创建了一个web服务器,它承载了一个web应用程序。web应用程序的所有文件都放在assets文件夹中。

现在,我通过运行我的应用程序启动web服务器,然后从chrome浏览器,我尝试通过调用index.html文件来运行我的web应用程序。页面的html和css部分可以正常加载,但是图片无法加载:

这是我的HttpRequestHandlerCode:

public class HomePageHandler implements HttpRequestHandler {
    private Context context = null;
    private static final Map<String, String> mimeTypes = new HashMap<String, String>() {
        {
            put("css", "text/css");
            put("htm", "text/html");
            put("html", "text/html");
            put("xhtml", "text/xhtml");
            put("xml", "text/xml");
            put("java", "text/x-java-source, text/java");
            put("md", "text/plain");
            put("txt", "text/plain");
            put("asc", "text/plain");
            put("gif", "image/gif");
            put("jpg", "image/jpeg");
            put("jpeg", "image/jpeg");
            put("png", "image/png");
            put("svg", "image/svg+xml");
            put("mp3", "audio/mpeg");
            put("m3u", "audio/mpeg-url");
            put("mp4", "video/mp4");
            put("ogv", "video/ogg");
            put("flv", "video/x-flv");
            put("mov", "video/quicktime");
            put("swf", "application/x-shockwave-flash");
            put("js", "application/javascript");
            put("pdf", "application/pdf");
            put("doc", "application/msword");
            put("ogg", "application/x-ogg");
            put("zip", "application/octet-stream");
            put("exe", "application/octet-stream");
            put("class", "application/octet-stream");
            put("m3u8", "application/vnd.apple.mpegurl");
            put("ts", " video/mp2t");
        }
    };
    public HomePageHandler(Context context){
        this.context = context;
    }
    @Override
    public void handle(HttpRequest request, HttpResponse response, HttpContext httpContext) throws HttpException, IOException {
        //String contentType = "text/html";
        //Log.i("Sushill", "..request : " + request.getRequestLine().getUri().toString());
        final String requestUri = request.getRequestLine().getUri().toString();
        final String contentType = contentType(requestUri);
                        String resp = Utility.openHTMLStringFromAssets(context, "html" + requestUri);
                        writer.write(resp);
                        writer.flush();
//                    }
                }
            });
            ((EntityTemplate) entity).setContentType(contentType);
            response.setEntity(entity);
        }
    }
    /**
     * Get content type
     *
     * @param fileName
     *            The file
     * @return Content type
     */
    private String contentType(String fileName) {
        String ext = "";
        int idx = fileName.lastIndexOf(".");
        if (idx >= 0) {
            ext = fileName.substring(idx + 1);
        }
        if (mimeTypes.containsKey(ext)) {
            //Log.i("Sushill", "...ext : " + ext);
            return mimeTypes.get(ext);
        }
        else
            return "application/octet-stream";
    }

处理图像,我尝试了这个,但它没有工作:

if(contentType.contains("image")) {
                    InputStream is = Utility.openImageFromAssets(context, "html" + requestUri);
                    char[] buffer = new char[1024];
                    try {
                        Reader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
                        int n;
                        while ((n = reader.read(buffer)) != -1) {
                            writer.write(buffer, 0, n);
                        }
                    } catch (UnsupportedEncodingException e) {
                        e.printStackTrace();
                    } catch (IOException e) {
                        e.printStackTrace();
                    } finally {
                        try {
                            is.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }

有人能帮我弄清楚如何加载图像也在我的浏览器。

感谢您的帮助

取消BufferedReader(new InputStreamReader'),所以你也取消了UTF-8。只使用InputStream 'is'。废除作家。你并不是在展示"作家"是什么,而是把它去掉。使用http连接的OutputStream。

保留在缓冲区中读取和写入的缓冲区和循环。