本文介绍了使用spring mvc和hibernate Rest服务保存和检索数据库中的图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开发一个应用程序,我需要将图像存储到数据库并进行检索。我已经完成了保存部分。余存储在字节[]影像form.when我检索图像JSON返回



jThiODUwMjE0MDNlNGFlZTI2MTk1YjBlNDFlYjAwYTI2MTI4OWRlOGU4NGU0OTFlZGU2NzAzMjE2ODA0N2RkMDY5NTkyODg4NzliOWE1ODlhNTNhYTM1OTYxNzZjNTc4YjcwMTJiZmUyNmY1NTJkNzI1MjhkN2FhZjU0ZmU0MWZmMzVjMWJhYzU2NmU3Y2M4NTUzMTBlNWMxODRhMjczYTIwZjhhNDk1MzU0NzhhN2YxNDgxMmYxNzRhNTVhMDI4YzVkYmI3NzgzNWMzNjZkYjFiNDgwN2JhNTUyNDEzMDAzZTJlZmRjYjNkNzFkZTIzZDNiMmNjYTgyN2I4ZTgzM



去像这100lines.Here我图像ID seraching。



休息服务:

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

返回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);

列表< ClassifiedImages> images = query.list();

返回图片;
}

如何以byte []格式检索json中的图像。提前感谢您的建议。 c $ c>,我假设你有这样一个字段:

  private byte [] image; 

该字符串表示您的图像的Base64编码值 byte [] 数组。这非常好,没有错。 REST和JSON要求将每个包含的字段序列化为包含字符串,byte []数组。

因此,在客户端代码中,需要Base64解码器来接收编码的 String 并将它转换回 byte [] 数组并显示您的图片。


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.

解决方案

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.

这篇关于使用spring mvc和hibernate Rest服务保存和检索数据库中的图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 12:45