table vaadin 版本 7.6.3 中的图像损坏
Broken images in table vaadin version 7.6.3
当我滚动到 table 的底部并 table 加载它的内容时,我的图像(来自 StreamResource 的图像是 嵌入式,我将图像作为 blob 存储在数据库中)损坏了。
包含嵌入式图像的table的第一行显示得很好,但是当我向下滚动时,看起来很好的图片被破坏了。
getAllPartners 方法(这里我从数据库加载 blob 图像):
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;
}
我的 table (tPartners) 初始化:
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);
下面的代码显示了我如何 在 table 中加载图像 :
//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);
}
我做错了什么?
两张图片显示了我的问题:
- how looks first time when I load content in table
- broken image picture, when I scroll for example to the top, and then back to top
我是 运行 我在 Tomcat v8.0 服务器上的本地主机上的应用程序。
当我在 Firefox 中使用 Firebug 检查图像时,它告诉我:无法加载图像。
不知何故,当我滚动时,tables 需要加载它,图像会损坏...
我该如何解决这个问题?请帮助我:-)
解决方案:
回答者Max Schuster in the Vaadin Forums:
我认为您不能重复使用 InputStream。尝试将数据作为字节数组存储在 DTO 中,并且 return 每次调用 StreamSource#getStream 时都会有一个新的 (ByteArray)InputSteam 实例。
但是这种方法会严重影响您的 ram,因为您必须将所有图像保存在服务器内存中。即使你不需要它们。
我建议您创建一个自定义 StreamSource 实现,它在每次调用 StreamSource#getStream 时从数据库接收图像。因为如果客户端浏览器要求,您只需要图像数据。这样,如果不再需要图像数据,就可以将其回收。
当我滚动到 table 的底部并 table 加载它的内容时,我的图像(来自 StreamResource 的图像是 嵌入式,我将图像作为 blob 存储在数据库中)损坏了。
包含嵌入式图像的table的第一行显示得很好,但是当我向下滚动时,看起来很好的图片被破坏了。
getAllPartners 方法(这里我从数据库加载 blob 图像):
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;
}
我的 table (tPartners) 初始化:
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);
下面的代码显示了我如何 在 table 中加载图像 :
//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);
}
我做错了什么?
两张图片显示了我的问题:
- how looks first time when I load content in table
- broken image picture, when I scroll for example to the top, and then back to top
我是 运行 我在 Tomcat v8.0 服务器上的本地主机上的应用程序。
当我在 Firefox 中使用 Firebug 检查图像时,它告诉我:无法加载图像。
不知何故,当我滚动时,tables 需要加载它,图像会损坏...
我该如何解决这个问题?请帮助我:-)
解决方案:
回答者Max Schuster in the Vaadin Forums:
我认为您不能重复使用 InputStream。尝试将数据作为字节数组存储在 DTO 中,并且 return 每次调用 StreamSource#getStream 时都会有一个新的 (ByteArray)InputSteam 实例。
但是这种方法会严重影响您的 ram,因为您必须将所有图像保存在服务器内存中。即使你不需要它们。
我建议您创建一个自定义 StreamSource 实现,它在每次调用 StreamSource#getStream 时从数据库接收图像。因为如果客户端浏览器要求,您只需要图像数据。这样,如果不再需要图像数据,就可以将其回收。