问题描述
- 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发送文件数据的源码:
/**
* Write the content of a file to the output stream
*
* @param filename the filename of the file to write to the stream
* @param out the stream to write to
* @throws IOException
*/
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();
}
}
}
解决方案
可以试试这个参数:
setFixedLengthStreamingMode()