代码拉取完成,页面将自动刷新
自定义的工具类,不需要进行任何配置,只需要在pom中加入依赖即可使用,为了减轻各个项目重复造轮子的问题,开始编写该项目。
jdk 1.8
v1.0.0
第一种:
mvn clean install
打包到本地仓库中。 <dependency>
<groupId>com.suhuamo</groupId>
<artifactId>util</artifactId>
<!-- 版本可自行配置,1.0.0版本一定可以运行-->
<version>1.0.0</version>
</dependency>
第二种:
本项目已经上传到了jitPack的中央仓库中,故用户可以直接通过maven下拉使用,pom.xml中的配置如下,
<repositories>
<!-- 配置jitpack的远程仓库-->
<repository>
<id>jitpack.io</id>
<url>https://www.jitpack.io</url>
</repository>
</repositories>
<dependencies>
<dependency>
<groupId>com.github.suhuamo</groupId>
<artifactId>util</artifactId>
<!-- 版本可自行配置,1.0.0版本一定可以运行-->
<version>1.0.0</version>
</dependency>
</dependencies>
可进行 Http进行请求
public class HttpTest {
@Test
public void sendGet() {
// 请求地址
String url = "http://www.baidu.com";
// 获取返回值
JsonObject jsonObject = HttpUtil.sendGet(url);
System.out.println("jsonObject = " + jsonObject);
// 拼接参数
Map<String, String> params = new HashMap<>();
params.put("name", "小花");
params.put("age", "18");
jsonObject = HttpUtil.sendGet(url, params);
System.out.println("jsonObject = " + jsonObject);
// 请求头参数
Map<String, String> headers = new HashMap<>();
headers.put("Authoration", "token xxx123");
jsonObject = HttpUtil.sendGet(url, params, headers);
System.out.println("jsonObject = " + jsonObject);
}
// Put和Delete同理
@Test
public void sendPost() {
// 请求地址
String url = "http://www.baidu.com";
// 请求数据
Person person = new Person();
// 获取返回值
JsonObject jsonObject = HttpUtil.sendPost(url, person);
System.out.println("jsonObject = " + jsonObject);
// 请求头参数
Map<String, String> headers = new HashMap<>();
headers.put("Authoration", "token xxx123");
jsonObject = HttpUtil.sendPost(url, person, headers);
System.out.println("jsonObject = " + jsonObject);
}
class Person {
String name;
Integer age;
}
}
public class ExcelTest {
@Test
public void readExcelTest() throws Exception {
// 文件地址
String file = "E:\\info\\test\\test.xlsx";
// 读取文件数据
List<Map<Integer, Object>> maps = ExcelUtil.readExcel(new File(file));
maps.forEach(e -> {
e.forEach((k, v) -> {
System.out.println(k + ": " + v);
});
});
}
@Test
public void writeExcelTest() throws Exception {
// 文件地址
String file = "E:\\info\\test\\write.xlsx";
// 存入的数据
List<List<Object>> data = new ArrayList<>();
for (int i = 0; i < 10; i++) {
List<Object> row = new ArrayList<>();
for (int j = 0; j < 10; j++) {
row.add(j);
}
data.add(row);
}
// 写入文件数据
ExcelUtil.writeExcel(new File(file),data);
}
}
public class FileTest {
@Test
public void getResourceFilePathTest() throws ClassNotFoundException {
String resourceFilePath = FileUtil.getResourceFilePath("t/2.txt");
System.out.println("resourceFilePath = " + resourceFilePath);
}
@Test
public void readFileTest() throws ClassNotFoundException, IOException {
// 获取resource文件夹下的1.txt文件的绝对路径
String resourceFilePath = FileUtil.getResourceFilePath("1.txt");
// 读取文件的内容
String s = FileUtil.readFile(resourceFilePath);
System.out.println("s = " + s);
}
@Test
public void writeFile() throws ClassNotFoundException, IOException {
// 获取resource文件夹下的1.txt文件的绝对路径
String resourceFilePath = FileUtil.getResourceFilePath("1.txt");
String content = "你好呀\n欢迎使用FileUtil\nO(∩_∩)O";
// 覆盖方式写入
FileUtil.writeFile(resourceFilePath, content);
// 追加方式写入
FileUtil.writeFile(resourceFilePath, content, true);
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。