我有一个要求,其中我将从html表单提交数据到Mule流程,而Mule应该返回一个PDF文件。我创建了以下流程。
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:http="http://www.mulesoft.org/schema/mule/http"
xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking" xmlns="http://www.mulesoft.org/schema/mule/core"
xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:spring="http://www.springframework.org/schema/beans" version="EE-3.4.1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/ee/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd">
<flow name="SampleMuleFlow1" doc:name="SampleMuleFlow1">
<http:inbound-endpoint exchange-pattern="request-response"
host="localhost" port="8081" path="test" doc:name="HTTP" contentType="application/pdf"
mimeType="application/pdf">
</http:inbound-endpoint>
<custom-transformer returnClass="org.mule.api.MuleMessage"
mimeType="application/pdf"
class="com.tcs.pdfgen.GenerateGFEService_GenerateGFEServiceImplPort_Client"
doc:name="Java" />
</flow>
</mule>
I attach the PDF file through the api message.addOutboundAttachment().Please check the Custom Tranformer file i have written.
package com.tcs.pdfgen;
/**
* Please modify this class to meet your needs
* This class is not complete
*/
import java.io.ByteArrayOutputStream;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.URL;
import javax.xml.namespace.QName;
import org.apache.cxf.attachment.LazyDataSource;
import org.mule.api.MuleMessage;
import org.mule.api.transformer.TransformerException;
import org.mule.transformer.AbstractMessageTransformer;
@Override
public Object transformMessage(MuleMessage message, String outputEncoding)
throws TransformerException {
try {
GenerateGFEService_GenerateGFEServiceImplPort_Client client = new GenerateGFEService_GenerateGFEServiceImplPort_Client();
/*client.genGFE(message, "GeneratedSamplePDF.pdf") method return a byte array of the PDF file.*/
message.addOutboundAttachment("GFEFile",
client.genGFE(message, "GeneratedSamplePDF.pdf"),
"application/pdf");
System.out.println("Attachment Names>>>>"
+ message.getOutboundAttachmentNames());
System.out.println("ApplicationHome settings>>>"
+ message.getOutboundProperty("ApplicationHome"));
return message;
} catch (Exception ioe) {
System.out.println("exception 1111");
System.out.println("Exception Message>>>>>>" + ioe.getMessage());
throw new TransformerException(this, ioe);
} catch (Throwable t) {
System.out.println("exception?>>>" + t.getMessage());
} finally {
try {
} catch (Throwable t) {
}
}
return message;
}
}
但是在浏览器上,我收到错误,PDF不是以“%PDF”开头。我相信PDF附件不是通过HTTP响应对象发送的。请告诉我,如果我遗漏了什么。
答案 0 :(得分:0)
将PDF内容输入流设置为消息有效负载而不是附件:这样Mule会将其流回浏览器。