|
@@ -4,14 +4,11 @@ package cn.cslg.pas.common.utils;
|
|
import cn.cslg.pas.Application;
|
|
import cn.cslg.pas.Application;
|
|
import cn.hutool.core.util.IdUtil;
|
|
import cn.hutool.core.util.IdUtil;
|
|
import com.alibaba.fastjson.JSON;
|
|
import com.alibaba.fastjson.JSON;
|
|
-import org.apache.commons.fileupload.FileItem;
|
|
|
|
-import org.apache.commons.fileupload.disk.DiskFileItemFactory;
|
|
|
|
import org.springframework.boot.system.ApplicationHome;
|
|
import org.springframework.boot.system.ApplicationHome;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.stereotype.Service;
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
import java.io.*;
|
|
import java.io.*;
|
|
-import java.net.MalformedURLException;
|
|
|
|
import java.net.URL;
|
|
import java.net.URL;
|
|
import java.nio.charset.StandardCharsets;
|
|
import java.nio.charset.StandardCharsets;
|
|
|
|
|
|
@@ -28,13 +25,11 @@ public class FileUtils {
|
|
//获取 applicationHome 内的路径 ...\target\classes 到这一层级下
|
|
//获取 applicationHome 内的路径 ...\target\classes 到这一层级下
|
|
File file = ah.getSource();
|
|
File file = ah.getSource();
|
|
//获取 file的parentFile 即最后一级之前的所有层级路径(包括盘符) 这里能获得到的最终层级为 ...\target 后续用FILE_SEPARATOR(系统路径分割通配符 即 "\") 以及fileName拼接生成存放文件的目录层级 即为根目录 root
|
|
//获取 file的parentFile 即最后一级之前的所有层级路径(包括盘符) 这里能获得到的最终层级为 ...\target 后续用FILE_SEPARATOR(系统路径分割通配符 即 "\") 以及fileName拼接生成存放文件的目录层级 即为根目录 root
|
|
- String rootPath =null;
|
|
|
|
|
|
+ String rootPath = null;
|
|
|
|
|
|
- if (fileName!=null&&!fileName.equals(""))
|
|
|
|
- {
|
|
|
|
- rootPath= file.getParentFile().toString() + FILE_SEPARATOR + fileName;
|
|
|
|
- }
|
|
|
|
- else {
|
|
|
|
|
|
+ if (fileName != null && !fileName.equals("")) {
|
|
|
|
+ rootPath = file.getParentFile().toString() + FILE_SEPARATOR + fileName;
|
|
|
|
+ } else {
|
|
rootPath = file.getParentFile().toString();
|
|
rootPath = file.getParentFile().toString();
|
|
}
|
|
}
|
|
//根据上方生成的根目录路径 生成对应文件夹 没有就新建
|
|
//根据上方生成的根目录路径 生成对应文件夹 没有就新建
|
|
@@ -77,7 +72,7 @@ public class FileUtils {
|
|
return last.toString();
|
|
return last.toString();
|
|
}
|
|
}
|
|
|
|
|
|
- public static File getFileByBytes(byte[] bytes, String prefix, String suffix) {
|
|
|
|
|
|
+ public static File getFileByBytes(byte[] bytes, String prefix, String suffix) {
|
|
BufferedOutputStream bos = null;
|
|
BufferedOutputStream bos = null;
|
|
FileOutputStream fos = null;
|
|
FileOutputStream fos = null;
|
|
File file = null;
|
|
File file = null;
|
|
@@ -111,12 +106,12 @@ public class FileUtils {
|
|
e.printStackTrace();
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
- return file;
|
|
|
|
|
|
+ return file;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
- public static File getFileByUrl(String dataUrl) throws IOException {
|
|
|
|
|
|
+ public static File getFileByUrl(String dataUrl) throws IOException {
|
|
|
|
|
|
URL url = new URL(dataUrl); //想要读取的url地址
|
|
URL url = new URL(dataUrl); //想要读取的url地址
|
|
InputStream in = url.openStream();
|
|
InputStream in = url.openStream();
|
|
@@ -133,7 +128,8 @@ public class FileUtils {
|
|
in.close();
|
|
in.close();
|
|
return file;
|
|
return file;
|
|
}
|
|
}
|
|
- public static File getPictureFileByUrl(String dataUrl) throws IOException {
|
|
|
|
|
|
+
|
|
|
|
+ public static File getPictureFileByUrl(String dataUrl) throws IOException {
|
|
|
|
|
|
URL url = new URL(dataUrl); //想要读取的url地址
|
|
URL url = new URL(dataUrl); //想要读取的url地址
|
|
InputStream in = url.openStream();
|
|
InputStream in = url.openStream();
|
|
@@ -150,9 +146,11 @@ public class FileUtils {
|
|
in.close();
|
|
in.close();
|
|
return file;
|
|
return file;
|
|
}
|
|
}
|
|
|
|
+
|
|
public String getPath(String url) {
|
|
public String getPath(String url) {
|
|
return getStaticPath(COMMON_FILE) + url;
|
|
return getStaticPath(COMMON_FILE) + url;
|
|
}
|
|
}
|
|
|
|
+
|
|
public String getDirectoryName() {
|
|
public String getDirectoryName() {
|
|
return DateUtils.getNowTimeFormat("yyyyMMdd");
|
|
return DateUtils.getNowTimeFormat("yyyyMMdd");
|
|
}
|
|
}
|
|
@@ -160,6 +158,7 @@ public class FileUtils {
|
|
public String getSavePath(String directoryName) {
|
|
public String getSavePath(String directoryName) {
|
|
return getStaticPath(COMMON_FILE) + FILE_SEPARATOR + directoryName + FILE_SEPARATOR;
|
|
return getStaticPath(COMMON_FILE) + FILE_SEPARATOR + directoryName + FILE_SEPARATOR;
|
|
}
|
|
}
|
|
|
|
+
|
|
public String createDirectory() {
|
|
public String createDirectory() {
|
|
String directoryName = this.getDirectoryName();
|
|
String directoryName = this.getDirectoryName();
|
|
String savePath = this.getSavePath(directoryName);
|
|
String savePath = this.getSavePath(directoryName);
|
|
@@ -169,8 +168,9 @@ public class FileUtils {
|
|
}
|
|
}
|
|
return directoryName;
|
|
return directoryName;
|
|
}
|
|
}
|
|
|
|
+
|
|
public String createRandomDirectory() {
|
|
public String createRandomDirectory() {
|
|
- String directoryName = IdUtil.simpleUUID();
|
|
|
|
|
|
+ String directoryName = IdUtil.simpleUUID();
|
|
String savePath = this.getSavePath(directoryName);
|
|
String savePath = this.getSavePath(directoryName);
|
|
File directory = new File(savePath);
|
|
File directory = new File(savePath);
|
|
if (!directory.exists()) {
|
|
if (!directory.exists()) {
|
|
@@ -178,6 +178,7 @@ public class FileUtils {
|
|
}
|
|
}
|
|
return directoryName;
|
|
return directoryName;
|
|
}
|
|
}
|
|
|
|
+
|
|
public static FileInputStream byteToFile(byte[] bytes) {
|
|
public static FileInputStream byteToFile(byte[] bytes) {
|
|
String fileName = IdUtil.simpleUUID() + ".png";
|
|
String fileName = IdUtil.simpleUUID() + ".png";
|
|
File file = new File(fileName);
|
|
File file = new File(fileName);
|
|
@@ -209,13 +210,16 @@ public class FileUtils {
|
|
public String getDirectory(String fileName) {
|
|
public String getDirectory(String fileName) {
|
|
return FILE_SEPARATOR + this.createDirectory() + FILE_SEPARATOR + fileName;
|
|
return FILE_SEPARATOR + this.createDirectory() + FILE_SEPARATOR + fileName;
|
|
}
|
|
}
|
|
|
|
+
|
|
public String getSystemPath(String url) {
|
|
public String getSystemPath(String url) {
|
|
return getStaticPath(COMMON_FILE) + FILE_SEPARATOR + url;
|
|
return getStaticPath(COMMON_FILE) + FILE_SEPARATOR + url;
|
|
}
|
|
}
|
|
|
|
+
|
|
public static String getSystemPath2(String url) {
|
|
public static String getSystemPath2(String url) {
|
|
return getStaticPath(COMMON_FILE) + FILE_SEPARATOR + url;
|
|
return getStaticPath(COMMON_FILE) + FILE_SEPARATOR + url;
|
|
}
|
|
}
|
|
- public static void writeFile(String json, String FilePath) {
|
|
|
|
|
|
+
|
|
|
|
+ public static void writeFile(String json, String FilePath) {
|
|
|
|
|
|
try {
|
|
try {
|
|
File file = new File(FilePath);
|
|
File file = new File(FilePath);
|
|
@@ -238,6 +242,7 @@ public class FileUtils {
|
|
e.printStackTrace();
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+
|
|
public static File getFileByName(File file, String name) {
|
|
public static File getFileByName(File file, String name) {
|
|
for (File file1 : file.listFiles()) {
|
|
for (File file1 : file.listFiles()) {
|
|
if (file1.getName().equals(name)) {
|
|
if (file1.getName().equals(name)) {
|
|
@@ -248,10 +253,9 @@ public class FileUtils {
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
- public static void writeFile(Object object,File file) {
|
|
|
|
- String json = JSON.toJSONString(object);
|
|
|
|
|
|
+ public static void writeFile(Object object, File file) {
|
|
|
|
+ String json = JSON.toJSONString(object);
|
|
try {
|
|
try {
|
|
-
|
|
|
|
// if file doesnt exists, then create it
|
|
// if file doesnt exists, then create it
|
|
if (!file.exists()) {
|
|
if (!file.exists()) {
|
|
file.createNewFile();
|
|
file.createNewFile();
|
|
@@ -259,7 +263,6 @@ public class FileUtils {
|
|
file.delete();
|
|
file.delete();
|
|
file.createNewFile();
|
|
file.createNewFile();
|
|
}
|
|
}
|
|
-
|
|
|
|
// true = append file
|
|
// true = append file
|
|
FileWriter fileWritter = new FileWriter(file, false);
|
|
FileWriter fileWritter = new FileWriter(file, false);
|
|
BufferedWriter bufferWritter = new BufferedWriter(fileWritter);
|
|
BufferedWriter bufferWritter = new BufferedWriter(fileWritter);
|
|
@@ -270,4 +273,34 @@ public class FileUtils {
|
|
e.printStackTrace();
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ public static File multipartFileToFile(MultipartFile file) throws Exception {
|
|
|
|
+ File toFile = null;
|
|
|
|
+ if (file == null || file.equals("") || file.getSize() <= 0) {
|
|
|
|
+ file = null;
|
|
|
|
+ } else {
|
|
|
|
+ InputStream ins = null;
|
|
|
|
+ ins = file.getInputStream();
|
|
|
|
+ toFile = new File(file.getOriginalFilename());
|
|
|
|
+ inputStreamToFile(ins, toFile);
|
|
|
|
+ ins.close();
|
|
|
|
+ }
|
|
|
|
+ return toFile;
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ //获取流文件
|
|
|
|
+ private static void inputStreamToFile(InputStream ins, File file) {
|
|
|
|
+ try {
|
|
|
|
+ OutputStream os = new FileOutputStream(file);
|
|
|
|
+ int bytesRead = 0;
|
|
|
|
+ byte[] buffer = new byte[8192];
|
|
|
|
+ while ((bytesRead = ins.read(buffer, 0, 8192)) != -1) {
|
|
|
|
+ os.write(buffer, 0, bytesRead);
|
|
|
|
+ }
|
|
|
|
+ os.close();
|
|
|
|
+ ins.close();
|
|
|
|
+ } catch (Exception e) {
|
|
|
|
+ e.printStackTrace();
|
|
|
|
+ }
|
|
|
|
+ }
|
|
}
|
|
}
|