Home > Mobile >  How to convert blob to image with Spring
How to convert blob to image with Spring

Time:12-17

I have a mysql db and I have a table called User and contains a column called pct that is of Type Blob.

I am using hibernate to carry out a native select query as follows:

   public List<Map<String, Object>> queryExtraction(String sql, QWSInputParam[] qwsInputParams) {

        sql = "SELECT user.name,user.pct FROM user user  WHERE user.id = user.id and user.id in :PARAM0";
        Query query = getSession().createSQLQuery(sql);

        query.setResultTransformer(CriteriaSpecification.ALIAS_TO_ENTITY_MAP);
        for (int i = 0; i < qwsInputParams.length; i  ) {
            LOGGER.info("PARAM"   i   ": "   Arrays.toString(qwsInputParams[i].getValues()));
            query.setParameterList("PARAM"   i, qwsInputParams[i].getValues());
        }

        //LOGGER.info("Query extraction: "   query.toString());
        //query.setTimeout(QUERY_TIME_OUT);
        List<Map<String, Object>> list = query.list();

        Object value = null;

        for (Map<String, Object> map : list) {
            for (Map.Entry<String, Object> entry : map.entrySet()) {
                String key = entry.getKey();
                value = entry.getValue();
                System.out.println("0 "   entry.getValue());

            }

        }
        return list;
    }

I cannot use entity because it is a generic method that should cater for any table and therefore not related to a specific table. Basically when the query is executed and for the column pct of type blob the following value is displayed:

[B@1a270232

Based on the enter image description here

Is it possible to convert the value [B@1a270232 to a base64 so that I can display it on my browser? Thanks in advance

CodePudding user response:

How about:

// ... once get hold of a byte array:
  byte[] bytes = (byte[]) entry.getValue(); // ..!??

  System.out.println(Base64Utils.encodeToString(bytes));
// ...

Javadoc:

CodePudding user response:

An alternative solution using apache IOUtils which matches with your exact requirement.

This is from my Github repo. Where I have the the Entity having:

@Lob
private Byte[] userpicture;

The controller to get image to view:

@GetMapping("appUser/{id}/appUserimage")
    public void renderImageFromDB(@PathVariable String id, HttpServletResponse response) throws IOException {
        AppUserCommand appUserCommand = appUserService.findCommandById(Long.valueOf(id));

        if (appUserCommand.getUserpicture() != null) {
            byte[] byteArray = new byte[appUserCommand.getUserpicture().length];
            int i = 0;

            for (Byte wrappedByte : appUserCommand.getUserpicture()){
                byteArray[i  ] = wrappedByte; //auto unboxing
            }

            response.setContentType("image/jpeg");
            InputStream is = new ByteArrayInputStream(byteArray);
            IOUtils.copy(is, response.getOutputStream());
        }
    }

And finally the view where the image is displayed:

<td><div th:if="${appUser.userpicture == null}">
<img th:src="@{/images/defaultuser.jpg}" width="40" height="40" /> 
<a href="#" th:href="@{'/appUser/'   ${appUser.id}   '/image'}" role="button">Upload</a> </div>
Change
  • Related