com.smart.common.FileHelper.java Source code

Java tutorial

Introduction

Here is the source code for com.smart.common.FileHelper.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.smart.common;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;
import org.apache.commons.codec.binary.Base64;

/**
 *
 * @author Administrator
 */
public class FileHelper {

    public static boolean ConvertBase64ToImage(String Base64param, String filePath) {
        if (Base64param == null) // ??
        {
            return false;
        }
        //BASE64Decoder decoder = new BASE64Decoder();
        try {
            byte[] data = Base64.decodeBase64(Base64param);
            try (OutputStream stream = new FileOutputStream(filePath)) {
                stream.write(data);
                stream.close();
            }
            return true;
        } catch (Exception e) {
            RSLogger.ErrorLogInfo(String.format("ConvertBase64ToImage error: %s", e.getLocalizedMessage()), e);
            return false;
        }

    }

    /**
     * ???
     *
     * @param filename
     * @return
     */
    public static String getExtensionName(String filename) {
        if ((filename != null) && (filename.length() > 0)) {
            int dot = filename.lastIndexOf('.');
            if ((dot > -1) && (dot < (filename.length() - 1))) {
                return filename.substring(dot + 1);
            }
        }
        return filename;
    }

    /**
     * ??
     *
     * @param sPath ?
     * @return ? true? false
     */
    public void DeleteFolder(String sPath) {
        File file = new File(sPath);
        // ?
        if (!file.exists()) { // ? false
            return;
        } else {
            // ?
            if (file.isFile()) { // 
                deleteFile(sPath);
            } else { // 
                deleteDirectory(sPath);
            }
        }
    }

    /**
     * ?
     *
     * @param sPath 
     * @return ?true?false
     */
    public boolean deleteDirectory(String sPath) {
        //sPath?
        if (!sPath.endsWith(File.separator)) {
            sPath = sPath + File.separator;
        }
        File dirFile = new File(sPath);
        //dir??
        if (!dirFile.exists() || !dirFile.isDirectory()) {
            return false;
        }
        //(?)
        File[] files = dirFile.listFiles();
        for (int i = 0; i < files.length; i++) {
            //?
            if (files[i].isFile()) {
                deleteFile(files[i].getAbsolutePath());
            } //?
            else {
                deleteDirectory(files[i].getAbsolutePath());
            }
        }
        //?
        if (dirFile.delete()) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * ?
     *
     * @param sPath ??
     * @return ??true?false
     */
    public static void deleteFile(String sPath) {
        File file = new File(sPath);
        // ?
        if (file.isFile() && file.exists()) {
            file.delete();
        }
    }

    /**
     * ?
     *
     * @param filePath 
     * @param isCreate ?
     * @return
     * @throws java.lang.Exception
     */
    public static boolean CheckFileExist(String filePath, boolean isCreate) throws Exception {
        try {
            File file = new File(filePath);
            if (!file.exists()) {
                if (isCreate) {
                    file.mkdir();
                }
                return false;
            } else {
                return true;
            }
        } catch (Exception e) {
            throw new Exception(e.getLocalizedMessage());
        }
    }

    /**
     * ?,?
     *
     * @param filePath 
     * @return
     * @throws Exception
     */
    public static boolean CheckFileExist(String filePath) throws Exception {
        return CheckFileExist(filePath, true);
    }

}