作者 张晓杰

文件下载

  1 +package org.jeecg.modules.erp.utils;
  2 +
  3 +import java.io.*;
  4 +import java.net.URLEncoder;
  5 +import java.nio.charset.StandardCharsets;
  6 +import javax.servlet.http.HttpServletRequest;
  7 +import javax.servlet.http.HttpServletResponse;
  8 +import org.apache.commons.io.IOUtils;
  9 +import org.apache.commons.lang3.ArrayUtils;
  10 +import org.apache.commons.io.FilenameUtils;
  11 +
  12 +/**
  13 + * 文件处理工具类
  14 + *
  15 + * @author ruoyi
  16 + */
  17 +public class FileUtils
  18 +{
  19 + public static String FILENAME_PATTERN = "[a-zA-Z0-9_\\-\\|\\.\\u4e00-\\u9fa5]+";
  20 +
  21 + /**
  22 + * 输出指定文件的byte数组
  23 + *
  24 + * @param filePath 文件路径
  25 + * @param os 输出流
  26 + * @return
  27 + */
  28 + public static void writeBytes(String filePath, OutputStream os) throws IOException
  29 + {
  30 + FileInputStream fis = null;
  31 + try
  32 + {
  33 + File file = new File(filePath);
  34 + if (!file.exists())
  35 + {
  36 + throw new FileNotFoundException(filePath);
  37 + }
  38 + fis = new FileInputStream(file);
  39 + byte[] b = new byte[1024];
  40 + int length;
  41 + while ((length = fis.read(b)) > 0)
  42 + {
  43 + os.write(b, 0, length);
  44 + }
  45 + }
  46 + catch (IOException e)
  47 + {
  48 + throw e;
  49 + }
  50 + finally
  51 + {
  52 + os.close();
  53 + fis.close();
  54 + }
  55 + }
  56 +
  57 +
  58 +
  59 + /**
  60 + * 删除文件
  61 + *
  62 + * @param filePath 文件
  63 + * @return
  64 + */
  65 + public static boolean deleteFile(String filePath)
  66 + {
  67 + boolean flag = false;
  68 + File file = new File(filePath);
  69 + // 路径为文件且不为空则进行删除
  70 + if (file.isFile() && file.exists())
  71 + {
  72 + flag = file.delete();
  73 + }
  74 + return flag;
  75 + }
  76 +
  77 + /**
  78 + * 文件名称验证
  79 + *
  80 + * @param filename 文件名称
  81 + * @return true 正常 false 非法
  82 + */
  83 + public static boolean isValidFilename(String filename)
  84 + {
  85 + return filename.matches(FILENAME_PATTERN);
  86 + }
  87 +
  88 +
  89 + /**
  90 + * 下载文件名重新编码
  91 + *
  92 + * @param request 请求对象
  93 + * @param fileName 文件名
  94 + * @return 编码后的文件名
  95 + */
  96 + public static String setFileDownloadHeader(HttpServletRequest request, String fileName) throws UnsupportedEncodingException
  97 + {
  98 + final String agent = request.getHeader("USER-AGENT");
  99 + String filename = fileName;
  100 + if (agent.contains("MSIE"))
  101 + {
  102 + // IE浏览器
  103 + filename = URLEncoder.encode(filename, "utf-8");
  104 + filename = filename.replace("+", " ");
  105 + }
  106 + else if (agent.contains("Firefox"))
  107 + {
  108 + // 火狐浏览器
  109 + filename = new String(fileName.getBytes(), "ISO8859-1");
  110 + }
  111 + else if (agent.contains("Chrome"))
  112 + {
  113 + // google浏览器
  114 + filename = URLEncoder.encode(filename, "utf-8");
  115 + }
  116 + else
  117 + {
  118 + // 其它浏览器
  119 + filename = URLEncoder.encode(filename, "utf-8");
  120 + }
  121 + return filename;
  122 + }
  123 +
  124 + /**
  125 + * 下载文件名重新编码
  126 + *
  127 + * @param response 响应对象
  128 + * @param realFileName 真实文件名
  129 + */
  130 + public static void setAttachmentResponseHeader(HttpServletResponse response, String realFileName) throws UnsupportedEncodingException
  131 + {
  132 + String percentEncodedFileName = percentEncode(realFileName);
  133 +
  134 + StringBuilder contentDispositionValue = new StringBuilder();
  135 + contentDispositionValue.append("attachment; filename=")
  136 + .append(percentEncodedFileName)
  137 + .append(";")
  138 + .append("filename*=")
  139 + .append("utf-8''")
  140 + .append(percentEncodedFileName);
  141 +
  142 + response.addHeader("Access-Control-Expose-Headers", "Content-Disposition,download-filename");
  143 + response.setHeader("Content-disposition", contentDispositionValue.toString());
  144 + response.setHeader("download-filename", percentEncodedFileName);
  145 + }
  146 +
  147 + /**
  148 + * 百分号编码工具方法
  149 + *
  150 + * @param s 需要百分号编码的字符串
  151 + * @return 百分号编码后的字符串
  152 + */
  153 + public static String percentEncode(String s) throws UnsupportedEncodingException
  154 + {
  155 + String encode = URLEncoder.encode(s, StandardCharsets.UTF_8.toString());
  156 + return encode.replaceAll("\\+", "%20");
  157 + }
  158 +
  159 + /**
  160 + * 获取图像后缀
  161 + *
  162 + * @param photoByte 图像数据
  163 + * @return 后缀名
  164 + */
  165 + public static String getFileExtendName(byte[] photoByte)
  166 + {
  167 + String strFileExtendName = "jpg";
  168 + if ((photoByte[0] == 71) && (photoByte[1] == 73) && (photoByte[2] == 70) && (photoByte[3] == 56)
  169 + && ((photoByte[4] == 55) || (photoByte[4] == 57)) && (photoByte[5] == 97))
  170 + {
  171 + strFileExtendName = "gif";
  172 + }
  173 + else if ((photoByte[6] == 74) && (photoByte[7] == 70) && (photoByte[8] == 73) && (photoByte[9] == 70))
  174 + {
  175 + strFileExtendName = "jpg";
  176 + }
  177 + else if ((photoByte[0] == 66) && (photoByte[1] == 77))
  178 + {
  179 + strFileExtendName = "bmp";
  180 + }
  181 + else if ((photoByte[1] == 80) && (photoByte[2] == 78) && (photoByte[3] == 71))
  182 + {
  183 + strFileExtendName = "png";
  184 + }
  185 + return strFileExtendName;
  186 + }
  187 +
  188 + /**
  189 + * 获取文件名称 /profile/upload/2022/04/16/ruoyi.png -- ruoyi.png
  190 + *
  191 + * @param fileName 路径名称
  192 + * @return 没有文件路径的名称
  193 + */
  194 + public static String getName(String fileName)
  195 + {
  196 + if (fileName == null)
  197 + {
  198 + return null;
  199 + }
  200 + int lastUnixPos = fileName.lastIndexOf('/');
  201 + int lastWindowsPos = fileName.lastIndexOf('\\');
  202 + int index = Math.max(lastUnixPos, lastWindowsPos);
  203 + return fileName.substring(index + 1);
  204 + }
  205 +
  206 + /**
  207 + * 获取不带后缀文件名称 /profile/upload/2022/04/16/ruoyi.png -- ruoyi
  208 + *
  209 + * @param fileName 路径名称
  210 + * @return 没有文件路径和后缀的名称
  211 + */
  212 + public static String getNameNotSuffix(String fileName)
  213 + {
  214 + if (fileName == null)
  215 + {
  216 + return null;
  217 + }
  218 + String baseName = FilenameUtils.getBaseName(fileName);
  219 + return baseName;
  220 + }
  221 +}