Java ZLIB输入流意外结束。谁关闭了我的小溪?
我有spring+camel+FTP模块。 我需要从FTP服务器下载文件,并使用ZipInputStream从zip文件中提取数据。 这是我的额外课程:Java ZLIB输入流意外结束。谁关闭了我的小溪?,java,spring,stream,zip,apache-camel,Java,Spring,Stream,Zip,Apache Camel,我有spring+camel+FTP模块。 我需要从FTP服务器下载文件,并使用ZipInputStream从zip文件中提取数据。 这是我的额外课程: public class TransactionsSupport { protected final Logger l = LoggerFactory.getLogger(getClass()); protected void copy(InputStream is, OutputStream os, byte[] buff
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);
//itsok
}
但当我试图通过camel处理它时,我得到了“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");
}
我假设camel或spring在处理输入流之前关闭了它。那个么,若我想手动关闭流,我可以做些什么来防止这种行为呢
以下是我的路线:
<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>
java.lang.Exception
咒骂是不受欢迎的。。。即使是在日志消息中:)(事实上,我也会说在日志或调试消息中写任何非正式的东西是一个非常坏的习惯:它最终会咬到你的背!)事实上,我已经为这种坏习惯付出了代价,当时我以为我对svn的评论没有人读。但是他们读了)。幸运的是,对我来说,这只是一个小问题song@DmitryBorovoy因此,这篇文章,咒骂性的和所有的,将被存档供后代(和未来的雇主)看。我建议你清理一下语言,有什么想法吗?为什么这条河是封闭的?