package com.soft.swork.pub;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Collection;
import javax.swing.filechooser.FileNameExtensionFilter;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.ObjectUtils.Null;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGEncodeParam;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
/**
*
* @author zhangsong
*
*/
public class MyUtils {
private static final int BUFFER_SIZE = 16 * 1024;
//
// 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();
}
/**
* 上传文件
*
* @param uploadFileName
* 被上传的文件名称
* @param savePath
* 文件的保存路径
* @param uploadFile
* 被上传的文件
* @return newFileName
*/
public static String upload(String fileName,String studentId, String savePath, File uploadFile) {
String[] split = fileName.split("\\.");// 将文件名已.的形式拆分
String extendFile = "." + split[split.length - 1].toLowerCase(); // 获文件的有效后缀
String newFileName = studentId.trim() + extendFile ;
System.out.println(newFileName);
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);
}
}
/**
* 将字符串已多个分隔符拆分为数组,调用Apache commons lang 中的方法
*
* <pre>
* Example:
* String[] arr = StringUtils.split("a b,c d,e-f", " ,-");
* System.out.println(arr.length);//输出6
* </pre>
*
* @param str
* 目标字符串
* @param separatorChars
* 分隔符字符串
* @return 字符串数组
*/
public static String[] split(String str, String separatorChars) {
if(str == null && "".equals(str))
{
return null ;
}
return StringUtils.split(str, separatorChars);
}
/**
* 调用指定字段的setter方法
*
* <pre>
* Example:
* User user = new User();
* MyUtils.invokeSetMethod("userName", user, new Object[] {"张三"});
* </pre>
*
* @param fieldName
* 字段(属性)名称
* @param invokeObj
* 被调用方法的对象
* @param args
* 被调用方法的参数数组
* @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));// 组建setter方法
for (Method m : methods) {
String name = m.getName();
if (mname.equals(name)) {
// 处理Integer参数
// System.out.println(f.getType().getSimpleName());
if (f.getType().getSimpleName().equalsIgnoreCase("int") ) {
args = Integer.valueOf(args.toString());
}
if (f.getType().getSimpleName().equalsIgnoreCase("float") ) {
args = Float.valueOf(args.toString());
}
if (f.getType().getSimpleName().equalsIgnoreCase("Date") ) {
SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
try {
args = format.parse(args.toString()) ;
} catch (ParseException e) {
System.out.println("action发射时间错误!!");
e.printStackTrace();
}
}
// 处理Boolean参数
if (f.getType().getSimpleName().equalsIgnoreCase("boolean")) {
args = Boolean.valueOf(args.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();
}
break ; //当成功反射赋值时跳出内层循环
}
}
if(flag == true)
{
break ; //当成功反射赋值时跳出外层循环
}
}
}
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 file
* 上传的文件流
* @param height
* 最小的尺寸
* @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) { // 如果文件小于锁略图的尺寸则复制即可
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 enc