ITeye博客

从oracle下载附件

dengkehai · 2008-05-19 11:19 · struts · Cache · SQL · Oracle · Markdown

public ActionForward viewFile(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws BaseException { String sano =request.getParameter("flag"); System.out.println("esno=" + sano); // 查找附件 FilelistDAO _filelistDAO = new FilelistDAO(); List filelistList = _filelistDAO.findByProperty("Stockapply", sano);

	// 有附件,则将附件填充到表单
	if (filelistList != null) {
		if (filelistList.size() > 0) {
			try {
			Filelist _fileList = (Filelist) filelistList.get(0);
			String fileType = _fileList.getFlfilename().trim();

			fileType = fileType.substring(fileType.indexOf(".") + 1);
			System.out.println("fileName=" + findFileType(fileType));
			response.setContentType(findFileType(fileType));
			response.setHeader("Content-Disposition",
					"attachment;filename=\""+ URLEncoder.encode(_fileList.getFlfilename().trim(),"UTF-8") + "\"");
			response.setHeader("Cache-Control",
					"must-revalidate, post-check=0, pre-check=0");
			response.setHeader("Pragma", "public");
			response.setDateHeader("Expires",
					(System.currentTimeMillis() + 1000));
			
				SerializableBlob s_blob = (SerializableBlob) _fileList
						.getFlfile();
				java.sql.Blob wrapblob = s_blob.getWrappedBlob();
				oracle.sql.BLOB blob = (oracle.sql.BLOB) wrapblob;
				InputStream bis = blob.getBinaryStream();
				OutputStream webOutStream = response.getOutputStream();
				byte[] l_buffer = new byte[1024];
				int l_nbytes = 0;
				while ((l_nbytes = bis.read(l_buffer)) != -1) {
					webOutStream.write(l_buffer, 0, l_nbytes);
				}
				bis.close();
				webOutStream.flush();
				webOutStream.close();
				webOutStream = null;

			} catch (IOException e) {
				e.printStackTrace();
			} catch (SQLException e) {
				e.printStackTrace();
			}

		}
	}
	return mapping.findForward("");
}

评论(0)

暂无评论。

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