Newer
Older
NemophilaServer / src / main / java / org / ntlab / nemophila / utils / Base64Decode.java
package org.ntlab.nemophila.utils;

import java.io.*;
import java.util.Base64;

public class Base64Decode {
    private static void saveAsFile(String path, String enImage) throws IOException{
        byte[] deData = Base64.getDecoder().decode(enImage);

        File file = new File(path);

        if(enImage == null) {
            file.delete();
            return;
        }

        file.getParentFile().mkdir();
        file.createNewFile();

        FileOutputStream fileOS = new FileOutputStream(file, false);
        BufferedOutputStream bf = new BufferedOutputStream(fileOS);
        bf.write(deData, 0, deData.length);

        bf.close();
    }

    public static void saveIcon(String id, String path1, String enImage) throws IOException {
        String fileName = "icon" + id + ".jpg";
        String path = path1 + "/" + fileName;
        saveAsFile(path, enImage);

        System.out.println(path);
    }

    public static void savePostImage(String id, String pid, String path1, String[] enImage) throws IOException {
        for(int i = 0; i < enImage.length; i++) {
            String fileName = "post_image" + id + "-" + pid + "-" + (i+1) + ".jpg";
            String path = path1 + "/" + fileName;

            if(enImage[i] != null) {
                saveAsFile(path, enImage[i]);
            }

            System.out.println(path);
        }
    }
}