0

我有 spring+camel+FTP 模块。我需要从 FTP 服务器下载文件并使用 ZipInputStream 从 zip 文件中提取数据。这是我的附加课程:

public class TransactionsSupport {

    protected final Logger l = LoggerFactory.getLogger(getClass());

    protected void copy(InputStream is, OutputStream os, byte[] buffer) throws IOException {
        int len;
        while ((len = is.read(buffer)) >= 0) {
            os.write(buffer, 0, len);
        }
        is.close();
        os.close();
    }

    public void copy(InputStream is, OutputStream os) throws IOException {
        copy(is, os, new byte[4096]);
    }

    public byte[] unzip(ZipInputStream zipInputStream) {
        byte[] bytes = null;
        try {
            ZipEntry entry = zipInputStream.getNextEntry();
            l.info("{}", entry.getName());
            ByteArrayOutputStream streamBuilder = new ByteArrayOutputStream();
            copy(zipInputStream, streamBuilder);
            bytes = streamBuilder.toByteArray();
            l.info("bytes.length {}", bytes.length);
        } catch (IOException ex) {
            l.error(ex.getMessage(), ex);
        } finally {
            if (zipInputStream != null) {
                try {
                    zipInputStream.close();
                } catch (IOException ex) {
                    l.error(ex.getMessage(), ex);
                }
            }
        }
        return bytes;
    }

我成功地用本地文件测试了这段代码。

public void inputStream2OutputStream(InputStream stream, OutputStream out) throws IOException {
        int readedBytes;
        byte[] buf = new byte[1024];
        while ((readedBytes = stream.read(buf)) > 0) {
            out.write(buf, 0, readedBytes);
        }
        stream.close();
        out.close();
    }

    @Test
        public void fromByteArrayStreamToZipInputStreamTest() throws IOException, UnsupportedTransactionType, WrongRecordsNumberException, WrongCheckSum {
            File fileIn = new File((postedTransactionPath + postedTransactionFileName));
            FileInputStream in = new FileInputStream(fileIn);
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            inputStream2OutputStream(in, out);
            InputStream inS = new ByteArrayInputStream(out.toByteArray());
            ZipInputStream s = new ZipInputStream(inS);
            byte[] bytes = support.unzip(s);
            inS.close();
            s.close();

            String text = new String(bytes);
    l.info(text);
    parser.parse(text);

// 没关系 }

但是当我尝试通过骆驼处理它时,我得到了“ZLIB 输入流的意外结束”这是我的处理器:

@Override
    public void process(Exchange exchange) throws Exception {
        l.info(exchange);
        RemoteFile remoteFile = (RemoteFile) exchange.getIn().getBody();
        ByteArrayOutputStream streamOut = (ByteArrayOutputStream) remoteFile.getBinding().getBody(new GenericFile());
        ZipInputStream zipStream = new ZipInputStream(new ByteArrayInputStream(streamOut.toByteArray()));
        l.info("file name: {}" + remoteFile.getFileName());
        TransactionsSupport support = new TransactionsSupport();
        byte[] bytes = support.unzip(zipStream); //Here we have exception that is refferenced to method copy() of TransactionsSupport class
        String text = new String(bytes);
        l.info(text);
        parser.parse(text);
        if (((FirstDataTransactionsParser) parser).getHeader() == null) {
            l.error("ERRORRRRR");
        }

我想骆驼或弹簧在处理之前关闭了我的输入流。那么我能做些什么来防止这种行为,如果我想手动关闭流。

这是我的路线:

  <routeContext id="transactionsRoutes" xmlns="http://camel.apache.org/schema/spring">

        <route id="routeFTP">
            <from uri="direct:start" />
            <from uri="ftps://{{ftps.host}}:2190/"/>
            <to uri="file://target/test-reports1234/"/>
            <doTry>
                <bean ref="transactionsProcessor"/>
                <doCatch>
                    <exception>java.lang.Exception</exception>
                    <bean ref="exceptionProcessor"/>
                </doCatch>
                <doFinally>
                    <bean ref="responsePublisher"/>
                </doFinally>
            </doTry>
        </route>
    </routeContext>

    <camelContext id="com.nxsystems.camel" xmlns="http://camel.apache.org/schema/spring" trace="true">
         <routeContextRef ref="transactionsRoutes"/>
    </camelContext>
4

0 回答 0