栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

Mule Zip File并将压缩文件发送到FTP服务器

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Mule Zip File并将压缩文件发送到FTP服务器

我已经为此实现了变压器

    package com.test.transformer;import java.io.IOException;import java.io.InputStream;import java.util.zip.ZipEntry;import java.util.zip.ZipOutputStream;import org.apache.commons.io.IOUtils;import org.apache.commons.io.output.ByteArrayOutputStream;import org.mule.api.MuleMessage;import org.mule.api.transformer.TransformerException;import org.mule.transformer.AbstractMessageTransformer;import org.slf4j.Logger;import org.slf4j.LoggerFactory;public class ZipTransformer  extends AbstractMessageTransformer{  private static final Logger log = LoggerFactory.getLogger(ZipTransformer.class);  public static final int DEFAULT_BUFFER_SIZE = 32768;  public static byte[] MAGIC = { 'P', 'K', 0x3, 0x4 };  public ZipTransformer()  {    registerSourceType(InputStream.class);    registerSourceType(byte[].class);  }  public Object transformMessage(MuleMessage message, String outputEncoding)    throws TransformerException  {    Object payload = message.getPayload();    try{        byte[] data;        if (payload instanceof byte[])        { data = (byte[]) payload;        }        else if (payload instanceof InputStream) { data = IOUtils.toByteArray((InputStream)payload);        }         else if (payload instanceof String)        { data = ((String) payload).getBytes(outputEncoding);        }        else        { data = muleContext.getObjectSerializer().serialize(payload);        }        return compressByteArray(data);    }catch (Exception ioex)    {        throw new TransformerException(this, ioex);    }  }  public Object compressByteArray(byte[] bytes) throws IOException  {      if (bytes == null || isCompressed(bytes))      {          if (logger.isDebugEnabled())          {   logger.debug("Data already compressed; doing nothing");          }          return bytes;      }      if (logger.isDebugEnabled())      {          logger.debug("Compressing message of size: " + bytes.length);      }      ByteArrayOutputStream baos = null;      ZipOutputStream  zos = null;      try      {          baos = new ByteArrayOutputStream(DEFAULT_BUFFER_SIZE);          zos = new ZipOutputStream(baos);          zos.putNextEntry(new ZipEntry("test.txt"));          zos.write(bytes, 0, bytes.length);          zos.finish();          zos.close();          byte[] compressedByteArray = baos.toByteArray();          baos.close();          if (logger.isDebugEnabled())          {   logger.debug("Compressed message to size: " + compressedByteArray.length);          }          return compressedByteArray;      }      catch (IOException ioex)      {          throw ioex;      }      finally      {          IOUtils.closeQuietly(zos);          IOUtils.closeQuietly(baos);      }  }  public boolean isCompressed(byte[] bytes) throws IOException  {      if ((bytes == null) || (bytes.length < 4 ))      {          return false;      }      else      {          for (int i = 0; i < MAGIC.length; i++) {     if (bytes[i] != MAGIC[i]) {      return false;     }          }          return true;      }  }}

用作

<custom-transformer  doc:name="file zip transformer"/>

截至目前,将文件名设置为test.txt。您可以更改使用任何属性或变量。

希望这可以帮助。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/516158.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号