【问题标题】:Reading BLOB image from MySQL database in spring mvc在 spring mvc 中从 MySQL 数据库中读取 BLOB 图像
【发布时间】:2015-01-23 06:13:15
【问题描述】:

我在从 MySQL 数据库中读取 blob 时遇到了一些问题。我已经让它成功插入数据库,但似乎无法让它读回。我知道你们中的一些人可能在想“他为什么要使用数据库来存储图像的 blob,而不仅仅是文件路径/文件名”,但我希望具有灵活性,因为很多这些图像将存储在服务器而不是本地,这可以优化效率,并允许我在需要时将图像移动到本地。我遵循了一个(简短的)教程,并编写了以下方法来接收 blob。

以下是我的域类:

    @Entity
      @Table(name="PROFILE")
     public class Profile implements Serializable{

           private static final long serialVersionUID = 3641L;


          @Id
       @Column(name="PROFILE_ID")
            @GeneratedValue(strategy=GenerationType.AUTO)
       private int profileId;

      @OneToOne
      @JoinColumn(name="CUSTOMER_ID")
      private Customer customer;



    @Column(name = "FIRST_NAME")
    private String firstName;

    @Column(name = "LAST_NAME")
    private String lastName;

    @Column(name = "ADDRESS")
    private String address;

    @Column(name = "CONTACT_NUMBER")
    private String contactNumber;

    @Column(name = "PROJECT_NAME")
    private String projectName;


        @Column(name="CONTENT")
    @Lob
    private Blob content;


    @Column(name="filename")
      private String filename;

    @Column(name="content_type")
      private String contentType;
   //getters and setter
             }

这是我的控制器:

        @RequestMapping(value="/myProfile.htm", method=RequestMethod.GET)

    public ModelAndView Profilelist(HttpServletRequest request,ModelMap model,Customer 
    customer,Profile profile, HttpServletResponse response) throws SQLException , 
    Exception{
    //Profile profile = new Profile();

    String customerName = request.getUserPrincipal().getName();

    customer = customerService.getCustomerId(customerName);
    profile = profileService.getBycustomerId(customer);
    System.out.println("cust:  "+ customer);
    System.out.println("profile:  "+ profile);
    logger.error("Viewing Profile" +customerName);
    //Customer customer = new Customer();



    ByteArrayOutputStream baos = new ByteArrayOutputStream();
    byte[] buf = new byte[1024];
    byte[] encodeBase64 = Base64.encodeBase64(buf);
    String base64Encoded = new String(encodeBase64, "UTF-8");
    ModelAndView mav = new ModelAndView();
    mav.addObject("profile", base64Encoded);
    //Blob blob = profile.getContent();

    customer.setEmailId(customerName);
    profile.setCustomer(customer);
    //profile.setContent(blob);
    System.out.println();
    profile = profileService.findProfileById(customer);
    model.addAttribute("content",base64Encoded);

    model.addAttribute("profile", profile);
    mav = new ModelAndView("myProfile");
    return mav;

   }

这里无法将字节设置到我的 profile.setContent ......请提供任何解决方案来解决我的问题

【问题讨论】:

    标签: java mysql hibernate spring-mvc


    【解决方案1】:

    您不应该在控制器中执行此操作,来自文档

    默认情况下,驱动程序使用 SQL 定位器 (BLOB) 实现 Blob,它 表示 Blob 对象包含指向 SQL BLOB 的逻辑指针 数据而不是数据本身。 Blob 对象对 创建的事务的持续时间。

    如果您需要访问字节数组,您应该做的是在 Profile 类中创建一个 JPA 瞬态属性,例如像

      @Transient byte[] image; // do add getters and setters
    

    并将字节数组设置为该属性。您的持久性机制会忽略瞬态属性。

    关于评论的更新

    由于目的是在 jsp 中显示图片,您可以采用两种方式,第一种是将文件存储为 Web 可访问的位置,然后简单地从 src 元素指向它,但我从你的问题中了解到,这不是你所需要的。

    第二个选项是将图像渲染为字节序列,但为了做到这一点,您首先必须将字节数组编码为 Base64 编码的字符串。这是一个例子

    要在 JSP 上显示图像而不存储到文件系统并链接到它,您必须对字节数组进行 Base64 编码。将您的控制器方法更改为

    @RequestMapping(value="/myProfile.htm", method=RequestMethod.GET)
    public String Profilelist(HttpServletRequest request,ModelMap model,Customer
            customer,Profile profile, HttpServletResponse response) throws SQLException ,
            Exception{
        //Profile profile = new Profile();
    
        String customerName = request.getUserPrincipal().getName();
    
        customer = customerService.getCustomerId(customerName);
        profile = profileService.getBycustomerId(customer);
        System.out.println("cust:  "+ customer);
        System.out.println("profile:  "+ profile);
        logger.error("Viewing Profile" +customerName);
        //Customer customer = new Customer();
    
    
    
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        byte[] buf = new byte[1024];
        Blob blob = profile.getContent();
        InputStream in =  blob.getBinaryStream();
        System.out.println("id content" +in);
        int n = 0;
        while ((n=in.read(buf))>=0)
        {
            baos.write(buf, 0, n);
    
        }
    
        in.close();
        byte[] bytes = baos.toByteArray();
        System.out.println("bytes" +bytes);
        byte[] encodeBase64 = Base64.encodeBase64(buf);
        String base64Encoded = new String(encodeBase64, "UTF-8");
    
    
        customer.setEmailId(customerName);
        profile.setCustomer(customer);
        //profile.setContent(blob);
        System.out.println();
        profile = profileService.findProfileById(customer);
        model.addAttribute("content",base64Encoded);
        model.addAttribute("profile", profile);
        return "myProfile";
    }
    

    【讨论】:

      猜你喜欢
      • 2012-07-16
      • 1970-01-01
      • 2013-04-08
      • 1970-01-01
      • 2014-08-20
      • 2015-07-28
      • 2012-03-27
      • 2014-02-19
      • 1970-01-01
      相关资源
      最近更新 更多