在Hibernate中怎么存放Word格式的文件,又如何把这类文件拿出按存放格式显示在网页上如在Action中怎么与存放Word格式文件的代码?
又如何取出按存放时的格式显示到网页上。
最好能给段代码。。
又如何取出按存放时的格式显示到网页上。
最好能给段代码。。
解决方案 »
- tomcat 限制 页面访问
- DATE-〉timestamp强制转型错误,求解决方法
- java写入word时怎样让字符串在word中换行?
- PermGen space——内存溢出问题
- 帮我看看这是什么错误啊?
- 用struts2的generator的id,出错
- java.mail You are not authorized to send mail
- 在配置apache2的httpd.conf,看资料说要把port改成8080,我怎么找不到PORT?
- 我用Resin服务器,浏览网页一段时间后,发现CPU占用100%!
- 用javabean做的数据库连接有时行,有时不行?
- 开发WEB报表,如果采用开源使用那个比较好!调查!!!
- 我只不过想迈出JSP第一步为什么不给个机会!ECLIPSE出错
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;
}上面几段代码,可以保存任何类型的文档,视频,图片等。
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>点击这个文件,就会自动弹出文件下框。
我Test下
读取的时候把blob变成流,页面上输出的时候response
type是
application/msword
看看5楼的代码
poi读取word
http://blog.csdn.net/hemingwang0902/archive/2009/07/26/4381598.aspx
显示有好多种方式呢,如果只是用浏览器显示,直接读取这个文件,设置 resposne 的 contentType,就可以按照你指定的方式显示咯。