我正在尝试解析我从服务器获得的响应,但我得到了一个例外。这是方法:
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;
}
我得到了这个例外:Jul 16, 2014 5:05:05 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [eu.eee.envs.api.JaxRsActivator] in context with path [/SQAutomationServer] threw exception [org.glassfish.jersey.server.ContainerException: java.io.IOException: unexpected EOF with 512 bytes unread] with root cause
java.io.IOException: unexpected EOF with 512 bytes unread
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)
答案 0 :(得分:1)
我最近遇到了类似的问题。
更改此代码。
while ((offset = myTarStream.read(content)) > 0) {
output.write(content, 0, offset);
}
到
while ((offset = myTarStream.read(content,0,1024)) != -1) {
output.write(content, 0, offset);
}