简体   繁体   English

java.io.IOException:在ByteArrayInputStream中传递时,不是GZIP格式

[英]java.io.IOException: Not in GZIP format when passing in ByteArrayInputStream

        final ByteArrayOutputStream stream = new ByteArrayOutputStream();
        try {
            jaxbContext.createMarshaller().marshal(proposal, stream);
            try {
                    stream.close();
            } catch (IOException e) {
                throw new PersistenceException("Failed to close output stream for " + proposal.getUniqueId());
            }
            // call persistenceService.put to send data to respective PersistenceServiceClass
            InputStream inputStream = new ByteArrayInputStream(stream.toByteArray());
            persistenceService.put(inputStream, proposal.getUniqueId());

persistenceService.put looks like persistenceService.put看起来像

public void put(@Nonnull final InputStream inputStream, @Nonnull final String uniqueId) throws PersistenceException {
        // a.) create xml.zip of inputStream as inputStream
        final GZIPInputStream zipInputStream;
        try {
            zipInputStream = new GZIPInputStream(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
            throw new PersistenceException("Persistence Service could not received input stream to persist for " + uniqueId);
        }

        // b.) call amazonS3client.putObject() to put data
        final PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, getProposalName(uniqueId), zipInputStream, new ObjectMetadata());
    }

When I run this code, I see the following stacktrace 当我运行此代码时,我看到以下堆栈跟踪

java.io.IOException: Not in GZIP format
    at java.util.zip.GZIPInputStream.readHeader(GZIPInputStream.java:141)
    at java.util.zip.GZIPInputStream.<init>(GZIPInputStream.java:56)
    at java.util.zip.GZIPInputStream.<init>(GZIPInputStream.java:65)
    at com.sunrunhome.sparrow.business.xml.persist.S3Service.put(S3Service.java:71)
    at com.sunrunhome.sparrow.business.ProposalManager.putDocument(ProposalManager.java:56)
    at com.sunrunhome.sparrow.business.ProposalManagerTest.testWriteToS3(ProposalManagerTest.java:165)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:28)
    at org.junit.rules.ExternalResource$1.evaluate(ExternalResource.java:46)
    at org.junit.rules.ExternalResource$1.evaluate(ExternalResource.java:46)
    at org.junit.rules.RunRules.evaluate(RunRules.java:18)

What is that I am not doing right here? 我现在不在这里做什么?

private static void gzip(File file) {
    //logic to extract file
    GZIPInputStream in = null;
    FileInputStream fin = null;
    OutputStream out = null;
    int noread = 0;

    try {
        in = new GZIPInputStream(new FileInputStream(file));
        extractedFile = new File("C:\\DATA\\extracted.txt");
        out = new FileOutputStream(extractedFile);
        byte[] buffer = new byte[65536];
        noread = in.read(buffer);

        while(noread > 0) {
            out.write(buffer, 0, noread);
            System.out.println("done");
            break;
        }
    }

You can probabaly pass it as fileInputStream and test it 您可以将其作为fileInputStream传递并进行测试

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM