|
@@ -1,8 +1,15 @@
|
|
|
package com.ruoyi.common.utils.file;
|
|
|
|
|
|
import cn.hutool.core.io.FileUtil;
|
|
|
+import com.ruoyi.common.utils.StringUtils;
|
|
|
+import org.apache.commons.lang3.ArrayUtils;
|
|
|
|
|
|
import javax.servlet.http.HttpServletResponse;
|
|
|
+import java.io.File;
|
|
|
+import java.io.FileInputStream;
|
|
|
+import java.io.FileNotFoundException;
|
|
|
+import java.io.IOException;
|
|
|
+import java.io.OutputStream;
|
|
|
import java.io.UnsupportedEncodingException;
|
|
|
import java.net.URLEncoder;
|
|
|
import java.nio.charset.StandardCharsets;
|
|
@@ -12,27 +19,65 @@ import java.nio.charset.StandardCharsets;
|
|
|
*
|
|
|
* @author ruoyi
|
|
|
*/
|
|
|
-public class FileUtils extends FileUtil
|
|
|
-{
|
|
|
+public class FileUtils extends FileUtil {
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 输出指定文件的byte数组
|
|
|
+ *
|
|
|
+ * @param filePath 文件路径
|
|
|
+ * @param os 输出流
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ public static void writeBytes(String filePath, OutputStream os) throws IOException {
|
|
|
+ FileInputStream fis = null;
|
|
|
+ try {
|
|
|
+ File file = new File(filePath);
|
|
|
+ if (!file.exists()) {
|
|
|
+ throw new FileNotFoundException(filePath);
|
|
|
+ }
|
|
|
+ fis = new FileInputStream(file);
|
|
|
+ byte[] b = new byte[1024];
|
|
|
+ int length;
|
|
|
+ while ((length = fis.read(b)) > 0) {
|
|
|
+ os.write(b, 0, length);
|
|
|
+ }
|
|
|
+ } catch (IOException e) {
|
|
|
+ throw e;
|
|
|
+ } finally {
|
|
|
+ if (os != null) {
|
|
|
+ try {
|
|
|
+ os.close();
|
|
|
+ } catch (IOException e1) {
|
|
|
+ e1.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ if (fis != null) {
|
|
|
+ try {
|
|
|
+ fis.close();
|
|
|
+ } catch (IOException e1) {
|
|
|
+ e1.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
/**
|
|
|
* 下载文件名重新编码
|
|
|
*
|
|
|
- * @param response 响应对象
|
|
|
+ * @param response 响应对象
|
|
|
* @param realFileName 真实文件名
|
|
|
* @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);
|
|
|
|
|
|
StringBuilder contentDispositionValue = new StringBuilder();
|
|
|
contentDispositionValue.append("attachment; filename=")
|
|
|
- .append(percentEncodedFileName)
|
|
|
- .append(";")
|
|
|
- .append("filename*=")
|
|
|
- .append("utf-8''")
|
|
|
- .append(percentEncodedFileName);
|
|
|
+ .append(percentEncodedFileName)
|
|
|
+ .append(";")
|
|
|
+ .append("filename*=")
|
|
|
+ .append("utf-8''")
|
|
|
+ .append(percentEncodedFileName);
|
|
|
|
|
|
response.setHeader("Content-disposition", contentDispositionValue.toString());
|
|
|
}
|
|
@@ -43,9 +88,29 @@ public class FileUtils extends FileUtil
|
|
|
* @param s 需要百分号编码的字符串
|
|
|
* @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());
|
|
|
return encode.replaceAll("\\+", "%20");
|
|
|
}
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 检查文件是否可下载
|
|
|
+ *
|
|
|
+ * @param resource 需要下载的文件
|
|
|
+ * @return true 正常 false 非法
|
|
|
+ */
|
|
|
+ public static boolean checkAllowDownload(String resource) {
|
|
|
+ // 禁止目录上跳级别
|
|
|
+ if (StringUtils.contains(resource, "..")) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 检查允许下载的文件规则
|
|
|
+ if (ArrayUtils.contains(MimeTypeUtils.DEFAULT_ALLOWED_EXTENSION, FileTypeUtils.getFileType(resource))) {
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 不在允许下载的文件规则
|
|
|
+ return false;
|
|
|
+ }
|
|
|
}
|