0

我正在尝试将消息(字符串)压缩到 zip 文件中,然后将其设置为 Apache Camel 中的交换对象的主体,以便下游服务之一(也使用 Apache Camel)能够提取 zip文件使用exchange.getIn().getBody()方法。

第一部分很好,我可以将 zip 文件设置body为. 事实上,它本身就是.exchange.getIn().getBody(ZipFile.class)nullbodynull

为什么会这样?

我试过在正文中发送一个普通的字符串,效果很好。文件(ZipFile)没有设置,想知道为什么。

以下是代码片段 -

路线 -

from(some_route)
.bean(SomeClass.class, "zipAndSend")
.to("activemq:queue:" + somequeue)

压缩文件 -

public void zipAndSend(Exchange exchange) throws Exception {
        String incomingMessage;
        try {
            incomingMessage = exchange.getIn().getBody().toString();
            File file = ZipUtil.createFile(incomingMessage);
            String zipFilePath = file.getPath().replace(".xml", ".zip");
            ZipFile zipFile = ZipUtil.zipFile(file.getPath(), zipFilePath);

            exchange.getOut().setHeader("Compressed", "Y");
            exchange.getOut().setHeader("ZipFilePath", zipFilePath);
            exchange.getOut().setBody(zipFile);

//the body is set correctly here, so far so good

        } catch (Exception e) {
            e.printStackTrace(); //other operations


        }
    }

public static File createFile(String incomingMessaage) {

        String fileName = "C:\\Project\\ZipUnzipTest\\incoming.xml";
        File file = new File(fileName);
        try {
            FileUtils.writeStringToFile(file, incomingMessaage);
        } catch (Exception e) {
            //log.error("Error in Writing Message into file " + fileName, e);
            String errorFile = fileName.replace("work", "error");
        }
        return file;
    }

在另一个服务(队列末尾)中,我重写了process()如下所示的方法,以便能够从压缩文件中的文件中提取消息(字符串)。

public void process(Exchange exchange) throws WorkflowDBException,
            Exception {

        try {
            ZipFile zipFile = exchange.getIn().getBody(ZipFile.class); //NPE as body is null
            String zipFilePath = exchange.getIn().getHeader("ZipFilePath").toString();
            File inFile = ZipUtil.unzipFile(zipFile, "C:\\Project\\ZipUnzipTest\\Output\\WF", true);
            String incomingMessage;
            incomingMessage = FileUtils.readFileToString(inFile, "UTF-8");
} catch (Exception e) {e.printStackTrace();}
}

依赖项 -

<dependency>
            <groupId>net.lingala.zip4j</groupId>
            <artifactId>zip4j</artifactId>
            <version>1.3.2</version>
        </dependency>
<dependency>
            <groupId>commons-io</groupId>
            <artifactId>commons-io</artifactId>
            <version>1.3.2</version>
        </dependency>

我希望正文的内容在inandout空间中是相同的。唉,事实并非如此。

4

1 回答 1

0

事实证明,Camel(在其他框架中)处理bytes[]得相当好。我将 the 转换ZipFile为 a byte array,并用它来设置对象body的 the exchange

incomingMessage = exchange.getIn().getBody().toString();
File file = ZipUtil.createFile(incomingMessage);
String zipFilePath = file.getPath().replace(".xml", ".zip");
ZipFile zipFile = ZipUtil.zipFile(file.getPath(), zipFilePath);
messageData = FileUtils.readFileToByteArray(new File(zipFilePath));

exchange.getOut().setHeader("Compressed", "Y");
exchange.getOut().setHeader("ZipFilePath", zipFilePath);
exchange.getOut().setBody(messageData);

在阅读它时,我使用ZipInputStreamZip EntryByteArrayInputStream.

byte [] bytes = exchange.getIn().getBody(byte[].class);
ZipInputStream zipStream = new ZipInputStream(new ByteArrayInputStream(bytes));
try {
    StringBuilder s = new StringBuilder();
    StringBuilder temp = new StringBuilder();
    byte[] buffer = new byte[1024];
    int read = 0;
    ZipEntry entry;
    while ((entry = zipStream.getNextEntry())!= null) {
        while ((read = zipStream.read(buffer, 0, 1024)) >= 0) {
            s.append(new String(buffer, 0, read));
        }
        temp = temp.append(s);
        s.setLength(0);
    }
    return temp.toString();
} catch (Exception e) {
            e.printStackTrace();
}

对,就是那样。仍然对解决此问题的其他方法持开放态度:)

于 2019-08-08T01:29:23.273 回答