package org.ssssssss.magicapi.utils;@b@@b@import org.slf4j.Logger;@b@import org.slf4j.LoggerFactory;@b@@b@import javax.sql.DataSource;@b@import java.io.*;@b@import java.nio.charset.StandardCharsets;@b@import java.nio.file.Files;@b@import java.util.ArrayList;@b@import java.util.List;@b@import java.util.regex.Pattern;@b@import java.nio.charset.StandardCharsets;@b@@b@/**@b@ * IO工具包@b@ *@b@ * @author mxd@b@ */@b@public class IoUtils {@b@@b@ private static final Logger logger = LoggerFactory.getLogger(IoUtils.class);@b@@b@ private static final Pattern FILE_NAME_PATTERN = Pattern.compile("^(?!\\.)[\\u4e00-\\u9fa5_a-zA-Z0-9.\\-()]+$");@b@@b@ public static boolean validateFileName(String name) {@b@ return FILE_NAME_PATTERN.matcher(name).matches();@b@ }@b@@b@ public static List<File> files(File file, String suffix) {@b@ List<File> list = new ArrayList<>();@b@ if (file.isDirectory()) {@b@ File[] files = file.listFiles((path) -> path.isDirectory() || path.getName().endsWith(suffix));@b@ if (files != null) {@b@ for (int i = files.length - 1; i >= 0; i--) {@b@ list.addAll(files(files[i], suffix));@b@ }@b@ }@b@ } else if (file.exists()) {@b@ list.add(file);@b@ }@b@ return list;@b@ }@b@@b@ public static List<File> dirs(File file) {@b@ return subDirs(true, file);@b@ }@b@@b@ private static List<File> subDirs(boolean isRoot, File file) {@b@ List<File> list = new ArrayList<>();@b@ if (file.isDirectory()) {@b@ File[] files = file.listFiles(File::isDirectory);@b@ if (files != null) {@b@ for (int i = files.length - 1; i >= 0; i--) {@b@ list.addAll(subDirs(false, files[i]));@b@ }@b@ }@b@ if (!isRoot) {@b@ list.add(file);@b@ }@b@ }@b@ return list;@b@ }@b@@b@ public static byte[] bytes(File file) {@b@ try {@b@ return Files.readAllBytes(file.toPath());@b@ } catch (IOException e) {@b@ logger.error("读取文件失败", e);@b@ return new byte[0];@b@ }@b@ }@b@@b@ public static String string(File file) {@b@ return new String(bytes(file), StandardCharsets.UTF_8);@b@ }@b@@b@ public static byte[] bytes(InputStream inputStream) {@b@ try {@b@ ByteArrayOutputStream baos = new ByteArrayOutputStream();@b@ byte[] buf = new byte[4096];@b@ int len;@b@ while ((len = inputStream.read(buf, 0, buf.length)) != -1) {@b@ baos.write(buf, 0, len);@b@ }@b@ return baos.toByteArray();@b@ } catch (IOException e) {@b@ logger.error("读取InputStream失败", e);@b@ return new byte[0];@b@ }@b@ }@b@@b@ public static String string(InputStream inputStream) {@b@ try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {@b@ StringBuilder result = new StringBuilder();@b@ String line;@b@ boolean flag = false;@b@ while ((line = reader.readLine()) != null) {@b@ if (flag) {@b@ result.append("\r\n");@b@ }@b@ result.append(line);@b@ flag = true;@b@ }@b@ return result.toString();@b@ } catch (IOException e) {@b@ logger.error("读取InputStream失败", e);@b@ return "";@b@ }@b@ }@b@@b@ public static boolean write(File file, byte[] bytes) {@b@ try {@b@ Files.write(file.toPath(), bytes);@b@ return true;@b@ } catch (IOException e) {@b@ logger.error("写文件失败", e);@b@ return false;@b@ }@b@ }@b@@b@ public static boolean write(File file, String content) {@b@ if (content == null) {@b@ return false;@b@ }@b@ return write(file, content.getBytes(StandardCharsets.UTF_8));@b@ }@b@@b@ public static boolean delete(File file) {@b@ if (file == null) {@b@ return true;@b@ }@b@ if (file.isDirectory()) {@b@ File[] files = file.listFiles();@b@ if (files != null) {@b@ for (int i = files.length - 1; i >= 0; i--) {@b@ if (!delete(files[i])) {@b@ return false;@b@ }@b@ }@b@ }@b@ }@b@ if (!file.exists()) {@b@ return true;@b@ }@b@ return file.delete();@b@ }@b@@b@ public static void closeDataSource(DataSource dataSource) {@b@ if (dataSource != null) {@b@ if (dataSource instanceof Closeable) {@b@ try {@b@ ((Closeable) dataSource).close();@b@ } catch (Exception e) {@b@ logger.warn("Close DataSource error", e);@b@ }@b@ } else {@b@ logger.warn("DataSource can not close");@b@ }@b@ }@b@ }@b@@b@ public static void close(Closeable closeable){@b@ try {@b@ if(closeable != null){@b@ closeable.close();@b@ }@b@ } catch (IOException ignored) {@b@ }@b@ }@b@}
package org.jeecgframework.core.util;@b@@b@import java.io.ByteArrayInputStream;@b@import java.io.ByteArrayOutputStream;@b@import java.io.File;@b@import java.io.FileInputStream;@b@import java.io.FileNotFoundException;@b@import java.io.FileOutputStream;@b@import java.io.IOException;@b@import java.io.InputStream;@b@import java.io.UnsupportedEncodingException;@b@@b@import jodd.io.StringInputStream;@b@@b@@b@@b@/**@b@ * @b@ * @author Andy.Chen@b@ * @mail Chenjunjun.ZJ@gmail.com@b@ * @b@ */@b@public class StreamUtils {@b@@b@ final static int BUFFER_SIZE = 4096;@b@ /**@b@ * 将InputStream转换成String@b@ * @b@ * @param in@b@ * InputStream@b@ * @return String@b@ * @throws Exception@b@ * @b@ */@b@ public static String InputStreamTOString(InputStream in) {@b@@b@ ByteArrayOutputStream outStream = new ByteArrayOutputStream();@b@ byte[] data = new byte[BUFFER_SIZE];@b@ String string = null;@b@ int count = 0;@b@ try {@b@ while ((count = in.read(data, 0, BUFFER_SIZE)) != -1)@b@ outStream.write(data, 0, count);@b@ } catch (IOException e) {@b@ e.printStackTrace();@b@ }@b@@b@ data = null;@b@ try {@b@ string = new String(outStream.toByteArray(), "UTF-8");@b@ } catch (UnsupportedEncodingException e) {@b@ e.printStackTrace();@b@ }@b@ return string;@b@ }@b@@b@ /**@b@ * 将InputStream转换成某种字符编码的String@b@ * @b@ * @param in@b@ * @param encoding@b@ * @return@b@ * @throws Exception@b@ */@b@ public static String InputStreamTOString(InputStream in, String encoding) {@b@ String string = null;@b@ ByteArrayOutputStream outStream = new ByteArrayOutputStream();@b@ byte[] data = new byte[BUFFER_SIZE];@b@ int count = -1;@b@ try {@b@ while ((count = in.read(data, 0, BUFFER_SIZE)) != -1)@b@ outStream.write(data, 0, count);@b@ } catch (IOException e) {@b@ e.printStackTrace();@b@ }@b@@b@ data = null;@b@ try {@b@ string = new String(outStream.toByteArray(), encoding);@b@ } catch (UnsupportedEncodingException e) {@b@ e.printStackTrace();@b@ }@b@ return string;@b@ }@b@@b@ /**@b@ * 将String转换成InputStream@b@ * @b@ * @param in@b@ * @return@b@ * @throws Exception@b@ */@b@ public static InputStream StringTOInputStream(String in) throws Exception {@b@@b@ ByteArrayInputStream is = new ByteArrayInputStream(in.getBytes("UTF-8"));@b@ return is;@b@ }@b@@b@ /**@b@ * 将String转换成InputStream@b@ * @b@ * @param in@b@ * @return@b@ * @throws Exception@b@ */@b@ public static byte[] StringTObyte(String in) {@b@ byte[] bytes = null;@b@ try {@b@ bytes = InputStreamTOByte(StringTOInputStream(in));@b@ } catch (IOException e) {@b@ } catch (Exception e) {@b@ e.printStackTrace();@b@ }@b@ return bytes;@b@ }@b@@b@ /**@b@ * 将InputStream转换成byte数组@b@ * @b@ * @param in@b@ * InputStream@b@ * @return byte[]@b@ * @throws IOException@b@ */@b@ public static byte[] InputStreamTOByte(InputStream in) throws IOException {@b@@b@ ByteArrayOutputStream outStream = new ByteArrayOutputStream();@b@ byte[] data = new byte[BUFFER_SIZE];@b@ int count = -1;@b@ while ((count = in.read(data, 0, BUFFER_SIZE)) != -1)@b@ outStream.write(data, 0, count);@b@@b@ data = null;@b@ return outStream.toByteArray();@b@ }@b@@b@ /**@b@ * 将byte数组转换成InputStream@b@ * @b@ * @param in@b@ * @return@b@ * @throws Exception@b@ */@b@ public static InputStream byteTOFInputStream(byte[] in) throws Exception {@b@@b@ InputStream is = new StringInputStream(InputStreamTOString(StreamUtils.byteTOInputStream(in)));@b@ return is;@b@ }@b@ /**@b@ * 将byte数组转换成InputStream@b@ * @b@ * @param in@b@ * @return@b@ * @throws Exception@b@ */@b@ public static InputStream byteTOInputStream(byte[] in) throws Exception {@b@@b@ ByteArrayInputStream is = new ByteArrayInputStream(in);@b@ return is;@b@ }@b@@b@ /**@b@ * 将byte数组转换成String@b@ * @b@ * @param in@b@ * @return@b@ * @throws Exception@b@ */@b@ public static String byteTOString(byte[] in) {@b@@b@ InputStream is = null;@b@ try {@b@ is = byteTOInputStream(in);@b@ } catch (Exception e) {@b@ e.printStackTrace();@b@ }@b@ return InputStreamTOString(is, "UTF-8");@b@ }@b@ /**@b@ * 将byte数组转换成String@b@ * @b@ * @param in@b@ * @return@b@ * @throws Exception@b@ */@b@ public static String getString(String in) {@b@@b@ String is = null;@b@ try {@b@ is = byteTOString(StringTObyte(in));@b@ } catch (Exception e) {@b@ e.printStackTrace();@b@ }@b@ return is;@b@ }@b@@b@ // InputStream 转换成byte[]@b@ public byte[] getBytes(InputStream is) throws IOException {@b@@b@ ByteArrayOutputStream baos = new ByteArrayOutputStream();@b@ byte[] b = new byte[BUFFER_SIZE];@b@ int len = 0;@b@@b@ while ((len = is.read(b, 0, BUFFER_SIZE)) != -1) {@b@ baos.write(b, 0, len);@b@ }@b@@b@ baos.flush();@b@@b@ byte[] bytes = baos.toByteArray();@b@@b@ org.jeecgframework.core.util.LogUtil.info(new String(bytes));@b@@b@ return bytes;@b@ }@b@ /**@b@ * 根据文件路径创建文件输入流处理@b@ * 以字节为单位(非 unicode )@b@ * @param path@b@ * @return@b@ */@b@ public static FileInputStream getFileInputStream(String filepath) {@b@ FileInputStream fileInputStream = null;@b@ try {@b@ fileInputStream = new FileInputStream(filepath);@b@ } catch (FileNotFoundException e) {@b@ System.out.print("错误信息:文件不存在");@b@ e.printStackTrace();@b@ }@b@ return fileInputStream;@b@ }@b@ /**@b@ * 根据文件对象创建文件输入流处理@b@ * 以字节为单位(非 unicode )@b@ * @param path@b@ * @return@b@ */@b@ public static FileInputStream getFileInputStream(File file) {@b@ FileInputStream fileInputStream = null;@b@ try {@b@ fileInputStream = new FileInputStream(file);@b@ } catch (FileNotFoundException e) {@b@ System.out.print("错误信息:文件不存在");@b@ e.printStackTrace();@b@ }@b@ return fileInputStream;@b@ }@b@ /**@b@ * 根据文件对象创建文件输出流处理@b@ * 以字节为单位(非 unicode )@b@ * @param file@b@ * @param append true:文件以追加方式打开,false:则覆盖原文件的内容@b@ * @return@b@ */@b@ public static FileOutputStream getFileOutputStream(File file,boolean append) {@b@ FileOutputStream fileOutputStream = null;@b@ try {@b@ fileOutputStream = new FileOutputStream(file,append);@b@ } catch (FileNotFoundException e) {@b@ System.out.print("错误信息:文件不存在");@b@ e.printStackTrace();@b@ }@b@ return fileOutputStream;@b@ }@b@ /**@b@ * 根据文件路径创建文件输出流处理@b@ * 以字节为单位(非 unicode )@b@ * @param path@b@ * @param append true:文件以追加方式打开,false:则覆盖原文件的内容@b@ * @return@b@ */@b@ public static FileOutputStream getFileOutputStream(String filepath,boolean append) {@b@ FileOutputStream fileOutputStream = null;@b@ try {@b@ fileOutputStream = new FileOutputStream(filepath,append);@b@ } catch (FileNotFoundException e) {@b@ System.out.print("错误信息:文件不存在");@b@ e.printStackTrace();@b@ }@b@ return fileOutputStream;@b@ }@b@@b@ public static File getFile(String filepath) {@b@ return new File(filepath);@b@ }@b@ public static ByteArrayOutputStream getByteArrayOutputStream() {@b@ return new ByteArrayOutputStream();@b@ }@b@@b@}