HttpServletResponse不会将字节流传输到客户端

时间:2017-07-22 06:36:36

标签: java servlets restlet

我一直试图找出为什么Servlet不会使用此代码将字节返回给客户端(尽管根据日志读取字节):

Redirector redirector = new SelfInjectingRedirector(getContext(), targetPattern,
        Redirector.MODE_SERVER_OUTBOUND){

    @Inject
    FileStore fileStore;

    String fileName = "something_for_sample";
    boolean isBufferFirst = true;

    @Override
    public void handle(Request request, Response response) {
        try {
            HttpServletRequest servletRequest = ServletUtils.getRequest(request);
            HttpServletResponse servletResponse = ServletUtils.getResponse(response);
            //
            // Either statement here wont return the bytes to the client
            // 
            if(isBufferFirst) {
                byte[] bytes = fileStore.get(fileName);
                System.out.println("Bytes read: " + bytes.length); // Bytes read: 5731
                servletResponse.getOutputStream().write(bytes, 0, bytes.length)
            } else {
                fileStore.get(fileName, servletResponse.getOutputStream());
            }
        } catch (Exception e) {
            response.setStatus(Status.SERVER_ERROR_INTERNAL);
            e.printStackTrace();
        }
        System.out.println("Handle Done");
    }

};

1 个答案:

答案 0 :(得分:0)

解决方案是添加“Content-Length”标题。