Skip to content

Commit 8735b61

Browse files
author
yangzhao
committed
Revert "新增图片工具类 将图片内容转换为base64"
This reverts commit a9cd505.
1 parent a9cd505 commit 8735b61

File tree

4 files changed

+203
-292
lines changed

4 files changed

+203
-292
lines changed

common-core/src/main/java/com/yz/common/core/utils/FileUtils.java

Lines changed: 158 additions & 204 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
import org.slf4j.Logger;
44
import org.slf4j.LoggerFactory;
5-
import sun.misc.BASE64Encoder;
6-
75
import java.awt.image.BufferedImage;
86
import java.io.*;
97
import java.nio.ByteBuffer;
@@ -12,208 +10,164 @@
1210

1311
public class FileUtils extends org.apache.commons.io.FileUtils {
1412

15-
private static final Logger logger = LoggerFactory.getLogger(FileUtils.class);
16-
17-
/**
18-
* 读取json文件转对象
19-
*
20-
* @param path :项目根目录下的路径
21-
* @param clazz
22-
* @return
23-
*/
24-
public static <T> T readJsonFileToObject(String path, Class<T> clazz) {
25-
path = FileUtils.class.getResource(path).getFile().toString();
26-
File file = new File(path);
27-
try {
28-
String data = readFileToString(file);
29-
return com.alibaba.fastjson.JSON.parseObject(data, clazz);
30-
} catch (IOException e) {
31-
e.printStackTrace();
32-
}
33-
return null;
34-
}
35-
36-
/**
37-
* 删除文件
38-
*
39-
* @param path
40-
* @return
41-
*/
42-
public static boolean delFile(String path) {
43-
try {
44-
File file = new File(path);
45-
file.delete();
46-
return true;
47-
} catch (Exception e) {
48-
logger.error("文件删除失败---- 路径:" + path);
49-
}
50-
return false;
51-
}
52-
53-
/**
54-
* NIO模式读取
55-
*
56-
* @param path
57-
* @param model
58-
* @return
13+
private static final Logger logger= LoggerFactory.getLogger(FileUtils.class);
14+
15+
/**
16+
* 读取json文件转对象
17+
* @param path :项目根目录下的路径
18+
* @param clazz
19+
* @return
20+
*/
21+
public static <T> T readJsonFileToObject(String path, Class<T> clazz) {
22+
path = FileUtils.class.getResource(path).getFile().toString();
23+
File file = new File(path);
24+
try {
25+
String data = readFileToString(file);
26+
return com.alibaba.fastjson.JSON.parseObject(data,clazz);
27+
} catch (IOException e) {
28+
e.printStackTrace();
29+
}
30+
return null;
31+
}
32+
33+
/**
34+
* 删除文件
35+
* @param path
36+
* @return
37+
*/
38+
public static boolean delFile(String path) {
39+
try {
40+
File file = new File(path);
41+
file.delete();
42+
return true;
43+
} catch (Exception e) {
44+
logger.error("文件删除失败---- 路径:" + path);
45+
}
46+
return false;
47+
}
48+
/**
49+
* NIO模式读取
50+
* @param path
51+
* @param model
52+
* @return
53+
*/
54+
public static byte[] readFile(String path, String model) {
55+
RandomAccessFile file = null;
56+
try {
57+
file = new RandomAccessFile(path, model);
58+
FileChannel channel = file.getChannel();
59+
int size = (int) channel.size();
60+
ByteBuffer buffer = ByteBuffer.allocate(size);
61+
channel.read(buffer);
62+
byte[] array = buffer.array();
63+
return array;
64+
} catch (Exception e) {
65+
e.printStackTrace();
66+
} finally {
67+
try {
68+
file.close();
69+
} catch (IOException e) {
70+
e.printStackTrace();
71+
}
72+
}
73+
return null;
74+
}
75+
/**
76+
* BIO模式读取
77+
* @param path
78+
* @param fileSize
79+
* @return
80+
*/
81+
public static byte[] readFile(String path, int fileSize) {
82+
FileInputStream fis = null;
83+
try {
84+
fis = new FileInputStream(path);
85+
byte[] data = new byte[fileSize];
86+
fis.read(data);
87+
return data;
88+
} catch (Exception e) {
89+
e.printStackTrace();
90+
} finally {
91+
try {
92+
fis.close();
93+
} catch (IOException e) {
94+
e.printStackTrace();
95+
}
96+
}
97+
return null;
98+
}
99+
100+
/**
101+
* 获取图片的宽和高
102+
* @param inputStream
103+
* @return
59104
*/
60-
public static byte[] readFile(String path, String model) {
61-
RandomAccessFile file = null;
62-
try {
63-
file = new RandomAccessFile(path, model);
64-
FileChannel channel = file.getChannel();
65-
int size = (int) channel.size();
66-
ByteBuffer buffer = ByteBuffer.allocate(size);
67-
channel.read(buffer);
68-
byte[] array = buffer.array();
69-
return array;
70-
} catch (Exception e) {
71-
e.printStackTrace();
72-
} finally {
73-
try {
74-
file.close();
75-
} catch (IOException e) {
76-
e.printStackTrace();
77-
}
78-
}
79-
return null;
80-
}
81-
82-
/**
83-
* BIO模式读取
84-
*
85-
* @param path
86-
* @param fileSize
87-
* @return
88-
*/
89-
public static byte[] readFile(String path, int fileSize) {
90-
FileInputStream fis = null;
91-
try {
92-
fis = new FileInputStream(path);
93-
byte[] data = new byte[fileSize];
94-
fis.read(data);
95-
return data;
96-
} catch (Exception e) {
97-
e.printStackTrace();
98-
} finally {
99-
try {
100-
fis.close();
101-
} catch (IOException e) {
102-
e.printStackTrace();
103-
}
104-
}
105-
return null;
106-
}
107-
108-
/**
109-
* 获取图片的宽和高
110-
*
111-
* @param inputStream
112-
* @return
113-
*/
114-
public static String getImageProperty(InputStream inputStream) {
115-
try {
116-
BufferedImage bufferedImage = ImageIO.read(inputStream);
117-
int width = bufferedImage.getWidth();
118-
int height = bufferedImage.getHeight();
119-
logger.info("宽度:" + width + "----" + "高度:" + height);
120-
return width + "," + height;
121-
} catch (IOException e) {
122-
e.printStackTrace();
123-
}
124-
return null;
125-
}
126-
127-
/**
128-
* 获取图片的宽和高
129-
*
130-
* @param file
131-
* @return
132-
*/
133-
public static String getImageProperty(File file) {
134-
try {
135-
BufferedImage bufferedImage = ImageIO.read(file);
136-
int width = bufferedImage.getWidth();
137-
int height = bufferedImage.getHeight();
138-
logger.info("宽度:" + width + "----" + "高度:" + height);
139-
return width + "," + height;
140-
} catch (IOException e) {
141-
e.printStackTrace();
142-
}
143-
return null;
144-
}
145-
146-
/**
147-
* 文件重命名
148-
*
149-
* @param path
150-
* @param newFileName
151-
*/
152-
public static void fileRename(String path, String newFileName) {
153-
File oldFile = new File(path);
154-
String parent = oldFile.getParent();
155-
File newFile = new File(parent + "/" + newFileName);
156-
oldFile.renameTo(newFile);
157-
}
158-
159-
/**
160-
* 递归读取文件所在路径
161-
*
162-
* @param filePath 路径
163-
* @param fileName 文件名
164-
* @return
165-
*/
166-
public static String recursionReadFile(String filePath, String fileName) {
167-
File file = new File(filePath);
168-
File[] files = file.listFiles();
169-
for (File f : files) {
170-
if (f.getName().equals(fileName)) {
171-
return file.getAbsolutePath() + "/" + f.getName();
172-
}
173-
}
174-
175-
for (File f : files) {
176-
String readPath = filePath + "/" + f.getName();
177-
file = new File(readPath);
178-
if (file.isDirectory()) {
179-
return recursionReadFile(readPath, fileName);
180-
}
181-
}
182-
183-
return null;
184-
}
185-
186-
/**
187-
* 文件内容转换成base64字符串
188-
*
189-
* @param imgFile
190-
* @return
191-
*/
192-
public static String fileContentToBase64(File imgFile) {// 将图片文件转化为字节数组字符串,并对其进行Base64编码处理
193-
byte[] data = null;
194-
// 读取图片字节数组
195-
try (
196-
InputStream in = new FileInputStream(imgFile);
197-
) {
198-
data = new byte[in.available()];
199-
in.read(data);
200-
} catch (IOException e) {
201-
e.printStackTrace();
202-
}
203-
// 对字节数组Base64编码
204-
BASE64Encoder encoder = new BASE64Encoder();
205-
// 返回Base64编码过的字节数组字符串
206-
return encoder.encode(data);
207-
}
208-
209-
/**
210-
* 文件内容转换成base64字符串
211-
*
212-
* @param imgFilePath 图片本地路径
213-
* @return
105+
public static String getImageProperty(InputStream inputStream){
106+
try {
107+
BufferedImage bufferedImage = ImageIO.read(inputStream);
108+
int width = bufferedImage.getWidth();
109+
int height = bufferedImage.getHeight();
110+
logger.info("宽度:"+width+"----"+"高度:"+height);
111+
return width+","+height;
112+
} catch (IOException e) {
113+
e.printStackTrace();
114+
}
115+
return null;
116+
}
117+
118+
/**
119+
* 获取图片的宽和高
120+
* @param file
121+
* @return
214122
*/
215-
public static String fileContentToBase64(String imgFilePath) {// 将图片文件转化为字节数组字符串,并对其进行Base64编码处理
216-
File file = new File(imgFilePath);
217-
return fileContentToBase64(file);
218-
}
123+
public static String getImageProperty(File file){
124+
try {
125+
BufferedImage bufferedImage = ImageIO.read(file);
126+
int width = bufferedImage.getWidth();
127+
int height = bufferedImage.getHeight();
128+
logger.info("宽度:"+width+"----"+"高度:"+height);
129+
return width+","+height;
130+
} catch (IOException e) {
131+
e.printStackTrace();
132+
}
133+
return null;
134+
}
135+
136+
/**
137+
* 文件重命名
138+
* @param path
139+
* @param newFileName
140+
*/
141+
public static void fileRename(String path,String newFileName){
142+
File oldFile = new File(path);
143+
String parent = oldFile.getParent();
144+
File newFile = new File(parent+"/"+newFileName);
145+
oldFile.renameTo(newFile);
146+
}
147+
148+
/**
149+
* 递归读取文件所在路径
150+
* @param filePath 路径
151+
* @param fileName 文件名
152+
* @return
153+
*/
154+
public static String recursionReadFile(String filePath,String fileName){
155+
File file = new File(filePath);
156+
File[] files = file.listFiles();
157+
for (File f:files){
158+
if (f.getName().equals(fileName)){
159+
return file.getAbsolutePath()+"/"+f.getName();
160+
}
161+
}
162+
163+
for (File f:files){
164+
String readPath=filePath+"/"+f.getName();
165+
file = new File(readPath);
166+
if (file.isDirectory()){
167+
return recursionReadFile(readPath, fileName);
168+
}
169+
}
170+
171+
return null;
172+
}
219173
}

0 commit comments

Comments
 (0)