123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183 |
- 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<String, String> FILE_TYPE_MAP = new HashMap<String, String>();
- static {
- getAllFileType(); // 初始化文件类型信息
- }
- /**
- * Created on 2013-1-21
- * <p>
- * 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<ImageReader> 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
- * <p>
- * Discription:[isImage,判断文件是否为图片]
- * </p>
- *
- * @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;
- }
- }
|