zoukankan      html  css  js  c++  java
  • 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;
        }
     
    果然是这个问题,数据流输出正常。
  • 相关阅读:
    设计模式--抽象工厂(个人笔记)
    C#中Trim()、TrimStart()、TrimEnd()的用法
    C#中String类的几个方法(IndexOf、LastIndexOf、Substring)
    枚举、字符串、值之间的转换
    C# 获取文件名、目录、后缀、无后缀文件名、扩展名、根目录等
    向服务器发送Post或Get请求(封装好的)
    未能加载文件或程序集“AspNetPager”或它的某一个依赖项。参数错误。 (异常来自 HRESULT:0x80070057 (E_INVALIDARG))
    Hibernate实现向数据库插入一条数据全过程(Study By Example)
    es6 模块化
    css3 属性认识
  • 原文地址:https://www.cnblogs.com/Bruce_H21/p/1205045.html
Copyright © 2011-2022 走看看