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;

        }
经验分享 程序员 微信小程序 职场和发展