NanoHTTPD:显示图像

时间:2016-09-08 14:26:06

标签: java nanohttpd

我尝试使用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";

1 个答案:

答案 0 :(得分:1)

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

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