RestTemplateTest.java
4.48 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package com.topdraw.resttemplate;
import com.alibaba.fastjson.JSONObject;
import com.topdraw.BaseTest;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.util.IOUtils;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import java.io.*;
import java.lang.reflect.Field;
import java.time.LocalDate;
import java.time.LocalDateTime;
public class RestTemplateTest extends BaseTest {
@Autowired
RestTemplateClient apiUtil;
@Test
public void t(){
JSONObject memberInfo = this.apiUtil.getMemberInfo(5L);
System.out.println(memberInfo);
}
@Test
public void error(){
String msg3 = "{\n" +
" \"evt\": \"play\", \n" +
" \"deviceType\": 1, \n" +
// " \"time\": \"2022-04-14 00:10:09\",\n" +
" \"time\": \"2022-05-03 23:10:09\",\n" +
" \"msgData\": {\n" +
" \"platformAccount\": \"topdraw\", \n" +
" \"playDuration\": "+23+", \n" +
" \"mediaCode\": \"\", \n" +
" \"mediaName\": \"\"\n" +
" }\n" +
" }";
String classpathUrlPrefix = ApplicationContext.CLASSPATH_URL_PREFIX;
String panfu = "";
// String filePath = classpathUrlPrefix+"/mq/play/error.log";
String property = System.getProperty("user.dir");
String separator = File.separator;
System.out.println(property);
String fileName = "error_"+ LocalDate.now() +".log";
String filePath = property+separator+"logs"+separator+"mq"+separator+fileName;
createFile(filePath);
this.writeStringToFile2(filePath, msg3);
String a = "{\n" +
"\"data\": [\n" +
"\t{\n" +
"\t\"app_id\": 57,\n" +
"\t\"user_id\": 1,\n" +
"\t\"type\": 1,\n" +
"\t\"name\": \"PersonalCollectionRecords\",\n" +
"\t\"count\": 22,\n" +
"\t\"images\": \"{\\\"map\\\":{\\\"poster\\\":[0]},\\\"list\\\":[{\\\"id\\\":47422,\\\"type\\\":2,\\\"width\\\":222,\\\"height\\\":294,\\\"fileUrl\\\":\\\"upload/image/media/2020-07-30/9a8a02db-9444-4bff-ba54-ea784ae4f88c.jpg\\\",\\\"size\\\":104643}]}\",\n" +
"\t\"id\": 756756,\n" +
"\t\"user_collection_id\": 1,\n" +
"\t\"detail_folder_code\": \"Default\",\n" +
"\t\"detail_type\": \"MEDIA\",\n" +
"\t\"detail_id\": 46532,\n" +
"\t\"detail_code\": \"media_558bc45a-5480-46ec-be9a-c749ffdbdf49\",\n" +
"\t\"detail_name\": \"熊出没之探险日记2\",\n" +
"\t\"detail_total_index\": 40,\n" +
"\t\"detail_sequence\": 1,\n" +
"\t\"create_time\": 1644503167000,\n" +
"\t\"update_time\": 1644503167000\n" +
"\t}\n" +
"\t],\n" +
"\"platformAccount\": \"topdraw\"\n" +
"}";
}
private void createFile(String filePath){
File testFile = new File(filePath);
File fileParent = testFile.getParentFile();//返回的是File类型,可以调用exsit()等方法
String fileParentPath = testFile.getParent();//返回的是String类型
System.out.println("fileParent:" + fileParent);
System.out.println("fileParentPath:" + fileParentPath);
if (!fileParent.exists()) {
fileParent.mkdirs();// 能创建多级目录
}
if (!testFile.exists()) {
try {
testFile.createNewFile();//有路径才能创建文件
} catch (IOException e) {
e.printStackTrace();
}
}
System.out.println(testFile);
}
public void writeStringToFile2(String filePath, String error) {
try {
FileWriter fw = new FileWriter(filePath, true);
BufferedWriter bw = new BufferedWriter(fw);
bw.append(LocalDateTime.now()+"=>"+error+"\n");
//bw.write("我是");// 往已有的文件上添加字符串
//bw.write("程序猿\n ");
bw.close();
fw.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}