|
@@ -0,0 +1,261 @@
|
|
|
|
+package com.fd.util;
|
|
|
|
+
|
|
|
|
+import lombok.extern.log4j.Log4j2;
|
|
|
|
+import org.springframework.util.ResourceUtils;
|
|
|
|
+
|
|
|
|
+import javax.servlet.http.HttpServletResponse;
|
|
|
|
+import java.io.*;
|
|
|
|
+import java.net.URLDecoder;
|
|
|
|
+import java.net.URLEncoder;
|
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
|
+import java.util.Base64;
|
|
|
|
+import java.util.Date;
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ * Created by Owen on 2019/10/25 0025 14:20
|
|
|
|
+ */
|
|
|
|
+@Log4j2
|
|
|
|
+public class FileUtils {
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * web 文件下载
|
|
|
|
+ * @param response
|
|
|
|
+ * @param path 源文件路径
|
|
|
|
+ * @param path 源文件名
|
|
|
|
+ * @return
|
|
|
|
+ * @throws Exception
|
|
|
|
+ */
|
|
|
|
+ public static void fileDownload(HttpServletResponse response, String path) throws Exception {
|
|
|
|
+ String fileName = path.substring(path.lastIndexOf(File.separator)+1);
|
|
|
|
+ // 当文件名不是英文名的时候,最好使用url解码器去编码一下,
|
|
|
|
+ fileName= URLEncoder.encode(fileName,"UTF-8");
|
|
|
|
+
|
|
|
|
+ // 获取response的输出流,用来输出文件
|
|
|
|
+// ServletOutputStream out = response.getOutputStream();
|
|
|
|
+ OutputStream out = response.getOutputStream();
|
|
|
|
+
|
|
|
|
+ // 将响应的类型
|
|
|
|
+ response.setContentType("APPLICATION/OCTET-STREAM");
|
|
|
|
+ response.setHeader("Content-Disposition","attachment; filename="+fileName);
|
|
|
|
+
|
|
|
|
+ // 以输入流的形式读取文件
|
|
|
|
+// log.info("zipPath: " + path);
|
|
|
|
+ FileInputStream in = new FileInputStream(path);
|
|
|
|
+
|
|
|
|
+ //可以自己 指定缓冲区的大小
|
|
|
|
+ byte[] buffer = new byte[1024];
|
|
|
|
+ int len = 0;
|
|
|
|
+ while ((len = in.read(buffer)) != -1) {
|
|
|
|
+ out.write(buffer, 0, len);
|
|
|
|
+ out.flush();
|
|
|
|
+ }
|
|
|
|
+ //关闭输入输出流
|
|
|
|
+ out.close();
|
|
|
|
+ in.close();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 获取类路径
|
|
|
|
+ */
|
|
|
|
+ public static String getResource(){
|
|
|
|
+ String path = "";
|
|
|
|
+ try {
|
|
|
|
+ path = ResourceUtils.getURL("classpath:").getPath();
|
|
|
|
+ path = URLDecoder.decode(path,"utf-8");
|
|
|
|
+ log.info("classpath path :"+path);
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ log.error(" classpath Error" + e.getMessage(), e);
|
|
|
|
+ }
|
|
|
|
+ return path;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 创建文件夹
|
|
|
|
+ * destDirName:文件夹名称
|
|
|
|
+ */
|
|
|
|
+ public static void createDir(String destDirName) {
|
|
|
|
+ File dir = new File(destDirName);
|
|
|
|
+ // 创建目录
|
|
|
|
+ if (!dir.exists()) {
|
|
|
|
+ dir.mkdirs();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 大文件读写
|
|
|
|
+ *
|
|
|
|
+ * 1.先将文件中的内容读入到缓冲输入流中
|
|
|
|
+ * 2.将输入流中的数据通过缓冲输出流写入到目标文件中
|
|
|
|
+ * 3.关闭输入流和输出流
|
|
|
|
+ *
|
|
|
|
+ * in: 输入流
|
|
|
|
+ * path: 保持位置
|
|
|
|
+ */
|
|
|
|
+
|
|
|
|
+ public static void bigFileWrite(InputStream in, String path) throws IOException {
|
|
|
|
+ long start = System.currentTimeMillis();
|
|
|
|
+ // 输入缓冲流
|
|
|
|
+ BufferedInputStream inputBuff = new BufferedInputStream(in);
|
|
|
|
+
|
|
|
|
+ // 输出缓冲流
|
|
|
|
+ BufferedOutputStream stream = new BufferedOutputStream(new FileOutputStream(path));
|
|
|
|
+ int len = 0;
|
|
|
|
+ byte[] buffer = new byte[1024];
|
|
|
|
+ while ((len = inputBuff.read(buffer)) != -1) {
|
|
|
|
+ stream.write(buffer, 0, len);
|
|
|
|
+ }
|
|
|
|
+ stream.flush();
|
|
|
|
+ stream.close();
|
|
|
|
+ inputBuff.close();
|
|
|
|
+
|
|
|
|
+ long end = System.currentTimeMillis();
|
|
|
|
+
|
|
|
|
+ System.out.println("total: "+ (end-start)/1000);
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ *
|
|
|
|
+ * @param content base64内容
|
|
|
|
+ * @param path 输出文件路径,需要后缀名
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public static boolean base64ToFileWriter(String content, String path) {
|
|
|
|
+ if (content == null) {
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+ try {
|
|
|
|
+ byte[] b = Base64.getDecoder().decode(content);
|
|
|
|
+ // processing data
|
|
|
|
+ for (int i = 0; i < b.length; ++i) {
|
|
|
|
+ if (b[i] < 0) {
|
|
|
|
+ b[i] += 256;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ OutputStream out = new FileOutputStream(path);
|
|
|
|
+ out.write(b);
|
|
|
|
+ out.flush();
|
|
|
|
+ out.close();
|
|
|
|
+ return true;
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ return false;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 读取文件方法
|
|
|
|
+ * @param Path 文件路径
|
|
|
|
+ * @return 返回内容
|
|
|
|
+ */
|
|
|
|
+ public static String readFile(String Path){
|
|
|
|
+ BufferedReader reader = null;
|
|
|
|
+ String laststr = "";
|
|
|
|
+ try{
|
|
|
|
+ FileInputStream fileInputStream = new FileInputStream(Path);
|
|
|
|
+ InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, "UTF-8");
|
|
|
|
+ reader = new BufferedReader(inputStreamReader);
|
|
|
|
+ String tempString = null;
|
|
|
|
+ while((tempString = reader.readLine()) != null){
|
|
|
|
+ laststr += tempString;
|
|
|
|
+ }
|
|
|
|
+ reader.close();
|
|
|
|
+ }catch(IOException e){
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }finally{
|
|
|
|
+ if(reader != null){
|
|
|
|
+ try {
|
|
|
|
+ reader.close();
|
|
|
|
+ } catch (IOException e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return laststr;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 生成文件
|
|
|
|
+ * @param content 内容
|
|
|
|
+ * @param path 生成路径
|
|
|
|
+ * @throws IOException
|
|
|
|
+ */
|
|
|
|
+ public static void fileWriter(String content, String path) throws IOException {
|
|
|
|
+ FileWriter writer = new FileWriter(path);
|
|
|
|
+ writer.write(content);
|
|
|
|
+ writer.flush();
|
|
|
|
+ writer.close();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ // 获取时间戳
|
|
|
|
+ public static String dateStr() {
|
|
|
|
+ SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");
|
|
|
|
+ String format = df.format(new Date());
|
|
|
|
+ long timeMillis = System.currentTimeMillis();
|
|
|
|
+ return format + "_" + timeMillis + "_";
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 删除指定文件夹下所有文件
|
|
|
|
+ * @param path 文件夹完整绝对路径
|
|
|
|
+ * @return
|
|
|
|
+ */
|
|
|
|
+ public static boolean delAllFile(String path) {
|
|
|
|
+ boolean flag = false;
|
|
|
|
+ File file = new File(path);
|
|
|
|
+ if (!file.exists()) {
|
|
|
|
+ return flag;
|
|
|
|
+ }
|
|
|
|
+ if (!file.isDirectory()) {
|
|
|
|
+ return flag;
|
|
|
|
+ }
|
|
|
|
+ String[] tempList = file.list();
|
|
|
|
+ File temp = null;
|
|
|
|
+ for (int i = 0; i < tempList.length; i++) {
|
|
|
|
+ if (path.endsWith(File.separator)) {
|
|
|
|
+ temp = new File(path + tempList[i]);
|
|
|
|
+ } else {
|
|
|
|
+ temp = new File(path + File.separator + tempList[i]);
|
|
|
|
+ }
|
|
|
|
+ if (temp.isFile()) {
|
|
|
|
+ temp.delete();
|
|
|
|
+ }
|
|
|
|
+ if (temp.isDirectory()) {
|
|
|
|
+// delAllFile(path + "/" + tempList[i]);//先删除文件夹里面的文件
|
|
|
|
+// delFolder(path + "/" + tempList[i]);//再删除空文件夹
|
|
|
|
+ delAllFile(path + tempList[i]);//先删除文件夹里面的文件
|
|
|
|
+ delFolder(path + tempList[i]);//再删除空文件夹
|
|
|
|
+ flag = true;
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ return flag;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ /**
|
|
|
|
+ * 删除文件夹
|
|
|
|
+ * @param folderPath 文件夹完整绝对路径
|
|
|
|
+ */
|
|
|
|
+ public static void delFolder(String folderPath) {
|
|
|
|
+ try {
|
|
|
|
+ delAllFile(folderPath); //删除完里面所有内容
|
|
|
|
+ String filePath = folderPath;
|
|
|
|
+ filePath = filePath.toString();
|
|
|
|
+ File myFilePath = new File(filePath);
|
|
|
|
+ myFilePath.delete(); //删除空文件夹
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ public static void main(String[] args) {
|
|
|
|
+ String a = "aaaa"+ File.separator + "1111.bb";
|
|
|
|
+ String b = "aaaa\\1111.bb";
|
|
|
|
+ System.out.println(a.substring(a.lastIndexOf(File.separator)+1));
|
|
|
|
+ System.out.println(b.substring(b.lastIndexOf(File.separator)+1));
|
|
|
|
+ }
|
|
|
|
+}
|