blob文件的存儲和讀取


一、blob文件的存儲

1.實體類:

private byte[] newPhoto;//數據庫保存圖片

注意返回值類型為字節數組,數據庫類型為BLOB;

2.jsp頁面

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

二、blob文件從數據庫的讀取

1.jsp頁面

<img alt="照片" src="${ctx}/sys/user/upAndReadImg?id=${user.id}" width="120" height="120">

2.controller文件

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

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM