바이너리 데이터 파일로 저장

최인효·2024년 3월 28일
0

java

목록 보기
3/4
post-thumbnail

바이너리 데이터를 파일로 변환.
파라미터로 받은 바이너리 문자열을 파일로 저장 하기 위해..
디코딩 후 원하는 경로에 저장.

import java.io.File;
import java.io.FileOutputStream;

import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.Base64;


...
	String byteData = "...바이너리문자열...";
	String byteURLDecode = URLDecoder.decode(byteData, StandardCharsets.UTF_8.toString()); // URLDecoder deocde
	byte[] bytes = Base64.getDecoder().decode(byteURLDecode);// Base64 decode
	writeToFile(filePath, bytes);
}

public void writeToFile(String filePath, byte[] bytes)
{
    if(pData == null) return;
    try{
    	File newFile = new File(filePath);
    	FileOutputStream lFileOutputStream = new FileOutputStream(newFile);
    	lFileOutputStream.write(bytes);
    	lFileOutputStream.close();
    }catch(Throwable e){
    	...
    }
}
                            

바이너리 파일을 외부에서 제공받은 경우 그에 맞게 디코딩을 해줘야합니다.
위 예시는 바이너리 파일을 Base64 인코딩 > URLEncoder 인코딩된 데이터 라고 가정 합니다.

0개의 댓글