save and retrieve image from database using spring mvc and hibernate Rest services

▼魔方 西西 提交于 2020-01-15 10:15:36

问题


I am developing an application where I need to store Image into db and retrieve.I have done saving part. I stored an Image in byte[] form.when I am retrieving an Image json returning

jThiODUwMjE0MDNlNGFlZTI2MTk1YjBlNDFlYjAwYTI2MTI4OWRlOGU4NGU0OTFlZGU2NzAzMjE2ODA0N2RkMDY5NTkyODg4NzliOWE1ODlhNTNhYTM1OTYxNzZjNTc4YjcwMTJiZmUyNmY1NTJkNzI1MjhkN2FhZjU0ZmU0MWZmMzVjMWJhYzU2NmU3Y2M4NTUzMTBlNWMxODRhMjczYTIwZjhhNDk1MzU0NzhhN2YxNDgxMmYxNzRhNTVhMDI4YzVkYmI3NzgzNWMzNjZkYjFiNDgwN2JhNTUyNDEzMDAzZTJlZmRjYjNkNzFkZTIzZDNiMmNjYTgyN2I4ZTgzM

goes like this 100lines.Here I am seraching by Image Id.

Rest service:

@RequestMapping(value="/getphoto/{clsfdimageid}", method=RequestMethod.GET)
    @ResponseBody
    public List<ClassifiedImages> getPhoto(@PathVariable int clsfdimageid)
    {
        System.out.println("entered....................");

        return classifiedService.getImage(clsfdimageid);
    }

DaoImpl class:

@Override
    public List<ClassifiedImages> getImage(int clsfdimageid) 
    {
        session = sessionFactory.getCurrentSession();

        String sql = "SELECT * from tblclsfdimages where clsfdimageid = :clsfdimageid";

        Query query= session.createSQLQuery(sql)
                .addEntity(ClassifiedImages.class)
                .setParameter("clsfdimageid",clsfdimageid);

        List<ClassifiedImages> images = query.list();

        return images;
    }

How can I retrieve an Image in json with byte[] format. Thanks in advance for your suggestions.


回答1:


Even if you haven't provided the ClassifiedImages, I assume you have a field like:

private byte[] image;

That string you see it's the Base64 encoded value of your image byte[] array. It's perfectly fine and there's nothing wrong with it. REST and JSON demand every contained field to be serialized as String, byte[] arrays included.

So, in the client code you need a Base64 decoder to take the encoded String and transform it back to a byte[] array and display your image.



来源:https://stackoverflow.com/questions/24567553/save-and-retrieve-image-from-database-using-spring-mvc-and-hibernate-rest-servic

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!