代码片段

发布时间 2023-09-07 09:58:40作者: 小赵同学爱编码
  • 通过反编译生成的,所以没有注解
// GetFileName.java

package com.api.weavernorth.gyrx.web;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.api.weavernorth.gyrx.util.Utils;
import com.engine.common.util.ParamUtil;
import com.icbccs.document.compare.dto.*;
import com.icbccs.document.compare.sevice.impl.FileCompareServiceImpl;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;

public class GetFileName
{

    public GetFileName()
    {
    }

    public JSONArray getdata(HttpServletRequest request, HttpServletResponse response)
    {
        // ParamUtil 是Java提供的一个工具类
        Map map = ParamUtil.request2Map(request);
        String fjid = Util.null2String(map.get("fjid"));
        // 创建了一个 JSON 数组,它的每个值都是一个 JavaScript 对象
        JSONArray stringHashMap = new JSONArray();
        if(!"".equals(fjid))
        {
            RecordSet rs = new RecordSet();
            // 对于封装的 execute 方法,不仅可以传入字符串,还可以传入 StringBuilder 类型的字符串,使用这种类型的字符串的作用是方便参数拼接
            rs.execute((new StringBuilder("SELECT t1.imagefileid,t1.imagefilename FROM ImageFile t1 LEFT JOIN DocImageFile t2 ON t1.imagefileid = t2.imagefileid WHERE t2.docid IN (")).append(fjid).append(") ").append("AND t2.versionId = ( SELECT MAX( t3.versionId ) FROM DocImageFile t3 WHERE t3.id = t2.id )").toString());
            JSONObject jsonObject;
            for(; rs.next(); stringHashMap.add(jsonObject))
            {
                jsonObject = new JSONObject();
                // Util.null2String 的作用就是如果字符串为null,返回 ""
                String imagefileid = Util.null2String(rs.getString("imagefileid"));
                String imagefilename = Util.null2String(rs.getString("imagefilename"));
                jsonObject.put("id", imagefileid);
                jsonObject.put("name", imagefilename);
            }

        }
        return stringHashMap;
    }

    public JSONObject compareFile(HttpServletRequest request, HttpServletResponse response)
    {
        BaseBean bb; // 作用是打印日志,打印日志时调用它的 writeLog 方法
        JSONObject para;
        String fileId1;
        String fileId2;
        bb = new BaseBean();
        para = new JSONObject();
        // 哈哈哈,这是个从请求中获取参数的常用套路
        Map map = ParamUtil.request2Map(request);
        fileId1 = Util.null2String(map.get("fileId1"));
        fileId2 = Util.null2String(map.get("fileId2"));
        bb.writeLog("===========fileId1===========", fileId1);
        bb.writeLog("===========fileId2===========", fileId2);
        CompareFileDto compareFileDto;
        String filePath1;
        String filePath2;
        compareFileDto = new CompareFileDto();
        compareFileDto.setAppId("calliper");
        compareFileDto.setSecret("hello_calliper");
        compareFileDto.setUrl("http://10.29.41.189:30090/calliper");
        filePath1 = Utils.downLoadLocal(fileId1);
        filePath2 = Utils.downLoadLocal(fileId2);
        // 有一个文件路径没拿到就直接 400 代表错误的请求
        if("".equals(filePath1) || "".equals(filePath2))
        {
            para.put("code", "400");
            // msg 的值为 unicode,下载临时失败!
            para.put("msg", "\u4E0B\u8F7D\u4E34\u65F6\u5931\u8D25\uFF01");
            return para;
        }
        try
        {
            bb.writeLog("===========filePath1===========", filePath1);
            bb.writeLog("===========filePath2===========", filePath2);
            compareFileDto.setPath1(fileId1);
            compareFileDto.setPath2(fileId2);
            FileCompareServiceImpl fileCompareService = new FileCompareServiceImpl();
            CompareFileResultDto compareFileResultDto = fileCompareService.compareFile(compareFileDto);
            bb.writeLog("===========compareFileResultDto===========", compareFileResultDto);
            if(CompareFileResultDto.STATUS_SUCCESS.equals(compareFileResultDto.getStatus()))
            {
                CompareFileStatusDto compareFileStatusDto = new CompareFileStatusDto();
                compareFileStatusDto.setAppId("calliper");
                compareFileStatusDto.setSecret("hello_calliper");
                compareFileStatusDto.setUrl("http://10.29.41.189:30090/calliper");
                compareFileStatusDto.setCmpId(compareFileResultDto.getCmpId());
                CompareFileStatusResultDto compareStatus = fileCompareService.getCompareStatus(compareFileStatusDto);
                bb.writeLog("===========compareStatus===========", compareStatus);
                if(CompareFileResultDto.STATUS_SUCCESS.equals(compareStatus.getStatus()))
                    if(CompareFileStatusResultDto.PROCESS_DONE.equals(compareStatus.getCompareProcess()))
                    {
                        para.put("code", "200");
                        para.put("msg", compareStatus.getDiffUrl());
                    } else
                    {
                        para.put("code", "200");
                        para.put("msg", compareStatus.getDescription());
                    }
            } else
            {
                para.put("code", "400");
                para.put("msg", compareFileResultDto.getMessage());
            }
        }
        catch(Exception e)
        {
            e.printStackTrace();
            para.put("code", "400");
            // 文件对比出现异常:
            para.put("msg", (new StringBuilder("\u6587\u4EF6\u5BF9\u6BD4\u51FA\u73B0\u5F02\u5E38\uFF1A")).append(e.getMessage()).toString());
        }
        return para;
    }
}
// Utils.java

