如何通过src标签显示包含base64格式图像的数组?

时间:2018-08-29 06:34:26

标签: base64 src

如何通过src标记显示包含base64格式图像的数组,我正在尝试显示base64格式的图像。当我将base64字符串粘贴为src时,它的工作正常。但这不是我想要做的。 我要显示一个包含图像的数组元素。

这样的JSP文件:

<img src="" id="popupnidImage"
                onmousemove=imageZoom() style="max-height: 500px;" padding-left: 0px;" />

像这样的Ajax(此代码也在jsp文件中):

$.ajax(
                            {
                                url : 'uploadOffierController.jsp?action=getnid&image_id='
                                        + ref_number + '&imgetype=' + imageType,
                                contentType : 'application/json',
                                context : document.body
                            })
                    .done(
                            function(response) {                                  
                                var repon = response.trim().split(",");
                                document.getElementById("popupnidImage")
                                        .setAttribute(
                                                'src',
                                                'data:image/jpg;base64,"'
                                                        + repon[0] + '"');

                            });

action = getnid 是返回base64图像的Java函数。使用该响应试图使用repon [0]数组元素显示图像。而且 getnid 功能可以正常工作。

getnid

List<AccountHistory> acchistoryDate = new ArrayList<AccountHistory>();
        AccountController acc = new AccountController();
        String imgDataPath = "";

        String query = "SELECT " + tablefieldname + " FROM tablename WHERE fieldname='" + msisdn + "'";
        System.out.print(">>****v123****>>" + query);
        imgDataPath = getImagePath(query);
        System.out.println("imgDataPath : " + imgDataPath);


        BufferedImage bImage = ImageIO.read(new File(imgDataPath));//give the path of an image
        System.out.println("bImage : " + bImage);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ImageIO.write(bImage, "jpg", baos);
        ImageIO.write(bImage, "png", baos);
        baos.flush();
        byte[] imageInByteArray = baos.toByteArray();
        baos.close();
        String b64 = javax.xml.bind.DatatypeConverter.printBase64Binary(imageInByteArray);

        String historydate = "SELECT `xxxxx` FROM `xxxx` WHERE `xxx`='" + msisdn
                + "'  AND " + xxxxx + " IS NOT NULL";
        System.out.println(">>historydate>>" + historydate);
        acchistoryDate = acc.gethistoryDetails(historydate);
        String nidhis = "";
        for (int x = 0; x < acchistoryDate.size(); x++) {
            nidhis += acchistoryDate.get(x).getRejectedDate().substring(0, 19) + ",";
        }

        out.print(b64 + "," + nidhis);

0 个答案:

没有答案