1、blob文件的存儲javascript
1.實體類:html
private byte[] newPhoto;//數據庫保存圖片
注意返回值類型爲字節數組,數據庫類型爲BLOB;前端
2.jsp頁面java
<form:input path="filePhoto" htmlEscape="false" type="file"/>
3.controller文件數據庫
MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
MultipartFile multipartFile = multipartRequest.getFile("filePhoto");// 與前端設置的fileDataName屬性值一致 String filename = multipartFile.getOriginalFilename();// 文件名稱 InputStream is = null; try { //讀取文件流 is = multipartFile.getInputStream(); byte[] bytes = FileCopyUtils.copyToByteArray(is); user.setNewPhoto(bytes); } catch (Exception e) { // TODO: handle exception }
2、blob文件從數據庫的讀取數組
1.jsp頁面jsp
<img alt="照片" src="${ctx}/sys/user/upAndReadImg?id=${user.id}" width="120" height="120">
2.controller文件ui
public String upAndReadImg(@RequestParam(value="id",required=true)String id,HttpServletResponse response){ User user=UserUtils.get(id); if(user != null){ byte[] bytes=user.getNewPhoto(); ServletOutputStream out=null; try { if (bytes != null && bytes.length > 0) { out=response.getOutputStream(); out.write(bytes); out.flush(); } } catch (IOException e) { e.printStackTrace(); } } return null; }