admin
2025-02-25 30d8e227e8d823b6c38c3b9c90ac2df03b63befe
fanli/src/main/java/com/yeshi/fanli/util/FileUtil.java
@@ -1,82 +1,82 @@
package com.yeshi.fanli.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.io.IOUtils;
public class FileUtil {
   @SuppressWarnings("deprecation")
   public static String getRealPath(HttpServletRequest request, String folderName) {
      String f = request.getRealPath(folderName);
      if (!new File(f).exists())
         new File(f).mkdirs();
      return f;
   }
   public static String getFileMD5(File file) throws IOException {
      FileInputStream fis = new FileInputStream(file);
      String md5 = DigestUtils.md5Hex(IOUtils.toByteArray(fis));
      IOUtils.closeQuietly(fis);
      return md5;
   }
   public static void deleteFileDir(File path) {
      if (!path.exists())
         return;
      if (path.isFile()) {
         path.delete();
         return;
      }
      File[] files = path.listFiles();
      for (int i = 0; i < files.length; i++) {
         deleteFileDir(files[i]);
      }
      path.delete();
   }
   public static String saveAsFile(InputStream inputStream, String path) throws IOException {
      if (!new File(path).exists()) {
         new File(path).createNewFile();
      }
      FileOutputStream fileOut = new FileOutputStream(new File(path));
      byte[] buf = new byte[1024 * 8];
      while (true) {
         int read = 0;
         if (inputStream != null) {
            read = inputStream.read(buf);
         }
         if (read == -1) {
            break;
         }
         fileOut.write(buf, 0, read);
      }
      fileOut.close();
      return path;
   }
   public static String getCacheDir() {
      String os = System.getProperty("os.name");
      if (os.toLowerCase().startsWith("win")) {
         File f = new File("D:/cache");
         if (!f.exists())
            f.mkdirs();
         return f.getPath();
      } else {
         File f = new File("/usr/local/cache");
         if (!f.exists())
            f.mkdirs();
         return f.getPath();
      }
   }
}
package com.yeshi.fanli.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.servlet.http.HttpServletRequest;
import org.apache.commons.codec.digest.DigestUtils;
import org.apache.commons.io.IOUtils;
public class FileUtil {
   @SuppressWarnings("deprecation")
   public static String getRealPath(HttpServletRequest request, String folderName) {
      String f = request.getRealPath(folderName);
      if (!new File(f).exists())
         new File(f).mkdirs();
      return f;
   }
   public static String getFileMD5(File file) throws IOException {
      FileInputStream fis = new FileInputStream(file);
      String md5 = DigestUtils.md5Hex(IOUtils.toByteArray(fis));
      IOUtils.closeQuietly(fis);
      return md5;
   }
   public static void deleteFileDir(File path) {
      if (!path.exists())
         return;
      if (path.isFile()) {
         path.delete();
         return;
      }
      File[] files = path.listFiles();
      for (int i = 0; i < files.length; i++) {
         deleteFileDir(files[i]);
      }
      path.delete();
   }
   public static String saveAsFile(InputStream inputStream, String path) throws IOException {
      if (!new File(path).exists()) {
         new File(path).createNewFile();
      }
      FileOutputStream fileOut = new FileOutputStream(new File(path));
      byte[] buf = new byte[1024 * 8];
      while (true) {
         int read = 0;
         if (inputStream != null) {
            read = inputStream.read(buf);
         }
         if (read == -1) {
            break;
         }
         fileOut.write(buf, 0, read);
      }
      fileOut.close();
      return path;
   }
   public static String getCacheDir() {
      String os = System.getProperty("os.name");
      if (os.toLowerCase().startsWith("win")) {
         File f = new File("C:/cache");
         if (!f.exists())
            f.mkdirs();
         return f.getPath();
      } else {
         File f = new File("/usr/local/cache");
         if (!f.exists())
            f.mkdirs();
         return f.getPath();
      }
   }
}