如何处理BLOB类型数据之二:下载BLOB内容并保存到文件中
来源:互联网 发布:淘宝添加自定义属性 编辑:程序博客网 时间:2024/05/24 06:30
实验环境:JDeveloper 11.1.2.0.0。
接上一个实验《如何处理BLOB类型数据之一:上传文件并保存到BLOB中》。
1. 修改页面代码,把Table中Filename字段改为CommandLink类型,这样点击文件名称即可下载该文件:
<af:commandLink text="#{row.Filename}" id="cl1" actionListener="#{myBackingBean.downloadLink_actionListener}"/>
2. 对应的Managed Bean代码
public void downloadLink_actionListener(ActionEvent actionEvent) {
FacesCtrlHierNodeBinding f = (FacesCtrlHierNodeBinding)this.richTable.getSelectedRowData();
Row row = f.getRow();
BlobDomain fileContent = (BlobDomain)row.getAttribute("Content");
String fileName = row.getAttribute("Filename").toString();
String fileType = "aplication/octet-stream";
if (fileName.endsWith(".pdf")) {
fileType = "application/PDF";
} else if (fileName.endsWith(".doc")) {
fileType = "aplication/msword";
} else if (fileName.endsWith(".txt")) {
fileType = "text/plain";
} else if (fileName.endsWith(".ppt")) {
fileType = "application/vnd.ms-powerpoint";
} else if (fileName.endsWith(".rar")) {
fileType = "aplication/octet-stream";
} else if (fileName.endsWith(".zip")) {
fileType = "aplication/zip";
} else if (fileName.endsWith(".jpg")) {
fileType = "aplication/jpg";
} else {
fileType = "aplication/octet-stream";
}
FacesContext facesContext = FacesContext.getCurrentInstance();
ExternalContext extContext = facesContext.getExternalContext();
Long length = fileContent.getLength();
HttpServletResponse response = (HttpServletResponse)extContext.getResponse();
response.setHeader("Content-Disposition", "attachment;filename=\"" + fileName + "\"");
response.setContentLength((int)length.intValue());
response.setBufferSize(1024);
response.setContentType(fileType);
try {
writeBlobDomainToOutputStream(fileContent,response.getOutputStream());
} catch (Exception e) {
e.printStackTrace();
}
facesContext.responseComplete();
}
3. 运行,点击文件名,出现下载提示。
Project 下载:UploadFileToBlob_DownloadBlobToFile.7z
问题1:重复下载同一文件时,会报出异常:java.net.ProtocolException: Didn't meet stated Content-Length, wrote: '0' bytes instead of stated: '364' bytes。
经过跟踪调试,发现是while ((bytesRead = in.read(buffer, 0, 8192)) != -1) 循环并没有进入。
感觉像是inputStream没有关闭,读指针还停留在上一次结束的位置,即文件尾。
因此,再次读取同一文件时,in.read() == -1,直接返回了。
解决办法: 增加blobDomain.closeInputStream();。
由于BlobDomain对象比较特殊,in.close();并没有关闭BlobDomain对象输入流,必须使用BlobDomain自身的方法closeInputStream()来关闭。
接上一个实验《如何处理BLOB类型数据之一:上传文件并保存到BLOB中》。
1. 修改页面代码,把Table中Filename字段改为CommandLink类型,这样点击文件名称即可下载该文件:
<af:commandLink text="#{row.Filename}" id="cl1" actionListener="#{myBackingBean.downloadLink_actionListener}"/>
2. 对应的Managed Bean代码
public void downloadLink_actionListener(ActionEvent actionEvent) {
FacesCtrlHierNodeBinding f = (FacesCtrlHierNodeBinding)this.richTable.getSelectedRowData();
Row row = f.getRow();
BlobDomain fileContent = (BlobDomain)row.getAttribute("Content");
String fileName = row.getAttribute("Filename").toString();
String fileType = "aplication/octet-stream";
if (fileName.endsWith(".pdf")) {
fileType = "application/PDF";
} else if (fileName.endsWith(".doc")) {
fileType = "aplication/msword";
} else if (fileName.endsWith(".txt")) {
fileType = "text/plain";
} else if (fileName.endsWith(".ppt")) {
fileType = "application/vnd.ms-powerpoint";
} else if (fileName.endsWith(".rar")) {
fileType = "aplication/octet-stream";
} else if (fileName.endsWith(".zip")) {
fileType = "aplication/zip";
} else if (fileName.endsWith(".jpg")) {
fileType = "aplication/jpg";
} else {
fileType = "aplication/octet-stream";
}
FacesContext facesContext = FacesContext.getCurrentInstance();
ExternalContext extContext = facesContext.getExternalContext();
Long length = fileContent.getLength();
HttpServletResponse response = (HttpServletResponse)extContext.getResponse();
response.setHeader("Content-Disposition", "attachment;filename=\"" + fileName + "\"");
response.setContentLength((int)length.intValue());
response.setBufferSize(1024);
response.setContentType(fileType);
try {
writeBlobDomainToOutputStream(fileContent,response.getOutputStream());
} catch (Exception e) {
e.printStackTrace();
}
facesContext.responseComplete();
}
3. 运行,点击文件名,出现下载提示。
Project 下载:UploadFileToBlob_DownloadBlobToFile.7z
问题1:重复下载同一文件时,会报出异常:java.net.ProtocolException: Didn't meet stated Content-Length, wrote: '0' bytes instead of stated: '364' bytes。
经过跟踪调试,发现是while ((bytesRead = in.read(buffer, 0, 8192)) != -1) 循环并没有进入。
感觉像是inputStream没有关闭,读指针还停留在上一次结束的位置,即文件尾。
因此,再次读取同一文件时,in.read() == -1,直接返回了。
解决办法: 增加blobDomain.closeInputStream();。
由于BlobDomain对象比较特殊,in.close();并没有关闭BlobDomain对象输入流,必须使用BlobDomain自身的方法closeInputStream()来关闭。
Project已被更新,下载文件地址和名称不变。
http://maping930883.blogspot.com/2011/08/adf096blobblob.html
0 0
- 如何处理BLOB类型数据之二:下载BLOB内容并保存到文件中
- 如何处理BLOB类型数据之一:上传文件并保存到BLOB中
- 下载blob并保存到文件
- 处理Blob类型数据,例如将对象保存到blob字段中
- 处理Blob类型数据:
- 如何处理BLOB类型数据之三:使用Servlet在页面上显示BLOB中的图片
- oracle 中 blob类型数据修改(从file保存到数据库,从java.sql.Blob 保存到oracle.sql.BLOB )
- jdbc处理Oracle中Blob类型数据
- MySQL中如何插入blob类型数据
- struts2文件下载(文件保存以BLOB类型在数据库中)
- 读取oracle blob字段内容并以文件形式下载
- Oracle处理Blob类型数据
- Matlab处理blob类型数据
- Kettle 生成的XML格式数据保存到Oracle 数据库的BLOB类型字段中
- 采用struts2+hibernate实现保存文件到Oracle数据库&&并附下载代码【导入即可用】;数据库中保存blob类型的二进制文件
- struts2中使用Blob类型处理图片上传保存在数据库中并在JSP页面中显示图片
- Blob对象判断是不是图片类型以及Blob数据下载
- 使用JDBC4.0处理Oracle中BLOB类型的数据
- Radar Installation
- UVA - 387 A Puzzling Problem(回溯)
- 刚开通博客来发个帖子安慰下自己 哈
- 递归——数字链
- java编写带过滤分页的通讯录
- 如何处理BLOB类型数据之二:下载BLOB内容并保存到文件中
- HDU 2015 偶数求和
- Nginx模块开发 —进程模型
- 如何处理BLOB类型数据之三:使用Servlet在页面上显示BLOB中的图片
- 我常去的一些ADF网站介绍
- 让你提前认识软件开发(48):集成测试
- POJ 2240 Arbitrage
- Farm Tour
- 如何在一个事务中插入一对多关系的多个EO对象?