开发者社区> 问答> 正文

HttpURLConnection怎么设置socket send buffer size大小

蛮大人123 2016-02-20 10:48:22 1258

使用apache-jmeter-2.11做上传文件测试,在服务器端发现每次都是按4k大小的数据接收,而在apache-jmeter-2.11中使用HttpURLConnection做文件上传操作,每次write数据量为1k。
java socket默认的send buffer size为8k,是不是HttpURLConnection将socket的这个默认大小设置为了4k呀?
贴一段apache-jmeter-2.11发送文件数据的源码:

private static void writeFileToStream(String filename, OutputStream out) throws IOException {
 byte[] buf = new byte[1 * 1024]; // default: 1024
 // 1k - the previous 100k made no sense (there's tons of buffers
 // elsewhere in the chain) and it caused OOM when many concurrent
 // uploads were being done. Could be fixed by increasing the evacuation
 // ratio in bin/jmeter[.bat], but this is better.
 InputStream in = new BufferedInputStream(new FileInputStream(filename));
 int read;
 boolean noException = false;
 try {
 int i = 1;
 while ((read = in.read(buf)) > 0) {
 out.write(buf, 0, read);
 //out.flush();
 System.out.println(read+" :"+(i++));
 }
 //out.flush();
 System.out.println("send file done.");
 noException = true;
 }
 finally {
 if(!noException) {
 // Exception in progress
 JOrphanUtils.closeQuietly(in);
 } else {
 in.close();
     }
   }
 }
Java
分享到
取消 提交回答
全部回答(1)
  • 蛮大人123
    2019-07-17 18:45:10

    可以试试这个参数:
    setFixedLengthStreamingMode();

    0 0

集结各类场景实战经验,助你开发运维畅行无忧

推荐文章
相似问题