RestTemplateTest.java 4.34 KB
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 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();
        }
    }

}