【问题标题】:How much data we can pass through URLConnection.?我们可以通过 URLConnection 传递多少数据?
【发布时间】:2015-04-16 21:03:49
【问题描述】:

我以 json 格式将 10 个学生信息从一个应用程序传递到另一个应用程序。我必须将 25 个 lacs 学生信息从一个应用程序发送到另一个应用程序。我想知道,我可以发送多少数据?我可以发送多少个 json 格式的学生。?
以下是发送方应用程序的代码。

    JSONObject jsonObject = new JSONObject();
    JSONArray jArray = new JSONArray();
    try {
        for (Student student : listStudent) {        
            JSONObject studentJSON = new JSONObject();
            studentJSON.put("First Name", student.getFirstName());
            studentJSON.put("Last Name", student.getLastName());
            jArray.put(studentJSON);
        }
        jsonObject.put("StudentArray", jArray);
        response.setContentType("application/json");
        response.getWriter().write(jsonObject.toString());
    } catch (IOException e) {
        e.printStackTrace();
    }

以下是接收器应用程序的代码。

     URL url = new URL("http:"//xyz.com);
     BufferedReader reader = new BufferedReader(newInputStreamReader(url.openStream()));
     JSONObject object = null ;
     String line=null;
     while ((line = reader.readLine()) != null) 
     {
       object= new JSONObject(line);
     }

【问题讨论】:

  • 取决于您发送数据的服务器...
  • 您是遇到错误还是这只是一个一般性问题?
  • 一般问题。我已经完成了它的开发。但我想澄清我的疑问,这样如果一个应用程序一次以 json 格式发送更多(25 lacs)用户信息,它就不会中断。

标签: java json urlconnection


【解决方案1】:

URLConnection 支持HTTP Specification 用于 HTTP 连接。我还没有真正看到可以通过 HTTP 传输的最大字节数,但是如果存在,大小必须与标头 Content-Length 匹配。否则将继续读取字节,直到连接关闭。

URLConnection 将 Content-Length 的值视为Java 1.6 及更早版本中的intJava 1.7 及更高版本中的long,这表明正文中支持的最大字节数将分别为 2^31-1 字节或 2^63-1 字节,只要设置了 Content-Length。

【讨论】:

  • Content-length 是自动设置的。这不是“只要设置Content-length”的问题。
  • 根据规范,Content-Length 不是必需的,如果没有提供,甚至还有一个用于确定长度的特殊部分。如果您专门针对 Java Web 服务器,您能否提供一个链接来支持您的主张?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-10-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-09
  • 1970-01-01
相关资源
最近更新 更多