[英]How can I pass zip file to payload in Mule java transformer?
我正在使用Anypoint Studio 6.2和Mule Runtime 3.8.3。
我想在src / main / resources / input文件夾中獲取一個文件並將其壓縮並保存到src / main / resources / output中,然后將郵件有效負載替換為zip文件。
為此,我添加了一個Java轉換器,該轉換器引用了我稱為Zip的Java類,該類將文件壓縮並保存到輸出文件夾中,但是如何將新創建的zip文件添加到消息的有效載荷中呢?
Java Zip類:
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;
}
}
謝謝
message.setPayload('src / main / resources / output / log4j2.zip');
現在,您將在可以引用的有效負載中找到zip文件的位置。
否則,請使用zip文件路徑創建屬性調用,並且可以在設置的有效負載組件中使用該屬性調用。
message.setProperty(“ test”,“ src / main / resources / output / log4j2.zip”,PropertyScope.INVOCATION);
在設置的有效載荷中,可以用作#[flowVars.test]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.