java通过路径返回图片base64

 @RequestMapping(value = "/detail", method = RequestMethod.GET)
    public Map wsyc_download(HttpServletRequest request, HttpServletResponse response) throws Exception {
            String path ="D:/a/111.jpg";
            byte[] data = null;
            FileImageInputStream input = null;
            try {
                input = new FileImageInputStream(new File(path));
                ByteArrayOutputStream output = new ByteArrayOutputStream();
                byte[] buf = new byte[1024];
                int numBytesRead = 0;
                while((numBytesRead = input.read(buf)) != -1){
                    output.write(buf, 0, numBytesRead);
                }
                data = output.toByteArray();
                output.close();
                input.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
            String encode = new String(Base64.encodeBase64(data), "UTF-8");
                    HashMap<Object, Object> map = new HashMap<>();
        map.put("code", "0");
        map.put("messages", "查询成功");
        map.put("data", encode);
        return map;

        }


版权声明:本文为Jzq0006原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。