FileUtils.java 1.8 KB
package com.framework.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class FileUtils {

	private static String[] imageType = {"jpg", "bmp", "gif", "png", "ico" };

	/** 截取文件后缀名 */
	public static String getSuffixName(String oriName) {
		oriName = oriName.substring(oriName.lastIndexOf("."), oriName.length());
		return oriName;
	}

	public static File genOutFile(File ori, int w, int h) {
		String oriName = ori.getName();
		if (oriName != null && oriName.length() > 0) {
			String name = oriName.substring(0, oriName.lastIndexOf('.'));
			String suffix = FileUtils.getSuffixName(oriName);
			return new File(ori.getParentFile(), name + "_" + w + "_" + h + suffix);
		}
		return null;
	}

	public static File genOutFile(File ori) {
		String oriName = ori.getName();
		if (oriName != null && oriName.length() > 0) {
			String name = oriName.substring(0, oriName.lastIndexOf('.'));
			String suffix = FileUtils.getSuffixName(oriName);
			return new File(ori.getParentFile(), name + suffix);
		}
		return null;
	}

	public static void copyFile(File src, File dst) throws IOException {
		InputStream in = new FileInputStream(src);
		OutputStream out = new FileOutputStream(dst);
		byte[] buf = new byte[1024];
		int len;
		while ((len = in.read(buf)) > 0) {
			out.write(buf, 0, len);
		}
		in.close();
		out.close();
	}

	public static boolean isImageFile(String fileName) {
		boolean f = false;
		String suffix = getSuffixName(fileName);
		suffix = suffix.toLowerCase();
		for (int i = 0; i < imageType.length; i++) {
			if (suffix.indexOf(imageType[i]) > 0) {
				f = true;
			}
		}
		return f;
	}
}