//图片直接显示 public static void makeImg(String path,HttpServletResponse response) throws IOException { byte[] bytes = get64Img(path,response); response.setContentType("image/png"); OutputStream output = response.getOutputStream(); InputStream in = new ByteArrayInputStream(bytes); int len; byte[] buf = new byte[1024]; while ((len = in.read(buf)) != -1) { output.write(buf, 0, len); } output.flush(); } //返回64位编码 public static byte[] get64Img(String path, HttpServletResponse response){ InputStream fis = null; ByteArrayOutputStream outputStream = null; byte[] finalBytes =null ; try{ fis = new FileInputStream(new File(path + "\\" + "1.png")); outputStream = new ByteArrayOutputStream(); byte[] bytes = new byte[1024]; int temp; while ((temp = fis.read(bytes)) != -1) { outputStream.write(bytes, 0, temp); } //转换后的byte[] finalBytes = outputStream.toByteArray(); fis.close(); outputStream.close(); } catch (IOException e) { e.printStackTrace(); } return finalBytes; } |
|