在Hibernate中怎么存放Word格式的文件,又如何把这类文件拿出按存放格式显示在网页上如在Action中怎么与存放Word格式文件的代码?
又如何取出按存放时的格式显示到网页上。
最好能给段代码。。

解决方案 »

  1.   

    一般不是都POI来处理word格式么
      

  2.   

    把word 当作 BLob类型存放就可以了。1。 你设计的这个保存的table要有个 blob类型的列2. 生成的java类和 hbm.xml片段如下:public class Document implements java.io.Serializable { private String resSkey;
    private Blob resFile;//这个就是blob类型
    private Date resUdate;。。<hibernate-mapping default-lazy="true" package="com.test.model">
        <class name="Document" table="Document">
            <id name="resSkey" type="string">
                <column name="RES_SKEY" length="10" />
                <generator class="assigned" />
            </id>
            <property name="resFile" type="blob">
                <column name="RES_FILE" />
            </property>
            <property name="resUdate" type="timestamp">
                <column name="RES_UDATE" />
            </property>
        </class>
    </hibernate-mapping>3. DAO层保存代码如下public void saveDocument(Document doc, byte[] data) {
    doc.setResFile(Hibernate.createBlob(data));
    Session session = SessionFactoryUtils.getSession(getSessionFactory(), false);
    session.save(doc);
    }byte[] date 就是你用文件流读好的文档,方法类型下面即可。private byte[] getByteData(File file) {
      byte[] data = (byte[])null;
      if (file == null) {
        return data;
      }  FileInputStream fin = null;
      try {
        fin = new FileInputStream(file);
        BufferedInputStream bfStream = new BufferedInputStream(fin);
        ByteArrayOutputStream byOutput = new ByteArrayOutputStream();    for (int by = bfStream.read(); by != -1; by = bfStream.read()) {
          byOutput.write(by);
        }
        data = byOutput.toByteArray();
        byOutput.close();
        bfStream.close();
      } catch (Exception e) {
        e.printStackTrace();
      } finally {
        try {
          fin.close();
        } catch (Exception localException2) {
        }
      }
      return data;
    }上面几段代码,可以保存任何类型的文档,视频,图片等。
      

  3.   

    算了,再给你写段读取的代码片段吧,我是集成spring来做,没有spring,这用 session也可以,记得用完关闭。1. DAO 层有个方法。 public Document getDocument(String skey) {
    return (Document)this.getHibernateTemplate().get(HrmDocument.class, skey);
    }2. service 层。public byte[] getDocument(String skey) {
      byte[] byDoc = (byte[])null;
      if (skey == null) {
        return byDoc;
      }  Document f = this.docFileDAO.getDocument(skey);
      try
      {
        if ((f != null) && (f.getResFile() != null)) {
          InputStream inputStream = f.getResFile().getBinaryStream();
          BufferedInputStream bufIS = new BufferedInputStream(inputStream);
          ByteArrayOutputStream byOutput = new ByteArrayOutputStream(1024);      for (int by = bufIS.read(); by != -1; by = bufIS.read()) {
            byOutput.write(by);
          }      byDoc = byOutput.toByteArray();
          byOutput.close();
          bufIS.close();
          inputStream.close();
        }
      } catch (SQLException e) {
        e.printStackTrace();
      } catch (IOException e) {
        e.printStackTrace();
      }
      return byDoc;
    }3. action层ServletOutputStream out = response.getOutputStream();
    response.setContentType("application/octet-stream");
    String fileName =this.resourceManager.getFileName(doc.getDocName());
    fileName = java.net.URLEncoder.encode(fileName, "utf-8");
    response.setHeader("Content-disposition", "attachment; filename="+fileName); // 上面是以下载框方式提供文件下载的。

    if(skey != null){
    byte[] buf = this.resourceManager.getDocument(skey);//就是根据主键,读取文件的service层方法。
    if(buf != null){

    ByteArrayInputStream bis = new ByteArrayInputStream(buf);
    BufferedInputStream bufIS = new BufferedInputStream(bis);
    BufferedOutputStream bos = new BufferedOutputStream(out);

    for(int b = bufIS.read(); b != -1; b = bufIS.read()){
    bos.write(b);
    }

    bos.flush();
    bufIS.close();
    bis.close();
    bos.close();
    }
    }
    out.flush();
    out.close();在页面上显示时,只要用 <a href="/getWordAction.do?读取文件的方法"/> 文件 </a>点击这个文件,就会自动弹出文件下框。
      

  4.   

    谢谢--youjianbo_han_87
    我Test下
      

  5.   

    存放的时候用blob
    读取的时候把blob变成流,页面上输出的时候response
    type是
    application/msword 
      

  6.   

    把word 当作 BLob类型存放就可以
    看看5楼的代码
      

  7.   

    http://download.csdn.net/source/1420353去下了看看一般用poi读取wordhttp://drivemewild.bokee.com/5606651.html
      

  8.   

    看这个
    poi读取word
    http://blog.csdn.net/hemingwang0902/archive/2009/07/26/4381598.aspx
      

  9.   


    显示有好多种方式呢,如果只是用浏览器显示,直接读取这个文件,设置 resposne 的 contentType,就可以按照你指定的方式显示咯。