package com.api.weavernorth.gyrx.util;

import com.wbi.bus.util.DocUtil;
import java.io.*;
import java.util.UUID;
import weaver.conn.RecordSet;
import weaver.general.BaseBean;
import weaver.general.Util;

public class Utils
{

    public Utils()
    {
    }

    public static String downLoadLocal(String imageFileId)
    {
        BaseBean baseBean;
        InputStream inputStream;
        String filePath;
        File file02;
        OutputStream fos;
        String dosPath = (new StringBuilder("/app/weaver/ecology/filesystem/tempfile/")).append(UUID.randomUUID()).toString();
        baseBean = new BaseBean();
        RecordSet rs = new RecordSet();
        rs.execute((new StringBuilder("select imagefilename,imagefileid from ImageFile where imagefileid=")).append(imageFileId).toString());
        String imagefileid = "";
        String fileName;
        for(fileName = ""; rs.next(); fileName = Util.null2String(rs.getString("imagefilename")))
            imagefileid = Util.null2String(rs.getString("imagefileid"));

        if("".equals(imageFileId) || "".equals(fileName))
            return "";
        inputStream = DocUtil.getInputStreamById(Integer.valueOf(imagefileid).intValue());
        File file = new File(dosPath);
        if(!file.exists())
            file.mkdirs();
        filePath = (new StringBuilder(String.valueOf(dosPath))).append("/").append(fileName).toString();
        baseBean.writeLog((new StringBuilder("dosPath------>")).append(dosPath).toString());
        baseBean.writeLog((new StringBuilder("filePath------>")).append(filePath).toString());
        file02 = new File(filePath);
        if(!file02.exists())
            try
            {
                file02.createNewFile();
            }
            catch(IOException e)
            {
                e.printStackTrace();
            }
        fos = null;
        baseBean.writeLog((new StringBuilder("OutputStream------>")).append(filePath).toString());
        fos = new FileOutputStream(file02);
        byte buffer[] = new byte[4096];
        int bytesRead;
        while((bytesRead = inputStream.read(buffer)) != -1) 
            fos.write(buffer, 0, bytesRead);
        break MISSING_BLOCK_LABEL_482;
        FileNotFoundException e;
        e;
        baseBean.writeLog(e.getMessage());
        if(fos != null)
            try
            {
                fos.close();
            }
            catch(IOException e)
            {
                e.printStackTrace();
            }
        if(inputStream != null)
            try
            {
                inputStream.close();
            }
            catch(IOException e)
            {
                e.printStackTrace();
            }
        break MISSING_BLOCK_LABEL_522;
        e;
        baseBean.writeLog(e.getMessage());
        if(fos != null)
            try
            {
                fos.close();
            }
            catch(IOException e)
            {
                e.printStackTrace();
            }
        if(inputStream != null)
            try
            {
                inputStream.close();
            }
            catch(IOException e)
            {
                e.printStackTrace();
            }
        break MISSING_BLOCK_LABEL_522;
        Exception exception;
        exception;
        if(fos != null)
            try
            {
                fos.close();
            }
            catch(IOException e)
            {
                e.printStackTrace();
            }
        if(inputStream != null)
            try
            {
                inputStream.close();
            }
            catch(IOException e)
            {
                e.printStackTrace();
            }
        throw exception;
        if(fos != null)
            try
            {
                fos.close();
            }
            catch(IOException e)
            {
                e.printStackTrace();
            }
        if(inputStream != null)
            try
            {
                inputStream.close();
            }
            catch(IOException e)
            {
                e.printStackTrace();
            }
        return filePath;
    }
}