在Hibernate中怎么存放Wordcl格式的文件

xuexijava 2009-11-13 11:55:27
在Hibernate中怎么存放Word格式的文件,又如何把这类文件拿出按存放格式显示在网页上

如在Action中怎么与存放Word格式文件的代码?
又如何取出按存放时的格式显示到网页上。。。。
最好能给段代码。。。。。
...全文
119 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
youjianbo_han_87 2009-11-16
  • 打赏
  • 举报
回复
[Quote=引用 7 楼 xuexijava 的回复:]
谢谢--youjianbo_han_87
我Test下。。。
[/Quote]

显示有好多种方式呢,如果只是用浏览器显示,直接读取这个文件,设置 resposne 的 contentType,就可以按照你指定的方式显示咯。
zl3450341 2009-11-15
  • 打赏
  • 举报
回复
zl3450341 2009-11-15
  • 打赏
  • 举报
回复
zl3450341 2009-11-15
  • 打赏
  • 举报
回复
把word 当作 BLob类型存放就可以

看看5楼的代码
blliy117 2009-11-15
  • 打赏
  • 举报
回复
存放的时候用blob
读取的时候把blob变成流,页面上输出的时候

response
type是
application/msword
xuexijava 2009-11-15
  • 打赏
  • 举报
回复
谢谢--youjianbo_han_87
我Test下。。。
youjianbo_han_87 2009-11-15
  • 打赏
  • 举报
回复
算了,再给你写段读取的代码片段吧,我是集成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>

点击这个文件,就会自动弹出文件下框。
youjianbo_han_87 2009-11-15
  • 打赏
  • 举报
回复
把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;
}



上面几段代码,可以保存任何类型的文档,视频,图片等。
xuexijava 2009-11-15
  • 打赏
  • 举报
回复
加分了,高手来看看呗。。。。
xuexijava 2009-11-14
  • 打赏
  • 举报
回复
自己顶一个
xuexijava 2009-11-14
  • 打赏
  • 举报
回复
再顶。。。
SUPERTIC 2009-11-13
  • 打赏
  • 举报
回复
一般不是都POI来处理word格式么

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