1

私は比較的単純なことをしようとしています。自分の管理下にないサーバーにファイルをアップロードするには、本文にファイルを含む単純な PUT リクエストを作成する必要があります。これまでのコードは次のとおりです。

connection = ((HttpURLConnection)new URL(ticket.getEndpoint()).openConnection());
connection.setRequestMethod("PUT");
connection.setRequestProperty("Content-Type", "video/mp4");
connection.setRequestProperty("Content-Length", String.valueOf(getStreamFile().length()));
connection.setUseCaches(false);
connection.setDoOutput(true);

connection.connect();

outputStream = connection.getOutputStream();

streamFileInputStream = new FileInputStream(getStreamFile());
streamFileBufferedInputStream = new BufferedInputStream(streamFileInputStream);

byte[] streamFileBytes = new byte[getBufferLength()];
int bytesRead = 0;
int totalBytesRead = 0;

while ((bytesRead = streamFileBufferedInputStream.read(streamFileBytes)) > 0) {
    outputStream.write(streamFileBytes, 0, bytesRead);
    outputStream.flush();

    totalBytesRead += bytesRead;

    notifyListenersOnProgress((double)totalBytesRead / (double)getStreamFile().length());
}

outputStream.close();

logger.debug("Wrote {} bytes of {}, ratio: {}", 
        new Object[]{totalBytesRead, getStreamFile().length(), 
            (double)totalBytesRead / (double)getStreamFile().length()});

ネットワーク マネージャーを監視していますが、ファイル サイズに近いものが送信されません。実際、何かが送信されているかどうかはわかりませんがエラーがスローされることはありません。

アップロードの進行状況をリスナーに通知できるように、このリクエストを送信し、アップロードのステータスを同期的に測定できるようにする必要があります。既存の例を修正して、ただ動くようにするにはどうすればよいですか?

4

1 に答える 1

2

content-typeパラメータを に設定してみてくださいmultipart/form-dataW3C フォーム

于 2012-07-10T20:19:46.463 に答える