Java 工具类整理汇总

记录下来一些常用的,或用过的,会持续更新 …

XML、JSON 字符串互转

2021-01-29 10:21:54

有时候,我们需要“xmlStr -> jsonStr -> xmlStr”

1)、导入依赖

1
2
3
4
5
6
<!-- https://mvnrepository.com/artifact/cn.hutool/hutool-all -->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.5.7</version>
</dependency>

2)、代码示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;


//2021/1/29 上午10:05 xmlStr -> jsonStr -> xmlStr
public static void main(String[] args) {
//<sites>
// <site>
// <name>菜鸟教程</name>
// <url>www.runoob.com</url>
// </site>
// <site>
// <name>Google</name>
// <url>www.google.com</url>
// </site>
// <site>
// <name>淘宝</name>
// <url>www.taobao.com</url>
// </site>
//</sites>


//<sites><site><name>菜鸟教程</name><url>www.runoob.com</url></site><site><name>Google</name><url>www.google.com</url></site><site><name>淘宝</name><url>www.taobao.com</url></site></sites>
String xmlStr = "<sites><site><name>菜鸟教程</name><url>www.runoob.com</url></site><site><name>Google</name><url>www.google.com</url></site><site><name>淘宝</name><url>www.taobao.com</url></site></sites>";


// xmlStr -> jsonStr
String jsonStr = JSONUtil.xmlToJson(xmlStr).toString();
//{"sites":{"site":[{"url":"www.runoob.com","name":"菜鸟教程"},{"url":"www.google.com","name":"Google"},{"url":"www.taobao.com","name":"淘宝"}]}}


// jsonStr -> xmlStr
String xmlStr1 = JSONUtil.toXmlStr(new JSONObject(jsonStr));
//<sites><site><url>www.runoob.com</url><name>菜鸟教程</name></site><site><url>www.google.com</url><name>Google</name></site><site><url>www.taobao.com</url><name>淘宝</name></site></sites>

}

Ftp

2021-01-21 16:01

1)、导入依赖(hutool 封装的 ftp 工具依赖 commons-net)

1
2
3
4
5
6
7
8
9
10
11
12
<!-- https://mvnrepository.com/artifact/commons-net/commons-net -->
<dependency>
<groupId>commons-net</groupId>
<artifactId>commons-net</artifactId>
<version>3.7.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/cn.hutool/hutool-all -->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.5.7</version>
</dependency>

2)、代码示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import cn.hutool.core.io.IoUtil;
import cn.hutool.extra.ftp.Ftp;
import org.apache.commons.net.ftp.FTPFile;
import java.io.InputStream;


//2021-01-21 16:01 测试 ftp
public static void main(String[] args) throws Exception {
Ftp ftp = new Ftp("121.127.241.19", 21, "ftp", "ftp");
//上传,下载
//ftp.upload("/opt/upload", FileUtil.file("e:/test.jpg"));
//ftp.download("/opt/upload", "test.jpg", FileUtil.file("e:/test2.jpg"));

//查看文件列表
FTPFile[] ftpFiles = ftp.lsFiles("/");
FTPFile ftpFile = ftpFiles[0];
String name = ftpFile.getName();// a


// 上传:会把原有的覆盖
String uploadStr = "我是愤怒1";
ftp.upload("/", "test_upload", IoUtil.toUtf8Stream(uploadStr));


// 读取数据
InputStream inputStream = ftp.getClient().retrieveFileStream("/a");
String read = IoUtil.readUtf8(inputStream);

//ftp inputStream处理完毕,记得 reply,防止下次获取 inputStream == null
ftp.getClient().getReply();

//关闭连接
ftp.close();
}

ExceptionUtil

2022-01-22 21:45:08

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package com.alibaba.nacos.common.utils;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;

/**
* Common methods for exception.
*
* @author nkorange
* @since 1.2.0
*/
public class ExceptionUtil {

/**
* Represents an empty exception, that is, no exception occurs, only a constant.
*/
public static final Exception NONE_EXCEPTION = new RuntimeException("");

public static String getAllExceptionMsg(Throwable e) {
Throwable cause = e;
StringBuilder strBuilder = new StringBuilder();

while (cause != null && !StringUtils.isEmpty(cause.getMessage())) {
strBuilder.append("caused: ").append(cause.getMessage()).append(";");
cause = cause.getCause();
}

return strBuilder.toString();
}

public static Throwable getCause(final Throwable t) {
final Throwable cause = t.getCause();
if (Objects.isNull(cause)) {
return t;
}
return cause;
}

public static String getStackTrace(final Throwable t) {
if (t == null) {
return "";
}

final ByteArrayOutputStream out = new ByteArrayOutputStream();
final PrintStream ps = new PrintStream(out);
t.printStackTrace(ps);
ps.flush();
return new String(out.toByteArray());
}
}