Northwind数据库惹的祸

早上使用Northwind数据库做Struts2和Hibernate的测试数据库,使用Struts的Action输出Categories数据表的Picture字段的数据,也就是输出图片。

    public String execute()
    {
        
        ***.redev.business.Categories bizCate
          
= new ***.redev.business.Categories();

          ***.redev.orm.Categories cate
            
=  (***.redev.orm.Categories)
                    bizCate.Retrieve(
2);
          

          
          
byte[] buf  = cate.getPicture();
          
         HttpServletResponse response 
          
=  org.apache.struts2.ServletActionContext.getResponse();
          
             response.setContentType(
"image/jpeg");
             
             
              javax.servlet.ServletOutputStream  os 
=null;
              
try
              {
                  os 
= response.getOutputStream();
                  os.write(buf
);
                  os.close();
                  
              }
              
catch(java.io.IOException ex)
              {
                  ex.printStackTrace();
              }
              
return SUCCESS;
    }

一开始怎么输出图片都是那种 打红色叉号那种。 以为写法有问题,使用字符串转换为byte[]测试结果显示正常,忙活了一个早上,后来查网上有网友说:
如何取出NorthWind中Employees表中的Photo字段的图片并显示出来?
此表中图片显示常见的问题是:由于Northwind数据库內含的 image 资料最开头有78 bytes 的表头,所以需要手动将它去除。这也是大多数人费劲心思都无法显示那九个员的的图片的原因。
 这不是摆明坑人,欺骗老百姓嘛!估计Category表也应该是同样原因。

    public String execute()
    {
        
        ***.redev.business.Categories bizCate
          
= new ***.redev.business.Categories();

          ***.redev.orm.Categories cate
            
=  (***.redev.orm.Categories)
                    bizCate.Retrieve(
2);
          
          
          
int  offset = 78;
          
          
byte[] buf  = cate.getPicture();
          
         HttpServletResponse response 
          
=  org.apache.struts2.ServletActionContext.getResponse();
          
             response.setContentType(
"image/jpeg");
             
             
              javax.servlet.ServletOutputStream  os 
=null;
              
try
              {
                  os 
= response.getOutputStream();
                  os.write(buf, offset, buf.length
-offset);
                  os.close();
                  
              }
              
catch(java.io.IOException ex)
              {
                  ex.printStackTrace();
              }
              
return SUCCESS;
    }
 
果然是这个问题,数据流输出正常。

posted on 2008-05-22 16:54  Neo0820  阅读(360)  评论(0编辑  收藏  举报

导航