티스토리 뷰

728x90
반응형

 

 

1. 저장

save.jsp

<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
<script type="text/javascript">
	function toBase64(event) {
		var reader = new FileReader();
		reader.readAsDataURL(event.target.files[0]);
		reader.onload = function(event) {
			var base64 = event.target.result;// img -> base64
		};
	}
</script>

</head>
<body>
	<input type="file"onchange="toBase64(event);" />
</body>
</html>

controller.java 

@RequestMapping(value = "/save.do", method = RequestMethod.POST, produces = "application/text; charset=UTF-8")
@ResponseBody
public String save(@RequestParam Map<String, Object> paramMap) throws Exception {
	
	Map<String,Object> map = new HashMap<String, Object>();
    
	String base64 = paramMap.get("base64").toString();
    
	btye[] bytes = base64.getBytes();
    
	map.put("bytes",bytes);
    
	Service.saveImg(map);
    
	return "";
}

 

sql.xml

<insert id="save" parameterType="java.util.Map">
		 INSERT INTO IMG (
	 		IMG	        
        ) VALUES (
	        #{bytes, jdbcType=BLOB, javaType=[B}	        
        )
</insert>

 

 

 

2. 불러오기

 

sql.xml

<resultMap type="java.util.Map" id="imgMap">
	<result property="base64" column="IMG" javaType="[B" jdbcType="BLOB"/>
</resultMap>
<select id="selectImg" resultMap="imgMap">
	SELECT IMG FROM TB_IMG
</select>

 

controller.java

@RequestMapping(value="Img.do")
public String Img(@RequestParam Map<String,Object> commandMap, HttpServletRequest request,HttpServletResponse response, ModelMap model) throws Exception {
	
    Map<String,Object> resultMap = Service.selectImg(); 
	
    byte[] arr = (byte[]) resultMap.get("base64");
    String base64ToString = new String(arr);
	
    model.addAttribute("imgSrc",base64ToString);
    
    return "view";
}

 

view.jsp

<body>
	<img src="${imgSrc}" />
</body>

 

 

728x90
반응형
댓글

"이 블로그의 모든 포스팅은 쿠팡 파트너스 활동의 일환으로, 이에 따른 일정액의 수수료를 제공받습니다."

반응형
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday