设为首页 加入收藏

TOP

mybatis oracle BLOB类型字段保存与读取(二)
2015-11-21 01:39:31 来源: 作者: 【 】 浏览:1
Tags:mybatis oracle BLOB 类型 保存 读取
Request request)
16 ? ? ? ? ? ? throws UnsupportedEncodingException {
17 ? ? ? ? Map result = new HashMap();
18?
19 ? ? ? ? MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
20 ? ? ? ? // 获得文件
21 ? ? ? ? MultipartFile multipartFile = multipartRequest.getFile("Filedata");// 与前端设置的fileDataName属性值一致
22 ? ? ? ? String filename = multipartFile.getOriginalFilename();// 文件名称
23 ? ? ? ? InputStream is = null;
24 ? ? ? ? try {
25 ? ? ? ? ? ? //读取文件流
26 ? ? ? ? ? ? is = multipartFile.getInputStream();
27 ? ? ? ? ? ? byte[] bytes = FileCopyUtils.copyToByteArray(is);
28 ? ? ? ? ? ? BlobField blobField = new BlobField();
29 ? ? ? ? ? ? blobField.setTabName("testL");
30 ? ? ? ? ? ? blobField.setTabPkidValue(testId);
31 ? ? ? ? ? ? blobField.setBlobColName("attachment");
32 ? ? ? ? ? ? blobField.setBlobColValue(bytes);
33 ? ? ? ? ? ? //保存blob字段
34 ? ? ? ? ? ? this.testService.save(blobField, testId, filename);
35 ? ? ? ? ? ? result.put("flag", true);
36 ? ? ? ? ? ? result.put("attachmentId", blobField.getId());
37 ? ? ? ? ? ? result.put("attachmentName", filename);
38 ? ? ? ? } catch (IOException e) {
39 ? ? ? ? ? ? e.printStackTrace();
40 ? ? ? ? ? ? result.put("flag", false);
41 ? ? ? ? } finally {
42 ? ? ? ? ? ? IOUtils.closeQuietly(is);
43 ? ? ? ? }
44 ? ? ? ? return result;
45 ? ? } ? ?
?
b、读取BLOB字段
?
?
?1 /**
?2 ? ? ?* 下载附件
?3 ? ? ?*?
?4 ? ? ?* @param attachmentId
?5 ? ? ?* @return
?6 ? ? ?*/
?7 ? ? @RequiresPermissions("exc:exceptioninfo:view")
?8 ? ? @RequestMapping(value = "download", method = RequestMethod.GET)
?9 ? ? public void download(@RequestParam(value = "attachmentId", required = true) String attachmentId,
10 ? ? ? ? ? ? @RequestParam(value = "attachmentName", required = true) String attachmentName, HttpServletRequest?
11?
12 request, HttpServletResponse response) {
13 ? ? ? ? ServletOutputStream out = null;
14 ? ? ? ? try {
15 ? ? ? ? ? ? response.reset();
16 ? ? ? ? ? ? String userAgent = request.getHeader("User-Agent");
17 ? ? ? ? ? ? byte[] bytes = userAgent.contains("MSIE") ? attachmentName.getBytes() : attachmentName.getBytes("UTF-
18?
19 8"); // fileName.getBytes("UTF-8")处理safari的乱码问题
20 ? ? ? ? ? ? String fileName = new String(bytes, "ISO-8859-1");
21 ? ? ? ? ? ? // 设置输出的格式
22 ? ? ? ? ? ? response.setContentType("multipart/form-data");
23 ? ? ? ? ? ? response.setHeader("Content-Disposition", "attachment;fileName=" + URLEncoder.encode(attachmentName,?
24?
25 "UTF-8"));
26 ? ? ? ? ? ? BlobField blobField = this.blobFieldService.get(attachmentId);
27 ? ? ? ? ? ? //获取blob字段
28 ? ? ? ? ? ? byte[] contents = blobField.getBlobColValue();
29 ? ? ? ? ? ? out = response.getOutputStream();
30 ? ? ? ? ? ? //写到输出流
31 ? ? ? ? ? ? out.write(contents);
32 ? ? ? ? ? ? out.flush();
33 ? ? ? ? } catch (IOException e) {
34 ? ? ? ? ? ? e.printStackTrace();
35 ? ? ? ? }
36 ? ? } ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
?
本例子将文件上传并保存到BLOB类型字段字段,下载的时候读取BLOB字段,并写入成输出流。
首页 上一页 1 2 下一页 尾页 2/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇rac_/home/oracle权限过大引发的.. 下一篇Dapper完美兼容Oracle 执行存储过..

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容: