File tree 3 files changed +125
-0
lines changed
src/main/java/com/luna/commons
3 files changed +125
-0
lines changed Original file line number Diff line number Diff line change
1
+ package com .luna .commons .exception ;
2
+
3
+ import com .luna .commons .exception .base .BaseException ;
4
+
5
+ /**
6
+ * 文件信息异常类
7
+ *
8
+ * @author ruoyi
9
+ */
10
+ public class FileException extends BaseException {
11
+ private static final long serialVersionUID = 1L ;
12
+
13
+ public FileException (int code , String message , Object [] args ) {
14
+ super (code , message , args );
15
+ }
16
+
17
+ public FileException (int code , String message ) {
18
+ super (code , message );
19
+ }
20
+ }
Original file line number Diff line number Diff line change
1
+ package com .luna .commons .exception .base ;
2
+
3
+ /**
4
+ * 异常类
5
+ *
6
+ * @author 15272
7
+ *
8
+ */
9
+ public class BaseException extends RuntimeException {
10
+
11
+ private int code ;
12
+ private String message ;
13
+
14
+ /**
15
+ * 错误码对应的参数
16
+ */
17
+ private Object [] args ;
18
+
19
+ public BaseException () {
20
+ super ();
21
+ }
22
+
23
+ public BaseException (int code , String message ) {
24
+ this .code = code ;
25
+ this .message = message ;
26
+ }
27
+
28
+ public BaseException (int code , String message , Object [] args ) {
29
+ this .code = code ;
30
+ this .message = message ;
31
+ this .args = args ;
32
+ }
33
+
34
+ public int getCode () {
35
+ return code ;
36
+ }
37
+
38
+ @ Override
39
+ public String getMessage () {
40
+ return message ;
41
+ }
42
+
43
+ }
Original file line number Diff line number Diff line change
1
+ package com .luna .commons .file ;
2
+
3
+ import com .luna .commons .baidu .Config .GetBaiduKey ;
4
+ import com .luna .commons .dto .constant .ResultCode ;
5
+ import com .luna .commons .exception .FileException ;
6
+ import org .slf4j .Logger ;
7
+ import org .slf4j .LoggerFactory ;
8
+
9
+ import java .io .*;
10
+
11
+ import javax .imageio .stream .FileImageOutputStream ;
12
+
13
+ /**
14
+ * 图片文件,与 byte[] 互转
15
+ */
16
+ public class ImageUtils {
17
+
18
+ private static final Logger log = LoggerFactory .getLogger (GetBaiduKey .class );
19
+
20
+ /**
21
+ * 图片转字节
22
+ *
23
+ * @param imgFile
24
+ * @return
25
+ */
26
+ public static byte [] getBytes (String imgFile ) {
27
+ // 将图片文件转化为字节数组
28
+
29
+ InputStream in = null ;
30
+ byte [] data = null ;
31
+ // 读取图片字节数组
32
+ try {
33
+ in = new FileInputStream (imgFile );
34
+ data = new byte [in .available ()];
35
+ in .read (data );
36
+ in .close ();
37
+ } catch (IOException e ) {
38
+ e .printStackTrace ();
39
+ }
40
+ return data ;
41
+ }
42
+
43
+ /**
44
+ * 字节转图片
45
+ *
46
+ * @param data
47
+ * @param path
48
+ */
49
+ public static void byte2image (byte [] data , String path ) {
50
+ if (data .length < 3 || path .equals ("" )) {
51
+ return ;
52
+ }
53
+ try {
54
+ FileImageOutputStream imageOutput = new FileImageOutputStream (new File (path ));
55
+ imageOutput .write (data , 0 , data .length );
56
+ imageOutput .close ();
57
+ log .info ("Make Picture success,Please find image in " + path );
58
+ } catch (Exception ex ) {
59
+ throw new FileException (ResultCode .PARAMETER_INVALID , "Exception: " + ex );
60
+ }
61
+ }
62
+ }
You can’t perform that action at this time.
0 commit comments