• 将数据保存本地文件


    package com.icil.edi.ws.common.utils;
    
    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.StringWriter;
    
    import javax.xml.bind.JAXBContext;
    import javax.xml.bind.Marshaller;
    
    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    import org.slf4j.MDC;
    
    import com.fasterxml.jackson.annotation.JsonInclude.Include;
    import com.fasterxml.jackson.databind.ObjectMapper;
    import com.icil.edi.ws.common.constant.CheckpointConstant;
    import com.icil.edi.ws.common.constant.EdiServiceConstant;
    import com.icil.edi.ws.common.model.CommonResponse;
    import com.icil.edi.ws.common.utils.ServicePathUtils;
    
    
    public class FileServiceUtils {
        
        private static final Logger LOGGER = LoggerFactory.getLogger(FileServiceUtils.class);
         
        /**
         * Save request message as a file.
         * 
         * @param  String userName
         * @param  String ediTransId
         */
        public static void saveRequestAsFile(String checkpoint ,String type, String party, String ediTransId)
        {
            LOGGER.info(" enter in FileServiceUtils.saveRequestAsFile() method");
            LOGGER.debug("type is "+type);
            try {
                /**get requestBody content from MDC*/
                String text = MDC.get("requestBody");
                //LOGGER.info("requestBody:"+"
    "+text);
                String file = "";
                if(CheckpointConstant.REC_REQUEST.equalsIgnoreCase(checkpoint)) {
                    file = createFilePath(type, party, ediTransId, EdiServiceConstant.IN_DIR,EdiServiceConstant.REQUEST_FILE_SYMBOL);
                }else if (CheckpointConstant.SEND_REQUEST.equalsIgnoreCase(checkpoint)) {
                    file = createFilePath(type, party, ediTransId, EdiServiceConstant.OUT_DIR,EdiServiceConstant.REQUEST_FILE_SYMBOL);
                }
                /** append content into file*/
                /**Here true is to append the content to file*/
                FileWriter fw = new FileWriter(file,true); 
                /**BufferedWriter writer give better performance*/
                BufferedWriter bw = new BufferedWriter(fw);
                bw.write(text);
                bw.close();
                LOGGER.info(" Save request message into file : " + file);
            } catch (Exception e) {
                LOGGER.error(" Cannot create in file, exception is " + e );
                
            }    
            LOGGER.info(" compile in FileServiceUtils.saveRequestAsFile() method");
        }
        
        /**
         * Save response message as a file.
         * 
         * @param  CommonResponse response
         * @param  String userName
         * @param  String ediTransId
         */
        public static void saveResponseAsFile(String checkpoint,CommonResponse response, String type, String party, String ediTransId,String jsonObj)
        {
            LOGGER.info(" enter in FileServiceUtils.saveResponseAsFile() method");
            try {
                String file = "";
                if(CheckpointConstant.GET_RESPONSE.equalsIgnoreCase(checkpoint)) {
                    file = createFilePath(type, party, ediTransId, EdiServiceConstant.IN_DIR,EdiServiceConstant.RESPONSE_FILE_SYMBOL);
                }else if (CheckpointConstant.SEND_RESPONSE.equalsIgnoreCase(checkpoint)) {
                    file = createFilePath(type, party, ediTransId, EdiServiceConstant.OUT_DIR,EdiServiceConstant.RESPONSE_FILE_SYMBOL);
                }
                /**generate response message*/
                StringWriter sw = null;
                FileWriter fw = new FileWriter(file,true); 
                BufferedWriter bw = new BufferedWriter(fw);
                String jsonInString = "";
                if(jsonObj!=null&&!"".equals(jsonObj)) {
                    jsonInString = jsonObj;
                }else {
                    if(MDC.get("contentType")=="application/json"){
                        ObjectMapper mapper = new ObjectMapper();
                        mapper.setSerializationInclusion(Include.NON_NULL);
                        jsonInString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(response);
                    }else{
                        JAXBContext context = JAXBContext.newInstance(CommonResponse.class);
                        Marshaller m = context.createMarshaller();
                        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
                        sw = new StringWriter();
                        m.marshal(response, sw);
                        jsonInString = sw.toString();
                    }
                }
    
                bw.write(jsonInString);
                bw.close();
                LOGGER.info(" Save response message into file : " + file);
            } catch (Exception e) {
                LOGGER.error(" Cannot create out file, exception is " + e );
                
            }    
            LOGGER.info(" compile in FileServiceUtils.saveResponseAsFile() method");
        }
        
        
        
        /**
         * generate null content file prepare to save in/out message
         * 
         * datasource file like:/data/EDI/@type/@party/in/@ediTransId_request_{YYYYMMddHHmmssSSS}.txt
         * response file like:/data/EDI/@type/@party/out/@ediTransId_response_{YYYYMMddHHmmssSSS}.txt
         * 
         * @param  String type
         * @param  String party
         * @param  String ediTransId
         * @param  String fileType
         * 
         * @return String file
         * @throws IOException 
         */
        private static String createFilePath(String type, String party, String ediTransId, String fileType,String fileSymbol ) throws IOException
        {
            LOGGER.info(" enter in FileServiceUtils:createFilePath() method");
             
            String fileFolder = null;
            String fileName = null;
            String fileFullPath = null;
            String userName = MDC.get("username");
            String dateTime = DateUtils.getTimeFormat("yyyyMMddHHmmssSSS");
            /**
             * 1.According to the fileType,decide filePath,dateTime,fileName
             */
            
            if(EdiServiceConstant.IN_DIR.equals(fileType)){
                LOGGER.info(" enter in FileServiceUtils.createFilePath() method to create in message file");
                //filePath = MilestoneConstant.MESSAGE_FILE_DIR + fileDir + File.separator + MilestoneConstant.IN_DIR ;
                fileFolder = ServicePathUtils.getPathOfServer(userName).get(0);
                fileName = ediTransId + "_" + fileSymbol + "_" + dateTime + ".txt" ;
                fileFullPath = fileFolder + type + "/" +party + "/" + fileType + "/" + fileName;
                MDC.put("inFileName", fileFullPath);
                
            }else if(EdiServiceConstant.OUT_DIR.equals(fileType)){
                LOGGER.info(" enter in FileServiceUtils.createFilePath() method to create out message file");
                //filePath = MilestoneConstant.MESSAGE_FILE_DIR + fileDir + File.separator + MilestoneConstant.OUT_DIR ;
                fileFolder = ServicePathUtils.getPathOfServer(userName).get(0);
                fileName = ediTransId + "_" + fileSymbol + "_" + dateTime + ".txt" ;
                fileFullPath = fileFolder + type + "/" +party + "/" + fileType + "/" + fileName;
                MDC.put("outFileName", fileFullPath);
                
            }else{
                //this is a undefined situation,in this situation,save file will catch exception/
                return null;
            }
            
            //File fileDirectory = new File(filePath);
            LOGGER.info(" file full path is :"+fileFullPath);
        
            
            /**2.generate file*/
    
            File file = new File(fileFullPath);
            if(!file.exists()){
                LOGGER.info(" file is not exists :"+fileFullPath+",go to create it.");
                file.getParentFile().mkdirs();
            }
        
            LOGGER.info(" leave FileServiceUtils:createFilePath() method");
            
            return fileFullPath;
        }
        
        
    }
  • 相关阅读:
    《Android 编程权威指南》学习笔记 : 第19章 数据绑定与MVVM
    《Android 编程权威指南》学习笔记 : 第13章 对话框
    《Android 编程权威指南》学习笔记 : 第17章 资源本地化
    《Android 编程权威指南》学习笔记 : 第15章 隐式 intent
    《Android 编程权威指南》学习笔记 : 第18章 Adroid 辅助功能
    《Android 编程权威指南》学习笔记 : 第14章 应用栏
    《Android 编程权威指南》学习笔记 : 第20章 音频播放与单元测试
    《Android 编程权威指南》学习笔记 : 第16章 使用 intent 拍照
    05月31日总结
    05月30日总结
  • 原文地址:https://www.cnblogs.com/lshan/p/9179497.html
Copyright © 2020-2023  润新知