package com.caimei.utils; import org.apache.commons.io.FileUtils; import javax.imageio.ImageIO; import javax.imageio.ImageReader; import javax.imageio.stream.ImageInputStream; import java.awt.image.BufferedImage; import java.io.*; import java.util.HashMap; import java.util.Iterator; import java.util.Map; public class FileUtil { public final static Map FILE_TYPE_MAP = new HashMap(); static { getAllFileType(); // 初始化文件类型信息 } /** * Created on 2013-1-21 *

* Discription:[getAllFileType,常见文件头信息] * * @author:shaochangfu */ private static void getAllFileType() { FILE_TYPE_MAP.put("jpg", "FFD8FF"); // JPEG (jpg) FILE_TYPE_MAP.put("png", "89504E47");// PNG (png) FILE_TYPE_MAP.put("gif", "47494638");// GIF (gif) FILE_TYPE_MAP.put("bmp", "424D"); // Windows Bitmap (bmp) FILE_TYPE_MAP.put("zip", "504B0304");// zip 压缩文件 } public FileUtil() { } /** * 把from路径下的文件拷贝到to路径下 * * @param from * @param to * @throws Exception */ public static void copy(String from, String to) throws Exception { int BUFF_SIZE = 100000; byte[] buffer = new byte[BUFF_SIZE]; InputStream in = null; OutputStream out = null; try { in = new FileInputStream(from); out = new FileOutputStream(to); while (true) { synchronized (buffer) { int amountRead = in.read(buffer); if (amountRead == -1) { break; } out.write(buffer, 0, amountRead); } } } finally { if (in != null) { in.close(); } if (out != null) { out.close(); } } } /** * 把文件移动到to路径下 * * @param file * @param toPath * @throws IOException */ public static void moveFile(File file, String toPath, String newName) throws IOException { /* * file.renameTo(new File((new StringBuilder(String.valueOf(s))).append( * File.separator).append(file.getName()).toString())); */ FileUtils.copyFile( file, new File((new StringBuilder(String.valueOf(toPath))) .append(File.separator).append(newName).toString())); } /** * 按照图片命名规则生成一个不带后缀的图片名 * * @param originalFullName * @return * @throws Exception */ public static String getNewImageName(String originalFullName) throws Exception { /* * int dotIndex = originalFullName.lastIndexOf('.'); if (dotIndex == -1) * { dotIndex = 0; } */ String fileName = RandomCodeGenerator.generateCodeString(10); return (new StringBuilder(String.valueOf(fileName))).append( DateTimeUtil.getCurrentDateTime().replaceAll("-", "") .replaceAll(":", "").replaceAll(" ", "")).toString(); } /** * 删除path路径下的文件 * * @param path */ public static void deleteFile(String path) { File file = new File(path); if (file.exists()) file.delete(); } /** * 得到文件的真实后缀名 (文件种类) * * @param f * @return * @throws Exception */ public final static String getImageFileType(File f) throws Exception { if (isImage(f)) { try { if (f.length() > 5242880) { throw new Exception("图片过大"); } ImageInputStream iis = ImageIO.createImageInputStream(f); Iterator iter = ImageIO.getImageReaders(iis); if (!iter.hasNext()) { return null; } ImageReader reader = iter.next(); iis.close(); return reader.getFormatName(); } catch (Exception e) { e.printStackTrace(); throw new Exception("图片后缀名检验异常"); } } else { throw new Exception("图片后缀名检验异常"); } } /** * Created on 2013-1-21 *

* Discription:[isImage,判断文件是否为图片] *

* * @param file * @return true 是 | false 否 * @author:shaochangfu */ public static final boolean isImage(File file) { boolean flag = false; try { BufferedImage bufreader = ImageIO.read(file); int width = bufreader.getWidth(); int height = bufreader.getHeight(); if (width == 0 || height == 0) { flag = false; } else { flag = true; } } catch (IOException e) { flag = false; } catch (Exception e) { flag = false; } return flag; } }