0

我在向服务器发出 POST 请求并在处理服务器响应结果之后遇到问题,但是当服务器写入数据时,有时数据无法传输到客户端,死在 writer.write() 函数中。我使用Apache Tomcat部署应用程序,每次服务器需要在gzip之前响应大约7m数据,在gzip之后需要300k

请帮忙 !

我的代码:

protected void dispatch(JSONObject request,
              Map<String, FormDataItem> formItems,
              HttpServletRequest servletRequest,
              HttpServletResponse servletResponse, SecurityToken token,
              String callback) throws JSONException, IOException {
        long timeGetData = 0;
        long begin = System.currentTimeMillis();

        String key = null;

        if (request.has("id")) {
              key = request.getString("id");
        }

        // getRpcHandler never returns null
        Future<?> future = getHandler(request, servletRequest).execute(
                    formItems, token, jsonConverter);

        timeGetData = System.currentTimeMillis();
        // Resolve each Future into a response.
        ResponseItem response = getResponseItem(future);
        ResponseItem responseTemp = (ResponseItem) response.getResponse();

        // Generate the output
        Writer writer = servletResponse.getWriter();

        if ("syndata.getAllDataFromServer".equals(request.getString("method"))) {
              if (responseTemp.getErrorCode() == 200) {
                    SynDataObject outObject = (SynDataObject) (responseTemp
                                .getResponse());
                    writer.write(jsonConverter.convertToString(outObject
                                .getParams()));
                    writer.write("\r\n");

                    for (int i = 0; i < outObject.getListTableData().size(); i++) {
                          TableDataObject subData = outObject.getListTableData().get(
                                      i);
                          int subDataLength = subData.getData().size();
                          int currentFarm = 0;
                          int factor = 1000;
                          int lastIndex = 0;

                          while (lastIndex < subDataLength) {
                                if ((currentFarm * factor + factor) > subDataLength) {
                                      lastIndex = (factor * currentFarm)
                                                  + (subDataLength - (factor * currentFarm));
                                } else {
                                      lastIndex = (factor * currentFarm) + factor;
                                }

                                TableDataObject chilData = new TableDataObject();
                                chilData.setTableName(subData.getTableName());
                                chilData.setTableColumns(subData.getTableColumns());
                                chilData.setPkField(subData.getPkField());

                                 chilData.setData(subData.getData().subList(
                                            currentFarm * factor, lastIndex));


                                writer.write(jsonConverter.convertToString(chilData));
                                writer.write("\r\n");
                                writer.flush();

                                ++currentFarm;
                          }
                    }
              } else {
                    Object result = getJSONResponse(key, response);
                    String content = jsonConverter.convertToString(result);

                    if (callback != null) {
                          writer.write(callback);
                          writer.write('(');
                    }

                    writer.write(content);

                    if (callback != null) {
                          writer.write(");\n");
                    }
              }
        } else {
              Object result = getJSONResponse(key, response);
              String content = jsonConverter.convertToString(result);

              if (callback != null) {
                    writer.write(callback);
                    writer.write('(');
              }

              writer.write(content);

              if (callback != null) {
                    writer.write(");\n");
              }
        }

  }
4

1 回答 1

0

每个浏览器都有一些请求超时值。如果服务请求所花费的时间或到达 writer.write() 行所花费的时间超过,则客户端连接将关闭。

所以你将无法向作家写任何信息。

于 2012-09-27T09:08:34.680 回答