/** * */ package cn.edu.fudan.ora.utils; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.net.URLEncoder; import java.text.SimpleDateFormat; import java.util.Calendar; import java.util.Date; import java.util.Properties; import java.util.UUID; import javax.servlet.http.HttpServletResponse; import org.apache.struts2.ServletActionContext; //************************************************************************ //【文件名】 文件工具类 /** * <p>文件名:FileUtil.java</p>. * <p>说 明:文件常用方法实现</p> * * @author yuegy * @version 2011-2-28 */ //************************************************************************ //【作 成】 www.sh-db.com.cn 2011-2-28(R1.00) //************************************************************************ public class FileUtil { // 配置文件 private static final String CONFIG_FILE = "/WEB-INF/config/shyc.properties"; // 文件数 private static int fileCount = 0; /** * 删除指定文件. * @param fileName * @return */ public static boolean deleteFile(String fileName) { try { File file = new File(fileName); if (file.exists()) { file.delete(); } return true; } catch (Exception ex) { return false; } } /** * 删除文件夹下的所有文件. * @param folderFullPath * @return */ public static boolean deleteAllFile(String folderFullPath) { boolean ret = false; File file = new File(folderFullPath); if (file.exists()) { if (file.isDirectory()) { File[] fileList = file.listFiles(); for (int i = 0; i < fileList.length; i++) { String filePath = fileList[i].getPath(); deleteAllFile(filePath); } } if (file.isFile()) { try { file.delete(); } catch (Exception e) { } } } return ret; } /** * 创建文件夹. * @param folderName 要创建文件夹名称 */ public static void createFolder(String folderName) { try { File dirFile = new File(folderName); boolean bFile = dirFile.exists(); if (bFile == true) { } else { bFile = dirFile.mkdir(); if (bFile == true) { } else { } } } catch (Exception err) { } } /** * 获取当前路径. * @return */ public static String getContextPath() { return ServletActionContext.getServletContext().getContextPath(); } /** * 获取实际路径. * @param urlpath * @return */ public static String getRealPath(String urlpath) { String realPath = ServletActionContext.getServletContext().getRealPath(urlpath); return realPath; } /** * 存在目录. * @param path * @return */ public static boolean existsDir(String path) { File dir = new File(path); return dir.exists() && dir.isDirectory(); } /** * 存在文件. * @param path * @return */ public static boolean existsFile(String path) { File dir = new File(path); return dir.exists() && dir.isFile(); } /** * 取临时目录. * @param request * @return */ public static String getTempPath() { String propertyfile = getRealPath(CONFIG_FILE); InputStream in = null; String temppath = combinePath(System.getProperty("java.io.tmpdir"), "temppath"); try { in = new BufferedInputStream(new FileInputStream(propertyfile)); Properties p = new Properties(); try { p.load(in); if (p.containsKey("temppath")) { temppath = p.getProperty("temppath"); } } catch (IOException e) { } } catch (FileNotFoundException e1) { } String destPath = temppath;// getRealPath(Util.TEMPPATH); if (!existsDir(temppath)) { createFolder(temppath); } return destPath; } /** * 路径组合方法. * * @param path1 * @param path2 * @return */ public static String combinePath(String path1, String path2) { if (path1 == null || "".equals(path1)) { return path2; } if (path2 == null || "".equals(path2)) { return path1; } String path = null; if (path1.endsWith(File.separator) && !path2.startsWith(File.separator)) { path = path1 + path2; } else if (!path1.endsWith(File.separator) && path2.startsWith(File.separator)) { path = path1 + path2; } else if (!path1.endsWith(File.separator) && !path2.startsWith(File.separator)) { path = path1 + File.separator + path2; } else if (path1.endsWith(File.separator) && path2.startsWith(File.separator)) { path = path1 + path2.substring(1); } return path; } /** * 取得临时文件名,包含路径名. * @param request * @return */ public static String getTempFileName() { return getTempFileName(true); } /** * 删除临时文件. */ private static void deleteTempfile() { String folderFullPath = getTempPath(); File file = new File(folderFullPath); if (file.exists()) { if (file.isDirectory()) { Calendar calendar = Calendar.getInstance(); calendar.setTime(new Date()); calendar.add(Calendar.DAY_OF_MONTH, -1); SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd"); String dateString = formatter.format(calendar.getTime()); File[] fileList = file.listFiles(); for (int i = 0; i < fileList.length; i++) { String name = fileList[i].getName(); if (dateString.compareTo(name) > 0) { deleteFile(fileList[i].getPath()); } } } } } /** * 获取临时文件路径. * @param includePath * @return */ public static String getTempFileName(boolean includePath) { fileCount = (++fileCount % 100); if (fileCount == 0) { deleteTempfile(); } SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd"); String dateString = formatter.format(new Date()); String name = dateString + UUID.randomUUID().toString(); if (includePath) { String realPath = getTempPath(); name = combinePath(realPath, name); } return name; } /** * 取得文件名. * @param fullpath 完整路径名 * @return */ public static String getFilename(String fullpath) { if (StrUtil.isEmpty(fullpath)) { return ""; } int lastindex = fullpath.lastIndexOf(File.separator); if (lastindex == -1) { return ""; } String name = fullpath.substring(lastindex + 1); return name; } /** * 取得文件扩展名. * @param filename * @return */ public static String getExtFilename(String filename) { if (StrUtil.isEmpty(filename)) { return ""; } int inx = filename.lastIndexOf("."); if (inx == -1) { return ""; } else { String extname = filename.substring(inx + 1); return extname; } } /** * 取得图片文件类型. * @param filename * @return */ public static String getContentType(String filename) { String extname = getExtFilename(filename); extname = extname.toLowerCase(); if ("bmp".equals(extname)) { return "application/x-bmp"; } else if ("gif".equals(extname)) { return "image/gif"; } else if ("ico".equals(extname)) { return "image/x-icon"; } else if ("jfif".equals(extname)) { return "image/jpeg"; } else if ("jpe".equals(extname)) { return "image/jpeg"; } else if ("jpeg".equals(extname)) { return "image/jpeg"; } else if ("jpg".equals(extname)) { return "image/jpeg"; } else if ("png".equals(extname)) { return "image/png"; } else if ("tif".equals(extname)) { return "image/tiff"; } else if ("tiff".equals(extname)) { return "image/tiff"; } else { return ""; } } /** * 下载文件. * @param file * @param fileName * @param response * @return */ public static HttpServletResponse download(File file, String fileName, HttpServletResponse response) { try { FileInputStream fis = new FileInputStream(file); byte[] buffer = new byte[fis.available()]; fis.read(buffer); fis.close(); // 清空response response.reset(); // 设置response的Header response.addHeader("Content-Disposition", "attachment;filename=" + URLEncoder.encode(fileName, "UTF-8")); response.addHeader("Content-Length", "" + file.length()); OutputStream toClient = new BufferedOutputStream(response.getOutputStream()); response.setContentType("application/octet-stream"); toClient.write(buffer); toClient.flush(); toClient.close(); } catch (IOException ex) { ex.printStackTrace(); } return response; } }