工具类

package com.byd.core;

import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.Random;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;

import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;

public class MyUtils {

	public static void main(String[] s) {
		List conditions = new ArrayList();
		MyUtils.addToCollection(conditions, MyUtils.split("a,b-c d,e f-g",
				" ,-"));
		System.out.println(conditions);// output[a, b, c, d, e, f, g]
	}

	/**
	 * 
	 * 
	 * @param filePathAndName
	 *            String �ļ�·������� ��c:/fqf.txt
	 * @param fileContent
	 *            String
	 * @return boolean
	 */
	public static boolean delFile(String filePathAndName) {
		File myDelFile = new java.io.File(filePathAndName);
		if (!myDelFile.exists()) {
			return true;
		}
		return myDelFile.delete();
	}

	public static boolean delAllFile(String filepath) {
		File file = new java.io.File(filepath);
		if (!file.exists()) {
			return true;
		}
		if (!file.isDirectory()) {
			return true;
		}
		String[] tempList = file.list();
		File temp = null;
		for (int i = 0; i < tempList.length; i++) {
			if (filepath.endsWith(File.separator)) {
				temp = new File(filepath + tempList[i]);
			} else {
				temp = new File(filepath + File.separator + tempList[i]);
			}
			if (temp.isFile() && temp.canWrite())
				temp.delete();
		}
		return true;
	}

