mule read single file from classpath during flow

前端 未结 8 1742
渐次进展
渐次进展 2020-12-05 11:13

Is there an easy way to configure a Flow to read a single file from the classpath one time? I don\'t need to poll for a file. I just need to read a known file and set its co

8条回答
  •  感动是毒
    2020-12-05 11:55

    I am trying to throw Java class transformer so you can use the following

    note : the path is the direct package that contain the file you want to read am store the path inside mule variable and then read the file content and stor its value into Payload

    .

    public class PayloadFileReader extends AbstractMessageTransformer {
    public Object transformMessage(MuleMessage message, String outputEncoding)
            throws TransformerException {
    
        String result = "";
        try {
            result = readFileTest(message.getInvocationProperty("path")
                    .toString());
        } catch (Exception e) {
            e.printStackTrace();
        }
        message.setPayload(result);
        return message;
    }
    
    
    public String readFileTest(String path) throws FileNotFoundException,
            IOException, Exception {
    
    
        ClassLoader classLoader = getClass().getClassLoader();
    
    "+classLoader.getResource("samples/v3/addVal-request.sample").getFile());
    
        File file = new File(classLoader.getResource(path).getFile());
        FileReader fileReader = new FileReader(file);
        BufferedReader bufferReader = null;
        StringBuilder stringBuffer = new StringBuilder();
        String line;
        try {
            bufferReader = new BufferedReader(fileReader);
            while ((line = bufferReader.readLine()) != null) {
                stringBuffer.append(line);
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (bufferReader != null) {
                try {
                    bufferReader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return stringBuffer.toString();
    }
    

提交回复
热议问题