Notice
Recent Posts
Recent Comments
Link
투케이2K
48. (spring/스프링) mysql blob (블랍) 이미지 (image) get 방식으로 조회 실시 - gradle , mybatis , application yml 본문
Spring
48. (spring/스프링) mysql blob (블랍) 이미지 (image) get 방식으로 조회 실시 - gradle , mybatis , application yml
투케이2K 2022. 6. 15. 11:00[개발 환경 설정]
개발 툴 : inteli j
개발 언어 : spring
[컨트롤러 : DB_Maria_Api_Controller : 방법 [1] 소스 코드]
// TODO [SEARCH FAST] : [테스트 BLOB 이미지 조회]
@GetMapping(value = "/TEST_BLOB_SELECT", produces = MediaType.IMAGE_JPEG_VALUE)
public @ResponseBody byte[] TEST_BLOB_SELECT(@RequestParam Map<String, String> param){ // [쿼리 파람 방식]
System.out.println("\n");
System.out.println("================================================");
System.out.println("[CLASS] : "+String.valueOf(CLASS_NAME));
System.out.println("[METHOD] : "+String.valueOf("TEST_BLOB_SELECT"));
System.out.println("[INPUT] : "+String.valueOf(param.toString()));
System.out.println("================================================");
System.out.println("\n");
/**
* // -----------------------------------------
* [호출 방법]
* // -----------------------------------------
* 1. 호출 방식 : GET
* // -----------------------------------------
* 2. 호출 방법 : http://localhost:7000/TEST_BLOB_SELECT?IDX=1
* // -----------------------------------------
* 3. 리턴 데이터 :
* TODO [BLOB 이미지 바이트 데이터]
* // -----------------------------------------
* */
// [인풋 데이터 널 체크 실시]
String IDX = String.valueOf(param.get("IDX"));
if (C_Util.stringNotNull(IDX) == true){
}
else {
System.out.println("\n");
System.out.println("================================================");
System.out.println("[CLASS] : "+String.valueOf(CLASS_NAME));
System.out.println("[METHOD] : "+String.valueOf("TEST_BLOB_SELECT"));
System.out.println("[ERROR] : "+String.valueOf("INPUT DATA IS NULL"));
System.out.println("================================================");
System.out.println("\n");
// TODO [리턴 데이터 반환]
return null;
}
// [service 호출 수행]
Map<String, Object> returnData = db_maria_api_service.TEST_BLOB_SELECT(IDX);
// [리턴 받은 map 데이터 널 체크 실시]
try {
if (returnData != null && returnData.size()>0){
System.out.println("\n");
System.out.println("================================================");
System.out.println("[CLASS] : "+String.valueOf(CLASS_NAME));
System.out.println("[METHOD] : "+String.valueOf("TEST_BLOB_SELECT"));
System.out.println("[TYPE] : "+String.valueOf(returnData.get("T_BLOB").getClass().getName()));
System.out.println("[RESULT] : "+String.valueOf(returnData.toString()));
System.out.println("================================================");
System.out.println("\n");
// [BLOB 데이터 확인]
Blob blob = (Blob) returnData.get("T_BLOB");
// [BLOB TO BYTE 변환 실시]
int blobLength = (int) blob.length();
byte[] blobAsBytes = blob.getBytes(1, blobLength);
blob.free();
/*
System.out.println("\n");
System.out.println("================================================");
System.out.println("[CLASS] : "+String.valueOf(CLASS_NAME));
System.out.println("[METHOD] : "+String.valueOf("TEST_BLOB_SELECT"));
System.out.println("[BLOB TO BYTE] : "+String.valueOf(Arrays.toString(blobAsBytes)));
System.out.println("================================================");
System.out.println("\n");
// */
// TODO [리턴 데이터 반환]
return blobAsBytes;
}
else {
System.out.println("\n");
System.out.println("================================================");
System.out.println("[CLASS] : "+String.valueOf(CLASS_NAME));
System.out.println("[METHOD] : "+String.valueOf("TEST_BLOB_SELECT"));
System.out.println("[ERROR] : "+String.valueOf("SELECT DATA IS NULl"));
System.out.println("================================================");
System.out.println("\n");
// TODO [리턴 데이터 반환]
return null;
}
}
catch (Exception e){
e.printStackTrace();
}
// TODO [리턴 데이터 반환]
return null;
}
[컨트롤러 : DB_Maria_Api_Controller : 방법 [2] 소스 코드]
// TODO [SEARCH FAST] : [테스트 BLOB 이미지 조회]
@GetMapping(value = "/TEST_BLOB_SELECT_2", produces = MediaType.IMAGE_JPEG_VALUE)
public ResponseEntity<byte[]> TEST_BLOB_SELECT_2(@RequestParam Map<String, String> param){ // [쿼리 파람 방식]
System.out.println("\n");
System.out.println("================================================");
System.out.println("[CLASS] : "+String.valueOf(CLASS_NAME));
System.out.println("[METHOD] : "+String.valueOf("TEST_BLOB_SELECT_2"));
System.out.println("[INPUT] : "+String.valueOf(param.toString()));
System.out.println("================================================");
System.out.println("\n");
/**
* // -----------------------------------------
* [호출 방법]
* // -----------------------------------------
* 1. 호출 방식 : GET
* // -----------------------------------------
* 2. 호출 방법 : http://localhost:7000/TEST_BLOB_SELECT_2?IDX=1
* // -----------------------------------------
* 3. 리턴 데이터 :
* TODO [BLOB 이미지 바이트 데이터 + http status 응답 코드]
* // -----------------------------------------
* */
// [인풋 데이터 널 체크 실시]
String IDX = String.valueOf(param.get("IDX"));
if (C_Util.stringNotNull(IDX) == true){
}
else {
System.out.println("\n");
System.out.println("================================================");
System.out.println("[CLASS] : "+String.valueOf(CLASS_NAME));
System.out.println("[METHOD] : "+String.valueOf("TEST_BLOB_SELECT_2"));
System.out.println("[ERROR] : "+String.valueOf("INPUT DATA IS NULL"));
System.out.println("================================================");
System.out.println("\n");
// TODO [리턴 데이터 및 응답 반환 : 400 에러]
return new ResponseEntity<byte[]>(HttpStatus.BAD_REQUEST);
}
// [service 호출 수행]
Map<String, Object> returnData = db_maria_api_service.TEST_BLOB_SELECT(IDX);
// [리턴 받은 map 데이터 널 체크 실시]
try {
if (returnData != null && returnData.size()>0){
System.out.println("\n");
System.out.println("================================================");
System.out.println("[CLASS] : "+String.valueOf(CLASS_NAME));
System.out.println("[METHOD] : "+String.valueOf("TEST_BLOB_SELECT_2"));
System.out.println("[TYPE] : "+String.valueOf(returnData.get("T_BLOB").getClass().getName()));
System.out.println("[RESULT] : "+String.valueOf(returnData.toString()));
System.out.println("================================================");
System.out.println("\n");
// [BLOB 데이터 확인]
Blob blob = (Blob) returnData.get("T_BLOB");
// [BLOB TO BYTE 변환 실시]
int blobLength = (int) blob.length();
byte[] blobAsBytes = blob.getBytes(1, blobLength);
blob.free();
/*
System.out.println("\n");
System.out.println("================================================");
System.out.println("[CLASS] : "+String.valueOf(CLASS_NAME));
System.out.println("[METHOD] : "+String.valueOf("TEST_BLOB_SELECT_2"));
System.out.println("[BLOB TO BYTE] : "+String.valueOf(Arrays.toString(blobAsBytes)));
System.out.println("================================================");
System.out.println("\n");
// */
// TODO [리턴 데이터 및 응답 반환 : 200 정상]
return new ResponseEntity<byte[]>(blobAsBytes, HttpStatus.OK);
}
else {
System.out.println("\n");
System.out.println("================================================");
System.out.println("[CLASS] : "+String.valueOf(CLASS_NAME));
System.out.println("[METHOD] : "+String.valueOf("TEST_BLOB_SELECT_2"));
System.out.println("[ERROR] : "+String.valueOf("SELECT DATA IS NULl"));
System.out.println("================================================");
System.out.println("\n");
// TODO [리턴 데이터 및 응답 반환 : 404 에러]
return new ResponseEntity<byte[]>(HttpStatus.NOT_FOUND);
}
}
catch (Exception e){
e.printStackTrace();
// TODO [리턴 데이터 및 응답 반환 : 500 에러]
return new ResponseEntity<byte[]>(HttpStatus.INTERNAL_SERVER_ERROR);
}
}
[서비스 : DB_Maria_Api_Service : 소스 코드]
// [SEARCH FAST] : [테스트 BLOB 이미지 조회]
public Map<String, Object> TEST_BLOB_SELECT(String IDX){
System.out.println("\n");
System.out.println("================================================");
System.out.println("[CLASS] : "+String.valueOf(CLASS_NAME));
System.out.println("[METHOD] : "+String.valueOf("TEST_BLOB_SELECT"));
System.out.println("[INPUT] : "+String.valueOf(IDX));
System.out.println("================================================");
System.out.println("\n");
// [mapper 인터페이스 호출 실시]
Map<String, Object> returnData = db_maria_api_mapper.TEST_BLOB_SELECT(IDX);
// [리턴 값 반환]
return returnData;
}
[매퍼 : DB_Maria_Api_Mapper : 소스 코드]
// TODO [SEARCH FAST] : [테스트 BLOB 이미지 조회]
Map<String, Object> TEST_BLOB_SELECT(String IDX);
[mybatis : DB_Maria_Api_Mybatis : 소스 코드]
<!--
// =========================================
// [SEARCH FAST] : [테스트 BLOB 이미지 조회]
// =========================================
// [select id] [mapper 인터페이스 지정 메소드] : TEST_BLOB_SELECT
// =========================================
-->
<select id="TEST_BLOB_SELECT" parameterType="hashmap" resultType="hashmap">
<![CDATA[
select T_BLOB
from TEST_BLOB
where T_IDX = #{IDX}
]]>
</select>
[DB Connection : application.yml : 소스 코드]
# [Maria DB]
maria:
driver-class-name: org.mariadb.jdbc.Driver
jdbc-url: jdbc:mariadb://3.54.138.43:3306/test?characterEncoding=UTF-8&serverTimezone=UTC&characterEncoding=utf8&noAccessToProcedureBodies=true
username: test
password: test@1234
hikari:
maximum-pool-size: 20
connection-timeout: 30000
[결과 출력]
반응형
'Spring' 카테고리의 다른 글
Comments