NanoHTTPD:显示图像

NanoHTTPD: Display images

我正在尝试使用 NanoHTTPD server 在浏览器上显示图像,但始终无法显示任何图像。 这是我的服务方法的一部分:

else if(uri.contains(".png")){
        SmallBinaryFiles smallBinaryFiles = new SmallBinaryFiles();
  InputStream is = new InputStream() {
            @Override
            public int read() throws IOException {
                throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
            }
        };
  long i=0; 
  try {
//smallBinaryFiles.readSmallBinaryFiles(uri): converts binary file given by uri to byte[]
            is = new ByteArrayInputStream(smallBinaryFiles.readSmallBinaryFile(uri));

  while ((is.read()) != -1){
      i++;
  }
        } catch (IOException ex) {
            Logger.getLogger(HelloServer.class.getName()).log(Level.SEVERE, null, ex);
        }

    return new NanoHTTPD.Response(NanoHTTPD.Response.Status.OK, MIME_PNG, is,i);

    } 
 //declaration of MIME_PNG in NanoHTTPD Core
public static final String  MIME_PNG = "image/png";

您的 while() 循环占用了您所有的输入流,因此没有任何内容可发送。将 -1 替换为 i 以使其成为块响应。

此外,您的 read() 方法似乎在调用时抛出异常。请改用 FileInputStream