本工具允许用户上传图片文件,并通过分析返回该图片的真实类型。帮助用户准确识别图片格式和属性。
package com.ylw.p2p.common.utils;
import java.awt.image.BufferedImage;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map.Entry;
import javax.imageio.ImageIO;
import javax.imageio.stream.ImageInputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class FileUtils {
public final static Map IMG_FILE_TYPE_MAP = new HashMap<>();
/**
* 图片文件上传
*
* @param request 请求对象
* @param response 响应对象
* @param photo 文件对象
* @param strtmp 文件名称,例如:xxx.jpg
* @param path 文件路径
* @param num 限制大小(字节)
*/
public static boolean updatePhoto(HttpServletRequest request, HttpServletResponse response, File photo, String strtmp,
String path, long num) {
File dir = new File(path);
if (!dir.exists()) {
dir.mkdirs();
}
File newFile = new File(dir, strtmp);
if (newFile.exists())
newFile.delete();
BufferedInputStream bis = null;
FileInputStream fis = null;
try {
fis = new FileInputStream(photo);
FileOutputStream fos = new FileOutputStream(newFile);
BufferedImage src = ImageIO.read(fis);
ImageIO.write(src, png, fos);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != bis)
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
if (null != fis)
try {
fis.close();
} catch (IOException e1) {
e1.printStackTrace();
}
}
return true;
}
/**
* 普通文件上传
*
* @param request 请求对象
* @param response 响应对象
* @param photo 文件对象
* @param strtmp 文件名称,例如:xxx.jpg
* @param path 文件路径
*/
public static boolean updateFile(HttpServletRequest request, HttpServletResponse response, File photo, String strtmp,
String path) {
File dir = new File(path);
if (!dir.exists()) {
dir.mkdirs();
}
File newFile = new File(dir, strtmp);
if (newFile.exists())
newFile.delete();
BufferedOutputStream bos = null;
BufferedInputStream bis = null;
FileInputStream fis = null;
try {
fis = new FileInputStream(photo);
long s = fis.available();
if (s > 2097152) { // 文件大小限制为2MB
return false;
}
bis = new BufferedInputStream(fis);
FileOutputStream fos = new FileOutputStream(newFile);
bos = new BufferedOutputStream(fos);
byte[] buf = new byte[4096];
int len;
while ((len = bis.read(buf)) != -1) {
bos.write(buf, 0, len);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
if (null != bos)
try {
bos.flush();
bos.close();
} catch (IOException e2) {
e2.printStackTrace();
}
if (null != bis)
try {
bis.close();
} catch (IOException e3) {
e3.printStackTrace();
}
if (null != fis)
try {
fis.close();
} catch (IOException e4) {
e4.printStackTrace();
}
}
return true;
}
/**
* 根据文件内容获取文件类型
*
* @param file 文件对象
*/
public final static String getFileByFile(File file) {
byte[] b = new byte[50];
InputStream is = null;
try {
is = new FileInputStream(file);
is.read(b);
return getFileTypeByStream(b);
} catch (FileNotFoundException e1) {
e1.printStackTrace();
} catch (IOException e2) {
e2.printStackTrace();
}
finally{
if(is != null)
try {
is.close();
} catch (IOException e5) {
}
}
return ;
}
}