	/**
	 * �ϴ��ļ�
	 * 
	 * @param uploadFileName
	 *            ���ϴ����ļ����
	 * @param savePath
	 *            �ļ��ı���·��
	 * @param uploadFile
	 *            ���ϴ����ļ�
	 * @return newFileName
	 */
	public static String upload(String uploadFileName, String savePath,
			File uploadFile) {
		String newFileName = getRandomName(uploadFileName, savePath);
		try {
			FileOutputStream fos = new FileOutputStream(savePath + newFileName);
			FileInputStream fis = new FileInputStream(uploadFile);
			byte[] buffer = new byte[1024];
			int len = 0;
			while ((len = fis.read(buffer)) > 0) {
				fos.write(buffer, 0, len);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return newFileName;
	}

	/**
	 * ���·������һϵ�е�Ŀ¼
	 * 
	 * @param path
	 */
	public static void mkDirectory(String path) {
		File file;
		try {
			file = new File(path);
			if (!file.exists()) {
				file.mkdirs();
			}
		} catch (RuntimeException e) {
			e.printStackTrace();
		} finally {
			file = null;
		}
	}

	/**
	 * �����������ÿһ��Ԫ�طֱ���ӵ�ָ��������,����Apache commons collections �еķ���
	 * 
	 * @param collection
	 *            �϶���
	 * @param arr
	 *            ��������
	 */
	public static void addToCollection(Collection collection, Object[] arr) {
		if (null != collection && null != arr) {
			CollectionUtils.addAll(collection, arr);
		}
	}

	/**
	 * ���
	 * 
	 * <pre>
	 *    Example:
	 *     String[] arr = StringUtils.split(&quot;a b,c d,e-f&quot;, &quot; ,-&quot;);
	 *     System.out.println(arr.length);//���6
	 * </pre>
	 * 
	 * @param str ַ�
	 * @param separatorChars
	 *            �ָ���ַ�
	 * @return �ַ�����
	 */
	public static String[] split(String str, String separatorChars) {
		return StringUtils.split(str, separatorChars);
	}

	/**
	 * ����ָ���ֶε�setter����
	 * 
	 * <pre>
	 *    Example:
	 *    User user = new User();
	 *    MyUtils.invokeSetMethod(&quot;userName&quot;, user, new Object[] {&quot;����&quot;});
	 * </pre>
	 * 
	 * @param fieldName
	 *            �ֶ�(����)���
	 * @param invokeObj
	 *            �����÷����Ķ���
	 * @param args
	 *            �����÷����IJ�������
	 * @return �ɹ����
	 */
	public static boolean invokeSetMethod(String fieldName, Object invokeObj,
			Object[] args) {
		boolean flag = false;
		Field[] fields = invokeObj.getClass().getDeclaredFields(); // ��ö���ʵ���������ж�����ֶ�
		Method[] methods = invokeObj.getClass().getDeclaredMethods(); // ��ö���ʵ���������ж���ķ���
		for (Field f : fields) {
			String fname = f.getName();
			if (fname.equals(fieldName)) {// ֶ���
				String mname = "set"
						+ (fname.substring(0, 1).toUpperCase() + fname
								.substring(1));// 
				for (Method m : methods) {
					String name = m.getName();
					if (mname.equals(name)) {
						// ����Integer����
						if (f.getType().getSimpleName().equalsIgnoreCase(
								"integer")
								&& args.length > 0) {
							args[0] = Integer.valueOf(args[0].toString());
						}
						// ����Boolean����
						if (f.getType().getSimpleName().equalsIgnoreCase(
								"boolean")
								&& args.length > 0) {
							args[0] = Boolean.valueOf(args[0].toString());
						}
						try {
							m.invoke(invokeObj, args);
							flag = true;
						} catch (IllegalArgumentException e) {
							flag = false;
							e.printStackTrace();
						} catch (IllegalAccessException e) {
							flag = false;
							e.printStackTrace();
						} catch (InvocationTargetException e) {
							flag = false;
							e.printStackTrace();
						}
					}
				}
			}
		}
		return flag;
	}

	/**
	 * �ж��ļ��Ƿ����
	 * 
	 * @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
	 * @return
	 */
	public static String getRandomName(String fileName, String dir) {
		String[] split = fileName.split("\\.");// ���ļ�����.����ʽ���
		String extendFile = "." + split[split.length - 1].toLowerCase(); // ���ļ�����Ч��׺

		Random random = new Random();
		int add = random.nextInt(1000000); // ���������10000����
		String ret = add + extendFile;
		while (isFileExist(ret, dir)) {
			add = random.nextInt(1000000);
			ret = fileName + add + extendFile;
		}
		return ret;
	}

	/**
	 * ��������ͼ
	 * 
	 * @param file
	 *            �ϴ����ļ���
	 * @param height
	 *            ��С�ijߴ�
	 * @throws IOException
	 */
	public static void createMiniPic(File file, float width, float height)
			throws IOException {
		Image src = javax.imageio.ImageIO.read(file); // ����Image����
		int old_w = src.getWidth(null); // �õ�Դͼ��
		int old_h = src.getHeight(null);
		int new_w = 0;
		int new_h = 0; // �õ�Դͼ��
		float tempdouble;
		if (old_w >= old_h) {
			tempdouble = old_w / width;
		} else {
			tempdouble = old_h / height;
		}

		if (old_w >= width || old_h >= height) { // ����ļ�С������ͼ�ijߴ����Ƽ���
			new_w = Math.round(old_w / tempdouble);
			new_h = Math.round(old_h / tempdouble);// ������ͼ����
			while (new_w > width && new_h > height) {
				if (new_w > width) {
					tempdouble = new_w / width;
					new_w = Math.round(new_w / tempdouble);
					new_h = Math.round(new_h / tempdouble);
				}
				if (new_h > height) {
					tempdouble = new_h / height;
					new_w = Math.round(new_w / tempdouble);
					new_h = Math.round(new_h / tempdouble);
				}
			}
			BufferedImage tag = new BufferedImage(new_w, new_h,
					BufferedImage.TYPE_INT_RGB);
			tag.getGraphics().drawImage(src, 0, 0, new_w, new_h, null); // ������С���ͼ
			FileOutputStream newimage = new FileOutputStream(file); // ����ļ���
			JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(newimage);
			JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(tag);
			param.setQuality((float) (100 / 100.0), true);// ����ͼ
			encoder.encode(tag, param);
			encoder.encode(tag); // ��JPEG����
			newimage.close();
		}
	}

	/**
	 * �ж��ļ������Ƿ��ǺϷ���,�����ж�allowTypes���Ƿ��contentType
	 * 
	 * @param contentType
	 *            �ļ�����
	 * @param allowTypes
	 *            �ļ������б�
	 * @return �Ƿ�Ϸ�
	 */
	public static boolean isValid(String contentType, String[] allowTypes) {
		if (null == contentType || "".equals(contentType)) {
			return false;
		}
		for (String type : allowTypes) {
			if (contentType.equals(type)) {
				return true;
			}
		}
		return false;
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值