0

サーバーの 1 つにファイルをアップロードするクライアント コードを Java で作成しました。Ubuntu マシンでは、クライアントは 5 MB/秒の速度でサーバーにファイルをアップロードできます。ただし、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

4

0 に答える 0