Java 使用HttpURLConnection发送包含PUT请求的文件
我试着做一些相对简单的事情。我需要发出一个简单的PUT请求,在正文中包含一个文件,以便将文件上载到不在我控制范围内的服务器。以下是我目前掌握的代码:Java 使用HttpURLConnection发送包含PUT请求的文件,java,http,io,httpurlconnection,Java,Http,Io,Httpurlconnection,我试着做一些相对简单的事情。我需要发出一个简单的PUT请求,在正文中包含一个文件,以便将文件上载到不在我控制范围内的服务器。以下是我目前掌握的代码: connection = ((HttpURLConnection)new URL(ticket.getEndpoint()).openConnection()); connection.setRequestMethod("PUT"); connection.setRequestProperty("Content-Type", "video/mp4"
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()});
我正在监视我的网络管理器,没有发送任何接近我文件大小的内容。事实上,我不知道是否发送了任何内容,但我没有看到任何错误抛出
我需要能够发送此请求,并同步测量上载状态,以便能够通知我的侦听器上载进度。如何修改现有示例以使其正常工作™? 尝试将
内容类型
参数设置为多部分/表单数据
我正在上传一段视频到Vimeo,他们告诉我这样做。