我用 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