From d5d7cd65923d3e16f1c0427e8664162ecf7698e9 Mon Sep 17 00:00:00 2001 From: RuoYi Date: Fri, 27 Jul 2018 21:02:50 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E5=8C=96=E4=B8=8B=E8=BD=BD=E6=96=87?= =?UTF-8?q?=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/ruoyi/common/utils/FileUtils.java | 90 +++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100644 src/main/java/com/ruoyi/common/utils/FileUtils.java diff --git a/src/main/java/com/ruoyi/common/utils/FileUtils.java b/src/main/java/com/ruoyi/common/utils/FileUtils.java new file mode 100644 index 000000000..e9e859877 --- /dev/null +++ b/src/main/java/com/ruoyi/common/utils/FileUtils.java @@ -0,0 +1,90 @@ +package com.ruoyi.common.utils; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.OutputStream; + +/** + * 文件处理工具类 + * + * @author ruoyi + */ +public class FileUtils +{ + + /** + * 输出指定文件的byte数组 + * + * @param filename 文件 + * @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(); + } + } + } + } + + /** + * @Desc 删除文件 + * @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; + } + +}