Pre Merge pull request !73 from opoa/N/A

This commit is contained in:
opoa 2021-06-16 09:13:33 +00:00 committed by Gitee
commit e111f138ab
1 changed files with 259 additions and 260 deletions

View File

@ -1,260 +1,259 @@
package com.ruoyi.common.core.utils.file; package com.ruoyi.common.core.utils.file;
import java.io.File; import java.io.File;
import java.io.FileInputStream; import java.io.FileInputStream;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream; import java.io.OutputStream;
import java.io.UnsupportedEncodingException; import java.io.UnsupportedEncodingException;
import java.net.URLEncoder; import java.net.URLEncoder;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.ArrayUtils; import org.apache.commons.lang3.ArrayUtils;
import com.ruoyi.common.core.utils.StringUtils; import com.ruoyi.common.core.utils.StringUtils;
/** /**
* 文件处理工具类 * 文件处理工具类
* *
* @author ruoyi * @author ruoyi
*/ */
public class FileUtils public class FileUtils
{ {
/** 字符常量:斜杠 {@code '/'} */ /** 字符常量:斜杠 {@code '/'} */
public static final char SLASH = '/'; public static final char SLASH = '/';
/** 字符常量:反斜杠 {@code '\\'} */ /** 字符常量:反斜杠 {@code '\\'} */
public static final char BACKSLASH = '\\'; public static final char BACKSLASH = '\\';
public static String FILENAME_PATTERN = "[a-zA-Z0-9_\\-\\|\\.\\u4e00-\\u9fa5]+"; public static String FILENAME_PATTERN = "[a-zA-Z0-9_\\-\\|\\.\\u4e00-\\u9fa5]+";
/** /**
* 输出指定文件的byte数组 * 输出指定文件的byte数组
* *
* @param filePath 文件路径 * @param filePath 文件路径
* @param os 输出流 * @param os 输出流
* @return * @return
*/ */
public static void writeBytes(String filePath, OutputStream os) throws IOException public static void writeBytes(String filePath, OutputStream os) throws IOException
{ {
FileInputStream fis = null; FileInputStream fis = null;
try try
{ {
File file = new File(filePath); File file = new File(filePath);
if (!file.exists()) if (!file.exists())
{ {
throw new FileNotFoundException(filePath); throw new FileNotFoundException(filePath);
} }
fis = new FileInputStream(file); fis = new FileInputStream(file);
byte[] b = new byte[1024]; byte[] b = new byte[1024];
int length; int length;
while ((length = fis.read(b)) > 0) while ((length = fis.read(b)) > 0)
{ {
os.write(b, 0, length); os.write(b, 0, length);
} }
} }
catch (IOException e) catch (IOException e)
{ {
throw e; throw e;
} }
finally finally
{ {
if (os != null) if (os != null)
{ {
try try
{ {
os.close(); os.close();
} }
catch (IOException e1) catch (IOException e1)
{ {
e1.printStackTrace(); e1.printStackTrace();
} }
} }
if (fis != null) if (fis != null)
{ {
try try
{ {
fis.close(); fis.close();
} }
catch (IOException e1) catch (IOException e1)
{ {
e1.printStackTrace(); e1.printStackTrace();
} }
} }
} }
} }
/** /**
* 删除文件 * 删除文件
* *
* @param filePath 文件 * @param filePath 文件
* @return * @return
*/ */
public static boolean deleteFile(String filePath) public static boolean deleteFile(String filePath)
{ {
boolean flag = false; boolean flag = false;
File file = new File(filePath); File file = new File(filePath);
// 路径为文件且不为空则进行删除 // 路径为文件且不为空则进行删除
if (file.isFile() && file.exists()) if (file.isFile() && file.exists())
{ {
file.delete(); flag = file.delete();
flag = true; }
} return flag;
return flag; }
}
/**
/** * 文件名称验证
* 文件名称验证 *
* * @param filename 文件名称
* @param filename 文件名称 * @return true 正常 false 非法
* @return true 正常 false 非法 */
*/ public static boolean isValidFilename(String filename)
public static boolean isValidFilename(String filename) {
{ return filename.matches(FILENAME_PATTERN);
return filename.matches(FILENAME_PATTERN); }
}
/**
/** * 检查文件是否可下载
* 检查文件是否可下载 *
* * @param resource 需要下载的文件
* @param resource 需要下载的文件 * @return true 正常 false 非法
* @return true 正常 false 非法 */
*/ public static boolean checkAllowDownload(String resource)
public static boolean checkAllowDownload(String resource) {
{ // 禁止目录上跳级别
// 禁止目录上跳级别 if (StringUtils.contains(resource, ".."))
if (StringUtils.contains(resource, "..")) {
{ return false;
return false; }
}
// 检查允许下载的文件规则
// 检查允许下载的文件规则 if (ArrayUtils.contains(MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION, FileTypeUtils.getFileType(resource)))
if (ArrayUtils.contains(MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION, FileTypeUtils.getFileType(resource))) {
{ return true;
return true; }
}
// 不在允许下载的文件规则
// 不在允许下载的文件规则 return false;
return false; }
}
/**
/** * 下载文件名重新编码
* 下载文件名重新编码 *
* * @param request 请求对象
* @param request 请求对象 * @param fileName 文件名
* @param fileName 文件名 * @return 编码后的文件名
* @return 编码后的文件名 */
*/ public static String setFileDownloadHeader(HttpServletRequest request, String fileName) throws UnsupportedEncodingException
public static String setFileDownloadHeader(HttpServletRequest request, String fileName) throws UnsupportedEncodingException {
{ final String agent = request.getHeader("USER-AGENT");
final String agent = request.getHeader("USER-AGENT"); String filename = fileName;
String filename = fileName; if (agent.contains("MSIE"))
if (agent.contains("MSIE")) {
{ // IE浏览器
// IE浏览器 filename = URLEncoder.encode(filename, "utf-8");
filename = URLEncoder.encode(filename, "utf-8"); filename = filename.replace("+", " ");
filename = filename.replace("+", " "); }
} else if (agent.contains("Firefox"))
else if (agent.contains("Firefox")) {
{ // 火狐浏览器
// 火狐浏览器 filename = new String(fileName.getBytes(), "ISO8859-1");
filename = new String(fileName.getBytes(), "ISO8859-1"); }
} else if (agent.contains("Chrome"))
else if (agent.contains("Chrome")) {
{ // google浏览器
// google浏览器 filename = URLEncoder.encode(filename, "utf-8");
filename = URLEncoder.encode(filename, "utf-8"); }
} else
else {
{ // 其它浏览器
// 其它浏览器 filename = URLEncoder.encode(filename, "utf-8");
filename = URLEncoder.encode(filename, "utf-8"); }
} return filename;
return filename; }
}
/**
/** * 返回文件名
* 返回文件名 *
* * @param filePath 文件
* @param filePath 文件 * @return 文件名
* @return 文件名 */
*/ public static String getName(String filePath)
public static String getName(String filePath) {
{ if (null == filePath)
if (null == filePath) {
{ return null;
return null; }
} int len = filePath.length();
int len = filePath.length(); if (0 == len)
if (0 == len) {
{ return filePath;
return filePath; }
} if (isFileSeparator(filePath.charAt(len - 1)))
if (isFileSeparator(filePath.charAt(len - 1))) {
{ // 以分隔符结尾的去掉结尾分隔符
// 以分隔符结尾的去掉结尾分隔符 len--;
len--; }
}
int begin = 0;
int begin = 0; char c;
char c; for (int i = len - 1; i > -1; i--)
for (int i = len - 1; i > -1; i--) {
{ c = filePath.charAt(i);
c = filePath.charAt(i); if (isFileSeparator(c))
if (isFileSeparator(c)) {
{ // 查找最后一个路径分隔符/或者\
// 查找最后一个路径分隔符/或者\ begin = i + 1;
begin = i + 1; break;
break; }
} }
}
return filePath.substring(begin, len);
return filePath.substring(begin, len); }
}
/**
/** * 是否为Windows或者LinuxUnix文件分隔符<br>
* 是否为Windows或者LinuxUnix文件分隔符<br> * Windows平台下分隔符为\LinuxUnix/
* Windows平台下分隔符为\LinuxUnix/ *
* * @param c 字符
* @param c 字符 * @return 是否为Windows或者LinuxUnix文件分隔符
* @return 是否为Windows或者LinuxUnix文件分隔符 */
*/ public static boolean isFileSeparator(char c)
public static boolean isFileSeparator(char c) {
{ return SLASH == c || BACKSLASH == c;
return SLASH == c || BACKSLASH == c; }
}
/**
/** * 下载文件名重新编码
* 下载文件名重新编码 *
* * @param response 响应对象
* @param response 响应对象 * @param realFileName 真实文件名
* @param realFileName 真实文件名 * @return
* @return */
*/ public static void setAttachmentResponseHeader(HttpServletResponse response, String realFileName) throws UnsupportedEncodingException
public static void setAttachmentResponseHeader(HttpServletResponse response, String realFileName) throws UnsupportedEncodingException {
{ String percentEncodedFileName = percentEncode(realFileName);
String percentEncodedFileName = percentEncode(realFileName);
StringBuilder contentDispositionValue = new StringBuilder();
StringBuilder contentDispositionValue = new StringBuilder(); contentDispositionValue.append("attachment; filename=")
contentDispositionValue.append("attachment; filename=") .append(percentEncodedFileName)
.append(percentEncodedFileName) .append(";")
.append(";") .append("filename*=")
.append("filename*=") .append("utf-8''")
.append("utf-8''") .append(percentEncodedFileName);
.append(percentEncodedFileName);
response.setHeader("Content-disposition", contentDispositionValue.toString());
response.setHeader("Content-disposition", contentDispositionValue.toString()); }
}
/**
/** * 百分号编码工具方法
* 百分号编码工具方法 *
* * @param s 需要百分号编码的字符串
* @param s 需要百分号编码的字符串 * @return 百分号编码后的字符串
* @return 百分号编码后的字符串 */
*/ public static String percentEncode(String s) throws UnsupportedEncodingException
public static String percentEncode(String s) throws UnsupportedEncodingException {
{ String encode = URLEncoder.encode(s, StandardCharsets.UTF_8.toString());
String encode = URLEncoder.encode(s, StandardCharsets.UTF_8.toString()); return encode.replaceAll("\\+", "%20");
return encode.replaceAll("\\+", "%20"); }
} }
}