简体   繁体   中英

How can I pass zip file to payload in Mule java transformer?

I am using Anypoint Studio 6.2 and Mule Runtime 3.8.3.

I want to take a file in the src/main/resources/input folder and zip it and save it to src/main/resources/output and then replace the message payload with the zip file.

To do this I have added a java transformer which references a java class I have called Zip which zips the file and saves it to the output folder but how to I add the newly created zip file to the payload of the message?

Java Zip Class:

public class Zip extends AbstractMessageTransformer
{
    public Object transformMessage(MuleMessage message, String outputEncoding) throws TransformerException {
        System.out.print("***java.utils.Zip started***");
        String sourceFile = "src/main/resources/input/log4j2.xml";
        FileInputStream fis = null;
        FileOutputStream fos = null;

        try {
            fos = new FileOutputStream("src/main/resources/output/log4j2.zip");
            ZipOutputStream zipOut = new ZipOutputStream(fos);
            File fileToZip = new File(sourceFile);
            fis = new FileInputStream(fileToZip);
            ZipEntry zipEntry = new ZipEntry(fileToZip.getName());
            zipOut.putNextEntry(zipEntry);
            final byte[] bytes = new byte[1024];
            int length;
            while((length = fis.read(bytes)) >= 0) {
                zipOut.write(bytes, 0, length);
            }
            message.setPayload(????);
            zipOut.close();
            fis.close();
            fos.close();
        } catch (FileNotFoundException e) {
            //File not found
            e.printStackTrace();
        } catch (IOException e) {
            //IO Exception
            e.printStackTrace();
        }
        System.out.print("***java.utils.Zip completed***");  
        return message;
    }
}

Thanks

message.setPayload('src/main/resources/output/log4j2.zip');

Now, you will have location of the zip file in the payload that can be referred.

or else create the property invocation with the zip file path and that can be used in set payload component.

message.setProperty("test", "src/main/resources/output/log4j2.zip", PropertyScope.INVOCATION);

In set payload this can be used as #[flowVars.test]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM