私は比較的単純なことをしようとしています。自分の管理下にないサーバーにファイルをアップロードするには、本文にファイルを含む単純な 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()});
ネットワーク マネージャーを監視していますが、ファイル サイズに近いものが送信されません。実際、何かが送信されているかどうかはわかりませんが、エラーがスローされることはありません。
アップロードの進行状況をリスナーに通知できるように、このリクエストを送信し、アップロードのステータスを同期的に測定できるようにする必要があります。既存の例を修正して、ただ動くようにするにはどうすればよいですか?