【文件属性】:
文件名称:图片文件上次,获取图片文件实际类型
文件大小:6KB
文件格式:JAVA
更新时间:2023-01-02 15:20:38
java图片上传
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.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import javax.imageio.ImageIO;
import javax.imageio.ImageReader;
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();
/**
* @Description: 图片文件上传
* @author Xiao.Sky
* @creaetime 2015年4月17日下午5:20:27
* @param request
* @param response
* @param photo
* @param strtmp
* 文件名称 xxx.jpg
* @param path
* 文件路径
* @param num
* @return
*/
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 {
try {
if (null != bis) {
bis.close();
}
if (null != fis) {
fis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return true;
}
/**
*
* @Description: 普通文件上传
* @author Xiao.Sky
* @creaetime 2015年4月23日下午3:33:53
* @param request
* @param response
* @param photo
* @param strtmp
* @param path
* @param num
* @return
*/
public static boolean updateFile(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();
BufferedOutputStream bos = null;
BufferedInputStream bis = null;
FileInputStream fis = null;
try {
fis = new FileInputStream(photo);
long s = fis.available();
// 检查文件上传的大小,文件不能大于2MB 2097152
if (s > num) {
return false;
}
bis = new BufferedInputStream(fis);
FileOutputStream fos = new FileOutputStream(newFile);
bos = new BufferedOutputStream(fos);
byte[] buf = new byte[4096];
int len = -1;
while ((len = bis.read(buf)) != -1) {
bos.write(buf, 0, len);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (null != bos) {
bos.flush();
bos.close();
}
if (null != bis) {
bis.close();
}
if (null != fis) {
fis.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return true;
}
/**
* Discription:[getAllFileType,常见文件头信息]
*/
static{
IMG_FILE_TYPE_MAP.put("jpg", "FFD8FF"); // JPEG (jpg)
IMG_FILE_TYPE_MAP.put("jpeg", "FFD8FF");
IMG_FILE_TYPE_MAP.put("png", "89504E47");// PNG (png)
IMG_FILE_TYPE_MAP.put("bmp", "424D"); // Windows Bitmap (bmp)
}
/**
*
* @Description:getImageFileType,获取图片文件实际类型,若不是图片则返回null
* @author Tang.Homvee
* @creaetime 2015年8月21日下午5:43:53
* @param f
* @return
*/
public final static String getImageFileType(File f) {
if (isImage(f)) {
ImageInputStream iis = null;
try {
iis= ImageIO.createImageInputStream(f);
Iterator iter = ImageIO.getImageReaders(iis);
if (!iter.hasNext()) {
return null;
}
ImageReader reader = iter.next();
return reader.getFormatName();
} catch (IOException e) {
e.printStackTrace();
return null;
} catch (Exception e) {
e.printStackTrace();
return null;
}finally{
if(iis != null){
try {
iis.close();
} catch (IOException e) {
}
}
}
}
return null;
}
public final static String getFileByFile(File file) {
String filetype = null;
byte[] b = new byte[50];
InputStream is = null;
try {
is = new FileInputStream(file);
is.read(b);
filetype = getFileTypeByStream(b);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}finally{
if(is != null){
try {
is.close();
} catch (IOException e) {
}
}
}
return filetype;
}
public final static String getFileTypeByStream(byte[] b) {
String filetypeHex = String.valueOf(getFileHexString(b));
Iterator> entryiterator = IMG_FILE_TYPE_MAP
.entrySet().iterator();
while (entryiterator.hasNext()