0

我的应用程序在尝试上传任何文件时抛出此错误:

java.io.IOException: Stream closed
        at java.base/java.io.PushbackInputStream.ensureOpen(PushbackInputStream.java:75)
        at java.base/java.io.PushbackInputStream.read(PushbackInputStream.java:163)
        at java.base/java.io.FilterInputStream.read(FilterInputStream.java:107)
        at com.google.common.io.ByteStreams.copy(ByteStreams.java:70)
        at com.google.common.io.ByteStreams.toByteArray(ByteStreams.java:115)
        at com.mycompany.backend.resource.impl.FileServerResource.createFile(FileServerResource.java:120)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.base/java.lang.reflect.Method.invoke(Method.java:566)
        at org.restlet.resource.ServerResource.doHandle(ServerResource.java:508)
        at org.restlet.resource.ServerResource.post(ServerResource.java:1341)
        at org.restlet.resource.ServerResource.doHandle(ServerResource.java:606)
        at org.restlet.resource.ServerResource.doNegotiatedHandle(ServerResource.java:662)
        at org.restlet.resource.ServerResource.doConditionalHandle(ServerResource.java:348)
        at org.restlet.resource.ServerResource.handle(ServerResource.java:1020)
        at org.restlet.resource.Finder.handle(Finder.java:236)
        at org.restlet.routing.Filter.doHandle(Filter.java:150)
        at org.restlet.routing.Filter.handle(Filter.java:197)
        at org.restlet.routing.Router.doHandle(Router.java:422)
        at org.restlet.routing.Router.handle(Router.java:641)
        at org.restlet.routing.Filter.doHandle(Filter.java:150)
        at org.restlet.routing.Filter.handle(Filter.java:197)
        at org.restlet.routing.Filter.doHandle(Filter.java:150)
        at org.restlet.routing.Filter.handle(Filter.java:197)
        at org.restlet.routing.Filter.doHandle(Filter.java:150)
        at org.restlet.routing.Filter.handle(Filter.java:197)
        at org.restlet.routing.Filter.doHandle(Filter.java:150)
        at org.restlet.routing.Filter.handle(Filter.java:197)
        at org.restlet.routing.Filter.doHandle(Filter.java:150)
        at org.restlet.routing.Filter.handle(Filter.java:197)
        at org.restlet.routing.Filter.doHandle(Filter.java:150)
        at org.restlet.engine.application.StatusFilter.doHandle(StatusFilter.java:140)
        at org.restlet.routing.Filter.handle(Filter.java:197)
        at org.restlet.routing.Filter.doHandle(Filter.java:150)
        at org.restlet.routing.Filter.handle(Filter.java:197)
        at org.restlet.engine.CompositeHelper.handle(CompositeHelper.java:202)
        at org.restlet.engine.application.ApplicationHelper.handle(ApplicationHelper.java:77)
        at org.restlet.Application.handle(Application.java:385)
        at org.restlet.routing.Filter.doHandle(Filter.java:150)
        at org.restlet.routing.Filter.handle(Filter.java:197)
        at org.restlet.routing.Router.doHandle(Router.java:422)
        at org.restlet.routing.Router.handle(Router.java:641)
        at org.restlet.routing.Filter.doHandle(Filter.java:150)
        at org.restlet.routing.Filter.handle(Filter.java:197)
        at org.restlet.routing.Router.doHandle(Router.java:422)
        at org.restlet.routing.Router.handle(Router.java:641)
        at org.restlet.routing.Filter.doHandle(Filter.java:150)
        at org.restlet.routing.Filter.handle(Filter.java:197)
        at org.restlet.engine.CompositeHelper.handle(CompositeHelper.java:202)
        at org.restlet.Component.handle(Component.java:408)
        at org.restlet.Server.handle(Server.java:507)
        at org.restlet.engine.connector.ServerHelper.handle(ServerHelper.java:63)
        at org.restlet.engine.adapter.HttpServerHelper.handle(HttpServerHelper.java:143)
        at org.restlet.ext.servlet.ServerServlet.service(ServerServlet.java:1117)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
        at nginx.unit.Context$ServletReg.service(Context.java:2011)
        at nginx.unit.Context$CtxFilterChain.doFilter(Context.java:750)
        at nginx.unit.Context.service(Context.java:971)

这个问题几乎是孤立的,只发生在使用 NGINX + NGINX Unit 服务器的远程服务器上。开发期间在 localhost / 嵌入式码头上运行不会引发此错误。

引发此错误的 Java 代码:

@Override
public File createFile(Representation entity) {
      if (entity != null
          && MediaType.APPLICATION_OCTET_STREAM.equals(entity.getMediaType())) {
        InputStream inputStream = entity.getStream(); // <--- error thrown here
        File file = fileRepository.put(destinationFile, inputStream);
        setStatus(Status.SUCCESS_CREATED);
        return file;
      }
}

如何从 NGINX 服务器和 Java 应用程序中调试它?我怎样才能找到过早关闭流的原因?

4

1 回答 1

0

当 NGINX proxy_pass 关闭连接时会发生此问题,因此要解决此问题,请执行以下操作:

server { 
  location / { 
    proxy_pass http://upstream; 
    proxy_set_header Connection ""; 
    proxy_http_version 1.1; 
  } 
}

设置proxy_set_header Connection ""

于 2020-12-19T01:50:54.580 回答