我尝试使用NanoHTTPD server在浏览器上显示图像,但总是没有显示。这是我的serve方法的一部分:
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";发布于 2016-09-11 05:01:35
while()循环吃掉了所有的输入流,所以没有什么需要发送的了。将i替换为-1,以使其成为块响应。
此外,您的read()方法在被调用时似乎会抛出异常。请改用FileInputStream。
https://stackoverflow.com/questions/39393557
复制相似问题