tomcat - java.io.IOException : unexpected EOF with 512 bytes unread 异常

标签 tomcat tar

我正在尝试解压缩从服务器获得的响应,但出现异常。这是方法:

 public InputStream getResource(@PathParam("id") String envId, @PathParam("appName") String appName, @PathParam("imageType") String imageType,
                               @QueryParam("resourcePath") String resourcePath) throws EnvAutomationException, IOException, InterruptedException {Environment env = Envs.getEnvironmentManager().findEnvironment(envId);
    ApplicationInstance appInst = env.getApplicationInstance(appName);
    Container container = appInst.getContainer(imageType);
    InputStream resource = Envs.getContainerizationManager().getResource(container, resourcePath);    

    if (resource != null && resource.available() > 0) {
        TarInputStream myTarStream = new TarInputStream(resource);
        TarEntry entry = myTarStream.getNextEntry();
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        byte[] content = new byte[1024];
        int offset = 0;
        while ((offset = myTarStream.read(content)) > 0) {
            output.write(content, 0, offset);
        }

        output.flush();
        byte[] bo = output.toByteArray();
        InputStream lastSentValues = new ByteArrayInputStream(bo);
        return lastSentValues;
    }
    return null;

}

我得到了这个异常:2014 年 7 月 16 日下午 5:05:05 org.apache.catalina.core.StandardWrapperValve 调用 严重:servlet [eu.eee.envs.api.JaxRsActivator] 在路径 [/SQAutomationServer] 上下文中的 Servlet.service() 抛出异常 [org.glassfish.jersey.server.ContainerException: java.io.IOException: unexpected EOF with 512 字节未读] 有根本原因 java.io.IOException:意外的 EOF,有 512 个字节未读

at org.codehaus.plexus.archiver.tar.TarInputStream.read(TarInputStream.java:376)
at org.codehaus.plexus.archiver.tar.TarInputStream.read(TarInputStream.java:314)
at eu.eee.envs.api.Resources.getResource(Resources.java:44)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:606)

最佳答案

我最近遇到了类似的问题。

更改此代码。

while ((offset = myTarStream.read(content)) > 0) {
        output.write(content, 0, offset);
    }

   while ((offset = myTarStream.read(content,0,1024)) != -1) {
            output.write(content, 0, offset);
        }

关于tomcat - java.io.IOException : unexpected EOF with 512 bytes unread 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24784649/

相关文章:

macos - mac os x 中的 tar 命令添加 "hidden"文件,为什么?

linux - 从 Tar 存档中按大小降序提取 3 个最小的文件

linux - Tar 当前目录中的 100 个目录

tomcat - JVM、JAVA binary 和 Tomcat..哪一个提供 servlet 容器?

java - SOAP Web 服务,可以进行性能测试吗?

tomcat - 通过命令提示符在tomcat中部署war文件

compression - 创建没有文件夹结构的 tar 文件

java - 使用Java运行 "tar"命令,但生成tar文件需要花费大量时间

java - Docker 中的 Tomcat

tomcat - 如何使 tomcat 随着内核数量的增加而可扩展?