2016-03-16 129 views
0

当我滚动到表和表加载它的内容,我的图片的底部(图片来自StreamResource嵌入式,我存储我的图片在数据库中的BLOB)得到破碎。破碎的形象7.6.3

包含的第一行表格嵌入式图像显示得很好,但是当我向下滚动时,看起来不错的图片会被破坏。

getAllPartners方法(在这里我加载从数据库团块图像):

public static Vector<PartnerDto> getAllPartner() { 
     Vector<PartnerDto> result = null; 

     PreparedStatement st = null; 
     Connection conn = null; 
     ResultSet rs = null; 
     try { 
      conn = Connect.getConnection(); 
      String sql = "select part_id,name, surname, picture from partner order by name, surname "; 
      st = conn.prepareStatement(sql); 
      System.out.println(sql); 
      st.execute(); 
      rs = st.getResultSet(); 
      result = new Vector<PartnerDto>(); 
      while (rs.next()) { 
       PartnerDto dto = new PartnerDto(); 
       dto.setPart_id(rs.getInt("part_id")); 
       dto.setName(rs.getString("name")); 
       dto.setSurname(rs.getString("surname")); 
       Blob imageBlob = rs.getBlob("picture"); 
       if(imageBlob!=null){ 
        InputStream binaryStream = imageBlob.getBinaryStream(1, imageBlob.length());     
        dto.setPicture(binaryStream); 
       }else{ 
        dto.setPicture(null); 
       } 
       result.addElement(dto); 
      } 
     } catch (SQLException e) { 
      result = null; 
      e.printStackTrace(); 
      System.out.println(e); 
     } finally { 
      Connect.closeConnection(rs, st, conn); 
     } 
     return result; 
    } 

我的表(tPartners)initalization:

 tPartners.addContainerProperty("Slika uporabnika", Embedded.class, null); 
     tPartners.addContainerProperty("Ime", String.class, null); 
     tPartners.addContainerProperty("Priimek", String.class, null); 
     tPartners.addContainerProperty("Rojstni datum", String.class, null); 
     tPartners.addContainerProperty("Rojstni kraj", String.class, null); 
     tPartners.addContainerProperty("Naslov", String.class, null); 
     tPartners.addContainerProperty("Pošta", String.class, null); 
     tPartners.addContainerProperty("Emšo", String.class, null); 
     tPartners.addContainerProperty("Šola", String.class, null); 
     tPartners.addContainerProperty("Razred", String.class, null); 
     tPartners.addContainerProperty("Poklic", String.class, null); 
     tPartners.addContainerProperty("Telefon", String.class, null); 
     tPartners.addContainerProperty("Mail", String.class, null); 
     tPartners.addContainerProperty("Klubska številka", String.class, null); 
     tPartners.addContainerProperty("Številka kartice", String.class, null); 
     tPartners.addContainerProperty("Datum včlanitve", String.class, null); 
     tPartners.addContainerProperty("Opomba", String.class, null); 
     tPartners.addContainerProperty("File type", String.class, null); 

     tPartners.setSelectable(true); 
     tPartners.setImmediate(true); 

代码波纹管展示了如何我负荷图像表:

   //get all partners 
      Vector<PartnerDto> vResutl = DBLogika.searchPartner(text); 

      for (int i = 0; i < vResutl.size(); i++) { 
       //class that represent PartnerDto 
       PartnerDto dto = vResutl.elementAt(i); 

       String bd = ""; 
       String din = ""; 

       if (dto.getBorn_date() != null) { 
        bd = sdf.format(dto.getBorn_date()); 
       } 

       if (dto.getDate_in() != null) { 
        din = sdf.format(dto.getDate_in()); 
       } 

       Embedded emb=new Embedded(); 

       StreamResource.StreamSource source = new StreamResource.StreamSource() { 

        public InputStream getStream() { 
         //get inputstream from database, I save my image as longblob in database 
         return dto.getPicture(); 

        } 

       }; 

       //uniq name for every file 
       SimpleDateFormat df = new SimpleDateFormat("yyyyMMddHHmmssSSS"); 
       String filename = "mytablepic" + df.format(new Date()) + ".png"; 

       StreamResource sourceone=new StreamResource(source, filename); 

       emb.setHeight("200px"); 
       emb.setWidth("150px"); 

       emb.setSource(dto.getPicture()==null? new ThemeResource("images/user_pic.jpg"): sourceone); 

       tPartners.addItem((new Object[] {emb, dto.getName(), dto.getSurname(), bd, dto.getBorn_place(), dto.getAddress(), 
          dto.getPostDto().getPostName(), dto.getEmso(), 
          dto.getSchool(), dto.getPartClass(), dto.getJob(), dto.getPhone(), 
          dto.getMail(), dto.getClub_number(), dto.getCard_number(), 
          din, dto.getDesc(),dto.getFileextention()}), dto); 

      } 

我做错了什么?显示我的问题

两个图像:

  1. how looks first time when I load content in table
  2. broken image picture, when I scroll for example to the top, and then back to top

的Tomcat 8.0服务器上运行我的本地主机上的应用程序。

当我用Firebug检查Firefox中的图像时,它告诉我:无法加载图像。

不知怎的,当我滚动和表需要加载它含量的不同,图像会出现问题...

我怎么能解决这个问题?请帮我:-)

回答

0

SOLUTION:

通过Max Schuster in the Vaadin Forums回答:

我想你不能重用的InputStream。尝试将数据作为字节数组存储在您的DTO中,并在每次调用StreamSource#getStream时返回一个新的(ByteArray)InputSteam实例。

但是,这种方法会打你的内存很难,因为你必须保留在服务器内存中的所有图像。即使你不需要它们。

我建议您创建一个自定义的StreamSource实现,每次调用StreamSource#getStream时都会从数据库接收图像。因为如果clientbrowser询问它,您只需要图像数据。这样,如果图像数据不再需要,图像数据可以被垃圾收集。

+2

请考虑写出“一个合适”的答案(不只是一个链接,明天可能会消失) – cfrick

+0

当然,我编辑我的解决方案。 – slodeveloper