ITeye博客

Java 下载Oracle中Blob数据

sam_chen · 2012-11-20 11:01 · Java · 编程语言 · Markdown

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;
		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;
}

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。