123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166 |
- package com.fdkk.fdkkmeta.util;
- import cn.hutool.core.io.IoUtil;
- import com.alibaba.fastjson.util.IOUtils;
- import org.springframework.util.MimeTypeUtils;
- import org.yaml.snakeyaml.util.ArrayUtils;
- import javax.servlet.http.HttpServletRequest;
- import javax.servlet.http.HttpServletResponse;
- import java.io.*;
- import java.net.URLEncoder;
- import java.nio.charset.StandardCharsets;
- import java.util.ArrayList;
- import java.util.List;
- /**
- * 文件处理工具类
- *
- * @author fdkk
- */
- public class FileUtils {
- private static String FILENAME_PATTERN = "[a-zA-Z0-9_\\-\\|\\.\\u4e00-\\u9fa5]+";
- /**
- * 输出指定文件的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 {
- IOUtils.close(os);
- IOUtils.close(fis);
- }
- }
- /**
- * 删除文件
- *
- * @param filePath 文件
- * @return
- */
- public static boolean deleteFile(String filePath) {
- boolean flag = false;
- File file = new File(filePath);
- // 路径为文件且不为空则进行删除
- if (file.isFile() && file.exists()) {
- file.delete();
- flag = true;
- }
- return flag;
- }
- /**
- * 文件名称验证
- *
- * @param filename 文件名称
- * @return true 正常 false 非法
- */
- public static boolean isValidFilename(String filename) {
- return filename.matches(FileUtils.FILENAME_PATTERN);
- }
- public static List<String> readFileByLines2(String path) {
- File f = new File(path);
- if (!f.exists()) {
- return null;
- }
- FileReader reader = null;
- BufferedReader br = null;
- try {
- // read file content from file
- reader = new FileReader(path);
- br = new BufferedReader(reader);
- String str = null;
- List<String> result = new ArrayList<String>();
- while ((str = br.readLine()) != null) {
- result.add(str);
- }
- return result;
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- IoUtil.close(reader);
- IoUtil.close(br);
- }
- return null;
- }
- /**
- * 下载文件名重新编码
- *
- * @param response 响应对象
- * @param realFileName 真实文件名
- * @return
- */
- public static void setAttachmentResponseHeader(HttpServletResponse response, String realFileName) throws UnsupportedEncodingException {
- String percentEncodedFileName = FileUtils.percentEncode(realFileName);
- StringBuilder contentDispositionValue = new StringBuilder();
- contentDispositionValue.append("attachment; filename=")
- .append(percentEncodedFileName)
- .append(";")
- .append("filename*=")
- .append("utf-8''")
- .append(percentEncodedFileName);
- response.setHeader("Content-disposition", contentDispositionValue.toString());
- response.setHeader("download-filename", percentEncodedFileName);
- }
- /**
- * 百分号编码工具方法
- *
- * @param s 需要百分号编码的字符串
- * @return 百分号编码后的字符串
- */
- private static String percentEncode(String s) throws UnsupportedEncodingException {
- String encode = URLEncoder.encode(s, StandardCharsets.UTF_8.toString());
- return encode.replaceAll("\\+", "%20");
- }
- /**
- * 获取图像后缀
- *
- * @param photoByte 图像数据
- * @return 后缀名
- */
- private static String getFileExtendName(byte[] photoByte) {
- String strFileExtendName = "jpg";
- if ((photoByte[0] == 71) && (photoByte[1] == 73) && (photoByte[2] == 70) && (photoByte[3] == 56)
- && ((photoByte[4] == 55) || (photoByte[4] == 57)) && (photoByte[5] == 97)) {
- strFileExtendName = "gif";
- } else if ((photoByte[6] == 74) && (photoByte[7] == 70) && (photoByte[8] == 73) && (photoByte[9] == 70)) {
- strFileExtendName = "jpg";
- } else if ((photoByte[0] == 66) && (photoByte[1] == 77)) {
- strFileExtendName = "bmp";
- } else if ((photoByte[1] == 80) && (photoByte[2] == 78) && (photoByte[3] == 71)) {
- strFileExtendName = "png";
- }
- return strFileExtendName;
- }
- }
|