开发者社区> 问答> 正文

HttpURLConnection怎么设置socket send buffer size大小

使用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();
     }
   }
 }

展开
收起
蛮大人123 2016-02-20 10:48:22 3657 0
1 条回答
写回答
取消 提交回答
  • 我说我不帅他们就打我,还说我虚伪

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

    2019-07-17 18:45:10
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载