dealfile.java

来自「在线考试功能」· Java 代码 · 共 94 行

JAVA
94
字号
package com.onlinestudy.service;

import java.io.File;
import java.util.Random;

import org.apache.struts.upload.FormFile;

public class DealFile {
    /**
     * 判断照片类型 .jpg .png .gif 目前只支持这三种格式
     * @param file
     * @return
     */
    public static boolean isPhoto(FormFile file) {
        String fileName = getString(file.getFileName());
        if (fileName.equals(""))
            return false;
        if ((fileName.toLowerCase().endsWith(".jpg"))
                || (fileName.toLowerCase().endsWith(".gif"))
                || (fileName.toLowerCase().endsWith(".png")))
            return true;
        else
            return false;
    }

    /**
     * 
     * @param str
     * @return
     */
    public static String getString(String str) {
        if (str == null)
            str = "";
        if (str.equals("null"))
            str = "";
        str = str.trim();
        return str;
    }

    /**
     * 判断文件是否存在
     * @param fileName
     * @param dir
     * @return
     */
    public static boolean isFileExist(String fileName, String dir) {
        File files = new File(dir + fileName);
        return (files.exists()) ? true : false;
    }

    /**
     * 重命名
     * @param fileName
     * @param dir
     */
    public static String rename(String fileName, String dir) {
      
        Random random = new Random();
        int add = random.nextInt(10000);
        String ret = add+fileName; 
        while (isFileExist(ret, dir)) {
            add = random.nextInt(10000);
            ret = add + fileName;
        }
       
        return ret;
    }

    public static boolean isGif(String file) {
        if (file.toLowerCase().endsWith(".gif")) {
            return true;
        } else {
            return false;
        }
    }

    public static boolean isJpg(String file) {
        if (file.toLowerCase().endsWith(".jpg")) {
            return true;
        } else {
            return false;
        }
    }

    public static boolean isPng(String file) {
        if (file.toLowerCase().endsWith(".png")) {
            return true;
        } else {
            return false;
        }
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?