반응형
// base64로 인코딩한 내용 받아오기
@RequestMapping(value = "/getBase64", method = RequestMethod.GET)
public @ResponseBody String getBase64(HttpServletRequest request, HttpServletResponse response) throws Exception {

    Base6430 base = new Base6430();
    String encodeBase64 = base.encodingBase64();

    return encodeBase64;

}

// 이미지 base64 인식
//	@RequestMapping(value = "/base64", method = RequestMethod.GET, produces = MediaType.IMAGE_JPEG_VALUE)
// mp4 base64 인식
//	@RequestMapping(value = "/base64", method = RequestMethod.GET, produces = "video/mp4")
// pdf base64 인식
//	@RequestMapping(value = "/base64", method = RequestMethod.GET, produces = "application/pdf")
// base64 디코딩해 해석해 화면에 노출
@RequestMapping(value = "/base64", method = RequestMethod.GET, produces = MediaType.IMAGE_JPEG_VALUE)
public @ResponseBody byte[] base64(HttpServletRequest request, HttpServletResponse response) throws Exception {

    String uri = "http://localhost:8080/getBase64";
    String encodeBase64 = callGetMethod(uri);

    byte decode[] = Base64.decodeBase64(encodeBase64);

    return decode;
}

StringBuffer response;
public String callGetMethod(String url) {		
    try {

        // 통신 주소  등록
        URL obj = new URL(url);

        // 통신 방식 설정
        HttpURLConnection conn = (HttpURLConnection) obj.openConnection();
        conn.setRequestProperty("Content-Type", "application/json; charset=utf-8" );
        conn.setDoOutput(true);
        conn.setRequestMethod("GET");

        // ---- 읽어온 데이터를 담기 ----
        BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream(),"UTF-8"));

        String inputLine;
        response = new StringBuffer();

        while ((inputLine = in.readLine()) != null) {
            response.append(inputLine);
        }
        in.close();

    } catch (Exception e) {
        e.printStackTrace();
    }

    // ---- 읽어온 데이터를 반환 ----
    String responseData =response.toString();
    return responseData; 
}

base64Controller

 

spring에서 제공하는 produces에 다양한 옵션이 존재하는데 ContentType 인식 시키는 방법에 대한 기술

(produces = MediaType.IMAGE_JPEG_VALUE → 이미지로 읽어라~)

 

package com.mycompany.myapp;

import java.io.File;
import java.io.FileInputStream;

import org.apache.commons.codec.binary.Base64;

public class Base6430 {

	public String encodingBase64() throws Exception {

		String strBase64 = "";
		String filePath = "D:\\dog.jpg";
//		String filePath = "D:\\test.mp4";
//		String filePath = "D:\\test.pdf";

		File f = new File(filePath);
		if (f.exists() && f.isFile() && f.length() > 0) {
			byte[] bt = new byte[(int) f.length()];
			FileInputStream fis = null;

			try {
				fis = new FileInputStream(f);
				fis.read(bt);
				strBase64 = new String(Base64.encodeBase64(bt));

			} catch (Exception e) {
				throw e;
			}
			fis.close();
			
		}
		return strBase64;
	}
}

base6430.java

 

해당 Path 정보를 읽어 Base64 인코딩하는 작업

 

<!-- https://mvnrepository.com/artifact/commons-codec/commons-codec -->
<dependency>
    <groupId>commons-codec</groupId>
    <artifactId>commons-codec</artifactId>
    <version>1.9</version>
</dependency>

pom.xml

반응형