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<String, String> IMG_FILE_TYPE_MAP = new HashMap<String, String>(); /** * @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<ImageReader> 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<Entry<String, String>> entryiterator = IMG_FILE_TYPE_MAP .entrySet().iterator(); while (entryiterator.hasNext()
- 粉丝: 2
- 资源: 5
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
- 基于matlab开发的根据rvm回归模型自己编的matlab程序.rar
- 高效C++学生成绩管理系统:教育技术+C++17编程+数据管理+教务自动化
- 基于matlab开发的Tipping的相关向量机RVM的回归MATLAB程序,有英文注释,可以运行.rar
- 一个点击正反转程序实例,可实现案件电机正反转
- 搜索链接淘特搜索引擎共享版-tot-search-engine.rar
- 第十八届全国大学生智能汽车竞赛 摄像头组/镜头组
- 基于matlab开发的AUV惯性导航系统matlab仿真程序,包括轨迹生成、gps和sins组合、gps和dvl组合.rar
- 基于SSM的“个性化电子相册”的设计与实现.zip
- 如何在撰写科研文献时,使用ai工具辅助去完成科研工作
- 吉林大学计组笔记 自用 基于b站翼云图灵的课.pdf