Oracle + Ibatis使用Blob二进制进行下载和使用文件流进行下载

网友投稿 328 2022-11-21

Oracle + Ibatis使用Blob二进制进行下载和使用文件流进行下载

数据库:使用BLOB存放二进制流,BLOB转换为java类型为byte[] DECLARE CNT INTEGER; BEGIN SELECT COUNT(0) INTO CNT FROM USER_ALL_TABLES WHERE TABLE_NAME = UPPER('G_ATTACH_BYTE'); IF CNT = 0 THEN EXECUTE IMMEDIATE 'CREATE TABLE G_ATTACH_BYTE( BYTE_ID NUMBER, ATTACH_ID NUMBER, ATTACH_BYTE BLOB )'; END IF; END; / COMMENT ON TABLE G_ATTACH_BYTE IS '文件存储表' / COMMENT ON COLUMN G_ATTACH_BYTE.BYTE_ID IS '主键' / COMMENT ON COLUMN G_ATTACH_BYTE.ATTACH_ID IS '附件Id' / COMMENT ON COLUMN G_ATTACH_BYTE.ATTACH_BYTE IS '二进制文件' / DECLARE CNT INTEGER; BEGIN SELECT COUNT(0) INTO CNT FROM USER_CONSTRAINTS WHERE CONSTRAINT_NAME = UPPER('G_ATTACH_BYTE_PK'); IF CNT = 0 THEN EXECUTE IMMEDIATE 'ALTER TABLE G_ATTACH_BYTE ADD CONSTRAINT G_ATTACH_BYTE_PK PRIMARY KEY(BYTE_ID)'; END IF; END; / 1.保存文件,和普通的一样,javaBean中一个字段类型为byte[] /** * @Description:保存一份数据备份到byte表 * @param attachId * @param filePath * @author luhao * @since:2019年5月29日 下午2:21:30 */ private void saveAttachByte(Integer attachId,String filePath) { byte[] bytes = null; try { bytes = IOUtil.toByteArray(new FileInputStream(filePath)); } catch (FileNotFoundException e) { logger.info(filePath + "未读取到文件!"); } catch (IOException e) { logger.info("IO错误:attachId = " + attachId + ",filePath = " + filePath); } AttachByte attachByte = new AttachByte(); attachByte.setAttachId(attachId); attachByte.setAttachByte(bytes); getDaoFacade().getAttachDao().saveAttachByte(attachByte); } SELECT SEQ_G_ATTACH_BYTE.NEXTVAL AS byteId FROM DUAL INSERT INTO G_ATTACH_BYTE ( BYTE_ID, ATTACH_ID, ATTACH_BYTE ) VALUES( #byteId#, #attachId#, #attachByte# ) public class AttachByte { /** 主键 */ private Integer byteId; /** 附件Id */ private Integer attachId; /** 二进制文件 */ private byte[] attachByte; } 二、读取和下载 /** * @Description:从数据库下载文件 * @param request * @author luhao * @since:2019年5月30日 上午9:50:03 */ private void downloadByDB(RequestContext request) { HttpServletResponse response = (HttpServletResponse) ContextUtils.getHttpContext().getResponse(); String fileId = request.getParameter("fileId"); Integer attachId = CastUtil.toInteger(fileId,0); ServerLocator locator = (ServerLocator) FrameworkUtil.getServiceLocator("AIMS"); AttachByteView attachByte = locator.getAttachService().getAttachByteByAttachId(attachId); if (attachByte != null && attachByte.getAttachByte() != null) { byte[] bytes = attachByte.getAttachByte(); String fileName = attachByte.getAttachName(); OutputStream outputStream = null; response.reset(); response.setContentType("application/x-msdownload"); response.setHeader("Content_Length",String.valueOf(bytes.length)); try { response.setHeader("Content-disposition", "attachment; filename=" + URLEncoder.encode(fileName, "UTF-8").replace('+', ' ')); outputStream = response.getOutputStream(); outputStream.write(bytes); outputStream.flush(); } catch (IOException e) { throw new RuntimeException(e.getMessage()); } finally { if(outputStream != null) { try { outputStream.close(); } catch (IOException e) { throw new RuntimeException(e.getMessage()); } } } } } 三、使用普通文件流下载,别人的方法参考 // 下载附件 private void download(RequestContext request) throws Exception { HttpServletResponse response = (HttpServletResponse) ContextUtils.getHttpContext().getResponse(); /** * update by zengshaoqi 2019/1/14 13:48 * TA0700 添加非明细导入excel动态下载 */ String downloadHandler = request.getParameter("downloadHandler"); if (CastUtil.toNotEmptyString(downloadHandler) != null) { downloadByHandler(request, response); return; } String fileId = request.getParameter("fileId"); String flag = request.getParameter("flag");// 是合作金融网点登记的附件1:是 String apply = request.getParameter("isApply"); String fileName = null; String filePath = null; try { ServerLocator locator = (ServerLocator) FrameworkUtil .getServiceLocator("AIMS"); if (ASS_FILE.equals(flag)) { AssociateBankAttach assFile = locator.getAssociateBankService() .queryAssociateBankAttachByFileId( CastUtil.toInteger(fileId)); fileName = assFile.getFileName(); filePath = assFile.getFilePath(); } //N0801 增加对正式附件的下载 start else if(ATTACH_FILE.equals(flag)) { Attach attach = locator.getAttachService().getAttachById(fileId); fileName = attach.getAttachName(); filePath = attach.getFilePath(); }else if(UM_FILE.equals(flag)) { UmFmAttach umFmAttach = locator.getUmFormService().queryUmFmAttachByFileId(CastUtil.toInteger(fileId)); fileName = umFmAttach.getFileTitle(); filePath = umFmAttach.getFileName(); } //N0801 增加对正式附件的下载 end else { if ("true".equals(apply)) { UmFmAttach file = locator .getUmFormService() .queryUmFmAttachByFileId(CastUtil.toInteger(fileId)); fileName = file.getFileTitle(); filePath = file.getFileName(); } else { AIMSFile aimsFile = locator.getCommonService() .queryAimsFileById(CastUtil.toInteger(fileId)); fileName = aimsFile.getFileName(); filePath = aimsFile.getFilePath(); } } } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e.getMessage()); } if (fileName != null && filePath != null) { InputStream inputStream = null; OutputStream outputStream = null; try { File file = new File(filePath); if (!file.exists()) { throw new RuntimeException("资料文件“" + fileName + "”不存在!"); } response.reset(); response.setContentType("application/x-msdownload"); response.setHeader( "Content-disposition", "attachment; filename=" + java.net.URLEncoder.encode(fileName, "UTF-8") .replace('+', ' ')); response.setHeader("Content_Length", String.valueOf(file.length())); inputStream = new FileInputStream(file); outputStream = response.getOutputStream(); byte[] buff = new byte[2048]; int bytesRead = 0; while ((bytesRead = inputStream.read(buff, 0, buff.length)) > 0) { outputStream.write(buff, 0, bytesRead); } outputStream.flush(); } catch (Exception e) { e.printStackTrace(); throw new RuntimeException(e.getMessage()); } finally { if (inputStream != null) try { inputStream.close(); } catch (IOException e) { e.printStackTrace(); throw new RuntimeException(e.getMessage()); } if (outputStream != null) try { outputStream.close(); } catch (IOException e) { e.printStackTrace(); throw new RuntimeException(e.getMessage()); } } } }

版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Java过滤器doFilter里chain.doFilter()函数的理解
下一篇:EAC-LCD70L接口定义和规格
相关文章

 发表评论

暂时没有评论,来抢沙发吧~