六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 63|回复: 0

Java 下载Oracle中Blob数据

[复制链接]

升级  22%

21

主题

21

主题

21

主题

秀才

Rank: 2

积分
83
 楼主| 发表于 2013-1-14 08:40:42 | 显示全部楼层 |阅读模式
public int downloadFile(HttpServletRequst req, HttpServletResponse response )
throws Exception {
long infoPageAttachId = req.getParameter("infoPageAttachId", 0L);
if (infoPageAttachId == 0L) {
return RETCODE_ERR;
}
TInfoPageAttach ipAtt = getService(TInfoPageAttachService.class)
.getById(infoPageAttachId);

String filename = ipAtt.getFileName();
response.setContentType("application/x-download");
response.setCharacterEncoding("UTF-8");
response.addHeader("Content-Disposition", "attachment;filename="
+ URLEncoder.encode(filename, "UTF-8"));
Blob blob = ipAtt.getFileContent();

InputStream ins = blob.getBinaryStream();
// 用文件模拟输出流
OutputStream fout = response.getOutputStream();
try {
// 下面将BLOB数据写入文件
byte[] b = new byte[1024];
int len = 0;
while ((len = ins.read(b)) != -1) {
fout.write(b, 0, len);
fout.flush();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
fout.close();
ins.close();
}
return RETCODE_OK;
}
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表