* Base64 Encode => Base64 Decode 하여 파일생성하기
Apache commons codec 에서 제공하는 Base64 En Decoding 사용했습니다.
-----------------------------------------------------------------------------
package com.test;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.apache.commons.codec.binary.Base64;
public class ImgTest {
public ImgTest(){
try {
File imgFile = new File("D:\\test.jpg");
// 이미지 파일을 byte[] 로 읽어온다.
FileInputStream fis = new FileInputStream(imgFile);
byte[] b = new byte[fis.available()];
fis.read(b);
// 읽어온 이미지 파일의 바이너리 데이터를 base64로 인코딩한다.
byte[] encoded = Base64.encodeBase64(b);
// byte[] 형태의 base64 데이터를 String으로 변환.
String base64Str = new String(encoded);
System.out.println(base64Str);
// 디코딩 작업.
byte[] decoded = Base64.decodeBase64(encoded);
File base64ToImgFile = new File("D:\\test2.jpg");
FileOutputStream fos = new FileOutputStream(base64ToImgFile);
fos.write(decoded);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally{
}
}
public static void main(String[] args) {
new ImgTest();
}
}
************************************
https://stackoverflow.com/questions/17506428/convert-base64-string-to-image-in-java
2018-09-16
2012-10-31
Java] InputStream 을 String으로 변환하기
InputStream 을 String으로 변환하는 방법
춮처 : http://www.kodejava.org/examples/266.html
package org.kodejava.example.io;
import java.io.InputStream;
import java.io.IOException;
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class StreamToString {
public static void main(String[] args) throws Exception {
StreamToString sts = new StreamToString();
/*
* Get input stream of our data file. This file can be in the root of
* you application folder or inside a jar file if the program is packed
* as a jar.
*/
InputStream is = sts.getClass().getResourceAsStream("/data.txt");
/*
* Call the method to convert the stream to string
*/
System.out.println(sts.convertStreamToString(is));
}
public String convertStreamToString(InputStream is) throws IOException {
/*
* To convert the InputStream to String we use the BufferedReader.readLine()
* method. We iterate until the BufferedReader return null which means
* there's no more data to read. Each line will appended to a StringBuilder
* and returned as String.
*/
if (is != null) {
StringBuilder sb = new StringBuilder();
String line;
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
while ((line = reader.readLine()) != null) {
sb.append(line).append("\n");
}
} finally {
is.close();
}
return sb.toString();
} else {
return "";
}
}
}
라벨:
자바,
inputStream,
java,
programing,
String
피드 구독하기:
글 (Atom)