我用Java编写了一个客户端代码,用于将文件上传到我们的服务器之一。在Ubuntu机器中,客户端能够以5 MB / s的速率将文件上载到服务器。但是在Windows 7中,上传仅以60 kB / s的速率发生(禁用防病毒和防火墙)。
代码段如下:
.......................
URL server = new URL(url);
HttpURLConnection connection = (HttpURLConnection)server.openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
connection.setInstanceFollowRedirects(false);
connection.setRequestMethod("POST");
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("content-length",fsize);
connection.setRequestProperty("charset", "UTF-8");
connection.setRequestProperty("file-name", file.getName());
connection.setFixedLengthStreamingMode((int)file.length());
connection.setReadTimeout(30000);
if (file.exists()) {
connection.connect();
OutputStream out = connection.getOutputStream();
BufferedInputStream in = new BufferedInputStream(new FileInputStream(path));
byte []buf = new byte[256*1024];
int length=0;
try {
while ((length=in.read(buf))!=-1) {
out.write(buf, 0,length);
}
out.flush();
}
..............
请告诉我是否需要添加任何HTTP标头(或)我应该对代码进行任何更改。
的问候, kit_kings